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