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;
14 class IntervalChecker extends GitHubChecker
19 public function __construct($cacheFile, $interval)
21 $this->cacheFile = $cacheFile;
22 $this->interval = $interval;
25 public function fetchLatestRelease()
27 // Read the cached file
28 $cached = \json_decode(@\file_get_contents($this->cacheFile, false));
29 if ($cached && isset($cached->last_check) && isset($cached->release)) {
30 $now = new \DateTime();
31 $lastCheck = new \DateTime($cached->last_check);
32 if ($lastCheck >= $now->sub($this->getDateInterval())) {
33 return $cached->release;
37 // Fall back to fetching from GitHub
38 $release = parent::fetchLatestRelease();
39 if ($release && isset($release->tag_name)) {
40 $this->updateCache($release);
46 private function getDateInterval()
48 switch ($this->interval) {
50 return new \DateInterval('P1D');
52 return new \DateInterval('P1W');
53 case Checker::MONTHLY:
54 return new \DateInterval('P1M');
58 private function updateCache($release)
61 'last_check' => \date(DATE_ATOM),
62 'release' => $release,
65 \file_put_contents($this->cacheFile, \json_encode($data));