Passed
Push — dev ( bf3609...863855 )
by Dispositif
02:57
created

GoogleApiQuota::setZero()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 7
rs 10
1
<?php
2
/**
3
 * This file is part of dispositif/wikibot application
4
 * 2019 © Philippe M. <[email protected]>
5
 * For the full copyright and MIT license information, please view the LICENSE file.
6
 */
7
8
declare(strict_types=1);
9
10
namespace App\Infrastructure;
11
12
use App\Domain\Exceptions\ConfigException;
13
use DateTime;
14
use DateTimeZone;
15
use Exception;
16
use Throwable;
17
18
/**
19
 * Count and increment, data saved in json file.
20
 * Set count to 0 everyday at 00:00 (America/Los_Angeles).
21
 * No need of SQL/singleton with the single file.
22
 * Class GoogleRequestQuota
23
 *
24
 * @package App\Infrastructure
25
 */
26
class GoogleApiQuota
27
{
28
    /** {"date":"2020-03-23T00:19:56-07:00","count":43}  */
29
    const FILENAME        = __DIR__.'/resources/google_quota.json';
30
    const REBOOT_TIMEZONE = 'America/Los_Angeles';
31
    const REBOOT_HOUR     = 0;
32
33
    /**
34
     * @var DateTime
35
     */
36
    private $lastDate;
37
    /**
38
     * @var int
39
     */
40
    private $count = 0;
41
    /**
42
     * @var DateTime Today reboot date/time of the quota
43
     */
44
    private $todayBoot;
45
46
    /**
47
     * GoogleRequestQuota constructor.
48
     *
49
     * @throws Exception
50
     */
51
    public function __construct()
52
    {
53
        $data = $this->getFileData();
54
        $this->lastDate = new DateTime($data['date'], new DateTimeZone(static::REBOOT_TIMEZONE));
55
        $this->count = (int)$data['count'];
56
57
        // Today reboot date/time of the quota
58
        $todayBoot = new DateTime();
59
        $todayBoot->setTimezone(new DateTimeZone('America/Los_Angeles'))->setTime(static::REBOOT_HOUR, 0);
60
        $this->todayBoot = $todayBoot;
61
62
        $this->checkNewReboot();
63
    }
64
65
    /**
66
     * @return array
67
     * @throws ConfigException
68
     */
69
    private function getFileData(): array
70
    {
71
        if (!file_exists(static::FILENAME)) {
72
            return ['date' => '2020-01-01T00:00:20-07:00', 'count' => 0];
73
        }
74
75
        try {
76
            $json = file_get_contents(static::FILENAME);
77
            $array = json_decode($json, true);
78
        } catch (Throwable $e) {
79
            throw new ConfigException('file malformed.');
80
        }
81
82
        return $array;
83
    }
84
85
    private function checkNewReboot(): void
86
    {
87
        $now = new DateTime();
88
        $now->setTimezone(new DateTimeZone(static::REBOOT_TIMEZONE));
89
90
        if ($now->diff($this->lastDate, true)->format('%h') > 24) {
91
            $this->setZero();
92
93
            return;
94
        }
95
        if ($this->lastDate < $this->todayBoot && $now > $this->todayBoot) {
96
            $this->setZero();
97
        }
98
    }
99
100
    private function setZero()
101
    {
102
        $now = new DateTime();
103
        $now->setTimezone(new DateTimeZone(static::REBOOT_TIMEZONE));
104
        $this->lastDate = $now;
105
        $this->count = 0;
106
        $this->saveDateInFile();
107
    }
108
109
    private function saveDateInFile(): void
110
    {
111
        $data = [
112
            'date' => $this->lastDate->format('c'),
113
            'count' => $this->count,
114
        ];
115
        file_put_contents(static::FILENAME, json_encode($data));
116
    }
117
118
    /**
119
     * @return int
120
     */
121
    public function getCount(): int
122
    {
123
        $this->checkNewReboot();
124
125
        return $this->count;
126
    }
127
128
    /**
129
     *
130
     */
131
    public function increment(): void
132
    {
133
        $this->checkNewReboot();
134
        $this->count = $this->count + 1;
135
        $this->saveDateInFile();
136
    }
137
}
138