Event::updateEvent()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 10
c 0
b 0
f 0
rs 10
cc 2
nc 2
nop 2
1
<?php
2
3
namespace CSlant\TelegramGitNotifier\Models;
4
5
use CSlant\TelegramGitNotifier\Constants\EventConstant;
6
7
class Event
8
{
9
    private array $eventConfig = [];
10
11
    public string $platform = EventConstant::DEFAULT_PLATFORM;
12
13
    private string $platformFile = '';
14
15
    /**
16
     * @return string
17
     */
18
    public function getPlatformFile(): string
19
    {
20
        return $this->platformFile;
21
    }
22
23
    /**
24
     * @param string $platformFile
25
     *
26
     * @return void
27
     */
28
    public function setPlatformFile(string $platformFile): void
29
    {
30
        $this->platformFile = $platformFile;
31
    }
32
33
    /**
34
     * @return array
35
     */
36
    public function getEventConfig(): array
37
    {
38
        return $this->eventConfig;
39
    }
40
41
    /**
42
     * Set event config
43
     *
44
     * @param string|null $platform
45
     *
46
     * @return void
47
     */
48
    public function setEventConfig(
49
        string $platform = null
50
    ): void {
51
        $this->platform = $platform ?? EventConstant::DEFAULT_PLATFORM;
52
53
        $json = file_get_contents($this->platformFile);
54
55
        if (!empty($json)) {
56
            $this->eventConfig = json_decode($json, true);
57
        }
58
    }
59
60
    /**
61
     * Update event config by event and action
62
     *
63
     * @param string $event
64
     * @param string|null $action
65
     *
66
     * @return void
67
     */
68
    public function updateEvent(string $event, string|null $action): void
69
    {
70
        if (!empty($action)) {
71
            $this->eventConfig[$event][$action]
72
                = !$this->eventConfig[$event][$action];
73
        } else {
74
            $this->eventConfig[$event] = !$this->eventConfig[$event];
75
        }
76
77
        $this->saveEventConfig();
78
    }
79
80
    /**
81
     * Save event config
82
     *
83
     * @return void
84
     */
85
    private function saveEventConfig(): void
86
    {
87
        if (file_exists($this->platformFile)) {
88
            $json = json_encode($this->eventConfig, JSON_PRETTY_PRINT);
89
            file_put_contents($this->platformFile, $json, LOCK_EX);
90
        }
91
    }
92
}
93