Daily.php
6 years ago
Hourly.php
6 years ago
Monthly.php
6 years ago
Schedule.php
6 years ago
SpecificTime.php
6 years ago
Weekly.php
6 years ago
Monthly.php
145 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Piwik - free/libre analytics platform |
| 4 | * |
| 5 | * @link https://matomo.org |
| 6 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 7 | * |
| 8 | */ |
| 9 | |
| 10 | namespace Piwik\Scheduler\Schedule; |
| 11 | |
| 12 | use Exception; |
| 13 | |
| 14 | /** |
| 15 | * Monthly class is used to schedule tasks every month. |
| 16 | * |
| 17 | * @see \Piwik\Scheduler\Task |
| 18 | */ |
| 19 | class Monthly extends Schedule |
| 20 | { |
| 21 | /** |
| 22 | * List of available week number strings used in setDayOfWeekFromString. |
| 23 | */ |
| 24 | private static $weekNumberStringToInt = array('first' => 0, 'second' => 1, 'third' => 2, 'fourth' => 3); |
| 25 | |
| 26 | /** |
| 27 | * Day of the week for scheduled time. |
| 28 | * |
| 29 | * @var int |
| 30 | */ |
| 31 | private $dayOfWeek = null; |
| 32 | |
| 33 | /** |
| 34 | * Week number for scheduled time. |
| 35 | * |
| 36 | * @var int |
| 37 | */ |
| 38 | private $week = null; |
| 39 | |
| 40 | public function setDayOfWeekFromString($day) |
| 41 | { |
| 42 | @list($weekNumberString, $dayNumberString) = explode(' ', $day); |
| 43 | |
| 44 | // get day number |
| 45 | $day = Weekly::getDayIntFromString($dayNumberString) % 7; |
| 46 | |
| 47 | // get week number |
| 48 | $weekNumberString = strtolower($weekNumberString); |
| 49 | if (isset(self::$weekNumberStringToInt[$weekNumberString])) { |
| 50 | $week = self::$weekNumberStringToInt[$weekNumberString]; |
| 51 | } else { |
| 52 | throw new Exception("Invalid week describer in Schedule\\Monthly::setDayOfWeekFromString: '$weekNumberString'. " |
| 53 | . "Supported values are 'first', 'second', 'third', 'fourth'."); |
| 54 | } |
| 55 | |
| 56 | $this->setDayOfWeek($day, $week); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * @return int |
| 61 | */ |
| 62 | public function getRescheduledTime() |
| 63 | { |
| 64 | $currentTime = $this->getTime(); |
| 65 | |
| 66 | // Adds one month |
| 67 | $rescheduledTime = mktime(date('H', $currentTime), |
| 68 | date('i', $currentTime), |
| 69 | date('s', $currentTime), |
| 70 | date('n', $currentTime) + 1, |
| 71 | 1, |
| 72 | date('Y', $currentTime) |
| 73 | ); |
| 74 | |
| 75 | $nextMonthLength = date('t', $rescheduledTime); |
| 76 | |
| 77 | // Sets scheduled day |
| 78 | $scheduledDay = date('j', $currentTime); |
| 79 | |
| 80 | if ($this->day !== null) { |
| 81 | $scheduledDay = $this->day; |
| 82 | } |
| 83 | |
| 84 | if ($this->dayOfWeek !== null |
| 85 | && $this->week !== null |
| 86 | ) { |
| 87 | $newTime = $rescheduledTime + $this->week * 7 * 86400; |
| 88 | while (date("w", $newTime) != $this->dayOfWeek % 7) { |
| 89 | // modulus for sanity check |
| 90 | |
| 91 | $newTime += 86400; |
| 92 | } |
| 93 | $scheduledDay = ($newTime - $rescheduledTime) / 86400 + 1; |
| 94 | } |
| 95 | |
| 96 | // Caps scheduled day |
| 97 | if ($scheduledDay > $nextMonthLength) { |
| 98 | $scheduledDay = $nextMonthLength; |
| 99 | } |
| 100 | |
| 101 | // Adjusts the scheduled day |
| 102 | $rescheduledTime += ($scheduledDay - 1) * 86400; |
| 103 | |
| 104 | // Adjusts the scheduled hour |
| 105 | $rescheduledTime = $this->adjustHour($rescheduledTime); |
| 106 | $rescheduledTime = $this->adjustTimezone($rescheduledTime); |
| 107 | |
| 108 | return $rescheduledTime; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * @param int $_day the day to set, has to be >= 1 and < 32 |
| 113 | * @throws Exception if parameter _day is invalid |
| 114 | */ |
| 115 | public function setDay($_day) |
| 116 | { |
| 117 | if (!($_day >= 1 && $_day < 32)) { |
| 118 | throw new Exception("Invalid day parameter, must be >=1 and < 32"); |
| 119 | } |
| 120 | |
| 121 | $this->day = $_day; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Makes this scheduled time execute on a particular day of the week on each month. |
| 126 | * |
| 127 | * @param int $_day the day of the week to use, between 0-6 (inclusive). 0 -> Sunday |
| 128 | * @param int $_week the week to use, between 0-3 (inclusive) |
| 129 | * @throws Exception if either parameter is invalid |
| 130 | */ |
| 131 | public function setDayOfWeek($_day, $_week) |
| 132 | { |
| 133 | if (!($_day >= 0 && $_day < 7)) { |
| 134 | throw new Exception("Invalid day of week parameter, must be >= 0 & < 7"); |
| 135 | } |
| 136 | |
| 137 | if (!($_week >= 0 && $_week < 4)) { |
| 138 | throw new Exception("Invalid week number, must be >= 1 & < 4"); |
| 139 | } |
| 140 | |
| 141 | $this->dayOfWeek = $_day; |
| 142 | $this->week = $_week; |
| 143 | } |
| 144 | } |
| 145 |