Subversion-Projekte lars-tiefland.laravel_shop

Revision

Zur aktuellen Revision | Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/*
4
 * This file is part of Psy Shell.
5
 *
6
 * (c) 2012-2022 Justin Hileman
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
 
12
namespace Psy\VersionUpdater;
13
 
14
class IntervalChecker extends GitHubChecker
15
{
16
    private $cacheFile;
17
    private $interval;
18
 
19
    public function __construct($cacheFile, $interval)
20
    {
21
        $this->cacheFile = $cacheFile;
22
        $this->interval = $interval;
23
    }
24
 
25
    public function fetchLatestRelease()
26
    {
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;
34
            }
35
        }
36
 
37
        // Fall back to fetching from GitHub
38
        $release = parent::fetchLatestRelease();
39
        if ($release && isset($release->tag_name)) {
40
            $this->updateCache($release);
41
        }
42
 
43
        return $release;
44
    }
45
 
46
    /**
47
     * @throws \RuntimeException if interval passed to constructor is not supported
48
     */
49
    private function getDateInterval(): \DateInterval
50
    {
51
        switch ($this->interval) {
52
            case Checker::DAILY:
53
                return new \DateInterval('P1D');
54
            case Checker::WEEKLY:
55
                return new \DateInterval('P1W');
56
            case Checker::MONTHLY:
57
                return new \DateInterval('P1M');
58
        }
59
 
60
        throw new \RuntimeException('Invalid interval configured');
61
    }
62
 
63
    private function updateCache($release)
64
    {
65
        $data = [
66
            'last_check' => \date(\DATE_ATOM),
67
            'release'    => $release,
68
        ];
69
 
70
        \file_put_contents($this->cacheFile, \json_encode($data));
71
    }
72
}