Validator::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
namespace CSlant\TelegramGitNotifier\Objects;
4
5
use CSlant\TelegramGitNotifier\Models\Event;
6
use CSlant\TelegramGitNotifier\Models\Setting;
7
use CSlant\TelegramGitNotifier\Trait\ActionEventTrait;
8
9
class Validator
10
{
11
    use ActionEventTrait;
0 ignored issues
show
introduced by
The trait CSlant\TelegramGitNotifier\Trait\ActionEventTrait requires some properties which are not provided by CSlant\TelegramGitNotifier\Objects\Validator: $action, $object_attributes, $noteable_type
Loading history...
12
13
    private Setting $setting;
14
15
    private Event $event;
16
17
    public function __construct(Setting $setting, Event $event)
18
    {
19
        $this->setting = $setting;
20
        $this->event = $event;
21
    }
22
23
    /**
24
     * Validate is access event before send notify
25
     *
26
     * @param string $platform Source code platform (GitHub, GitLab)
27
     * @param string $event Event name (push, pull_request)
28
     * @param object $payload
29
     *
30
     * @return bool
31
     */
32
    public function isAccessEvent(
33
        string $platform,
34
        string $event,
35
        object $payload
36
    ): bool {
37
        if (!$this->setting->isNotified()) {
38
            return false;
39
        }
40
41
        if ($this->setting->isAllEventsNotification()) {
42
            return true;
43
        }
44
        $this->event->setEventConfig($platform);
45
46
        $eventConfig = $this->event->getEventConfig()[tgn_convert_event_name($event)] ?? false;
47
        $action = $this->getActionOfEvent($payload);
48
49
        if (!empty($action) && isset($eventConfig[$action])) {
50
            $eventConfig = $eventConfig[$action];
51
        }
52
53
        if (is_array($eventConfig) || !$eventConfig) {
54
            $eventConfig = false;
55
            error_log('\n Event config is not found \n');
56
        }
57
58
        return $eventConfig;
59
    }
60
}
61