4 * This file is part of Psy Shell.
6 * (c) 2012-2018 Justin Hileman
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Psy\VersionUpdater;
16 class IntervalChecker extends GitHubChecker
21 public function __construct($cacheFile, $interval)
23 $this->cacheFile = $cacheFile;
24 $this->interval = $interval;
27 public function fetchLatestRelease()
29 // Read the cached file
30 $cached = json_decode(@file_get_contents($this->cacheFile, false));
31 if ($cached && isset($cached->last_check) && isset($cached->release)) {
32 $now = new \DateTime();
33 $lastCheck = new \DateTime($cached->last_check);
34 if ($lastCheck >= $now->sub($this->getDateInterval())) {
35 return $cached->release;
39 // Fall back to fetching from GitHub
40 $release = parent::fetchLatestRelease();
41 if ($release && isset($release->tag_name)) {
42 $this->updateCache($release);
48 private function getDateInterval()
50 switch ($this->interval) {
52 return new \DateInterval('P1D');
54 return new \DateInterval('P1W');
55 case Checker::MONTHLY:
56 return new \DateInterval('P1M');
60 private function updateCache($release)
63 'last_check' => date(DATE_ATOM),
64 'release' => $release,
67 file_put_contents($this->cacheFile, json_encode($data));