1: <?php
2:
3: namespace malkusch\bav;
4:
5: /**
6: * Abstract update plan.
7: *
8: * @author Markus Malkusch <markus@malkusch.de>
9: * @link bitcoin:1335STSwu9hST4vcMRppEPgENMHD2r1REK Donations
10: * @license GPL
11: * @api
12: */
13: abstract class UpdatePlan
14: {
15:
16: /**
17: * Bundesbank releases updates in those months
18: */
19: private static $updateMonths = array(
20: 3, // March
21: 6, // June
22: 9, // September
23: 12, // December
24: );
25:
26: /**
27: * Number of months before an update month when Bundesbank is supposed
28: * to deliver the updated file.
29: *
30: * @var int
31: */
32: private static $relaseThreshold = 1;
33:
34: /**
35: * Perform the update process.
36: */
37: abstract public function perform(DataBackend $backend);
38:
39: /**
40: * Returns true if the data is to old and needs an update
41: *
42: * @see DataBackend::getLastUpdate()
43: * @return bool
44: */
45: public function isOutdated(DataBackend $backend)
46: {
47: /*
48: * The following code creates a sorted list with the release months (update month - $relaseThreshold)
49: * and the current month. To build that threshold date simply pick the month before the current month from
50: * that list.
51: *
52: * Note that the second parameter of the date() calls is there on purpose. This allows
53: * to mock time() for testing.
54: */
55:
56: /*
57: * The current month gets an increment of 0.5 for the case that the current month is a
58: * release month (e.g. the list will look (2, 2.5, 5, 8, 11)).
59: */
60: $currentMonth = date("n", time()) + 0.5;
61:
62: $monthList = array($currentMonth);
63: foreach (self::$updateMonths as $month) {
64: $releaseMonth = $month - self::$relaseThreshold;
65: $monthList[] = $releaseMonth;
66:
67: }
68: sort($monthList); // You have now something like (2, 2.5, 5, 8, 11).
69:
70: // Now add the cycle between the last and the first month(11, 2, 3.5, 5, 8, 11, 2).
71: $monthList[] = self::$updateMonths[0] - self::$relaseThreshold; // this is acually not needed.
72: array_unshift($monthList, self::$updateMonths[count(self::$updateMonths) - 1] - self::$relaseThreshold);
73:
74: $index = array_search($currentMonth, $monthList);
75: assert($index > 0);
76: $previousIndex = $index - 1;
77:
78: $thresholdMonth = $monthList[$previousIndex];
79:
80: // flip the year if the threshold was in the last year.
81: $year = $thresholdMonth > $currentMonth ? date("Y", time()) - 1 : date("Y", time());
82:
83: $threshold = mktime(0, 0, 0, $thresholdMonth, 1, $year);
84:
85: return $backend->getLastUpdate() < $threshold;
86: }
87: }
88: