Passed
Push — dev ( 043eb4...bf3609 )
by Dispositif
06:23
created

GoogleQuota::checkNewDay()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 7
nc 3
nop 0
dl 0
loc 12
rs 10
c 1
b 0
f 0
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 Throwable;
14
15
/**
16
 * Count and increment, data saved in json file.
17
 * Set count to 0 everyday at 08:00 (America/Los_Angeles)
18
 * {"date":"2020-03-23T00:19:56-07:00","count":43}
19
 * Class GoogleQuota
20
 *
21
 * @package App\Infrastructure
22
 */
23
class GoogleQuota
24
{
25
    const FILENAME        = __DIR__.'/resources/googlequota.json';
26
    const REBOOT_TIMEZONE = 'America/Los_Angeles';
27
28
    /**
29
     * @var \DateTime
30
     */
31
    private $lastDate;
32
    /**
33
     * @var int
34
     */
35
    private $count = 0;
36
    /**
37
     * @var \DateTime
38
     */
39
    private $todayBoot;
40
41
    /**
42
     * GoogleQuota constructor.
43
     *
44
     * @throws \Exception
45
     */
46
    public function __construct()
47
    {
48
        $data = $this->getFileData();
49
        $this->lastDate = new \DateTime($data['date'], new \DateTimeZone(self::REBOOT_TIMEZONE));
50
        $this->count = (int) $data['count'];
51
52
        // Today reboot date/time of the quota
53
        $todayBoot = new \DateTime();
54
        $todayBoot->setTimezone(new \DateTimeZone('America/Los_Angeles'))->setTime(8, 0);
55
        $this->todayBoot = $todayBoot;
56
57
        $this->checkNewDay();
58
    }
59
60
    /**
61
     * @return int
62
     */
63
    public function getCount(): int
64
    {
65
        $this->checkNewDay();
66
67
        return $this->count;
68
    }
69
70
    /**
71
     *
72
     */
73
    public function increment(): void
74
    {
75
        $this->checkNewDay();
76
        $this->count = $this->count + 1;
77
        $this->saveFile();
78
    }
79
80
    private function checkNewDay(): void
81
    {
82
        $now = new \DateTime();
83
        $now->setTimezone(new \DateTimeZone(self::REBOOT_TIMEZONE));
84
85
        if ($now->diff($this->lastDate, true)->format('%h') > 24) {
86
            $this->setZero();
87
88
            return;
89
        }
90
        if ($this->lastDate < $this->todayBoot && $now > $this->todayBoot) {
91
            $this->setZero();
92
        }
93
    }
94
95
    private function setZero()
96
    {
97
        $now = new \DateTime();
98
        $now->setTimezone(new \DateTimeZone(self::REBOOT_TIMEZONE));
99
        $this->lastDate = $now;
100
        $this->count = 0;
101
        $this->saveFile();
102
    }
103
104
    private function saveFile(): void
105
    {
106
        $data = [
107
            'date' => $this->lastDate->format('c'),
108
            'count' => $this->count,
109
        ];
110
        file_put_contents(static::FILENAME, json_encode($data));
111
    }
112
113
    /**
114
     * @return array
115
     * @throws ConfigException
116
     */
117
    private function getFileData(): array
118
    {
119
        if (!file_exists(static::FILENAME)) {
120
            // todo create file ?
121
            throw new ConfigException('No GoogleQuota file found.');
122
        }
123
124
        try {
125
            $json = file_get_contents(self::FILENAME);
126
            $array = json_decode($json, true);
127
        } catch (Throwable $e) {
128
            throw new ConfigException('file malformed.');
129
        }
130
131
        return $array;
132
    }
133
}
134