Passed
Push — main ( ef2030...a17612 )
by Tan
15:25 queued 13:15
created

EventTrait::validatePlatformFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 6
rs 10
cc 2
nc 2
nop 0
1
<?php
2
3
namespace CSlant\TelegramGitNotifier\Trait;
4
5
use CSlant\TelegramGitNotifier\Constants\EventConstant;
6
use CSlant\TelegramGitNotifier\Exceptions\ConfigFileException;
7
use Symfony\Component\HttpFoundation\Request;
8
9
trait EventTrait
10
{
11
    use ActionEventTrait;
12
13
    public function setPlatFormForEvent(?string $platform = EventConstant::DEFAULT_PLATFORM, ?string $platformFile = null): void
14
    {
15
        /** @var array $platformFileDefaults<platform, platformFile> */
16
        $platformFileDefaults = config('telegram-git-notifier.data_file.platform');
17
        $this->event->setPlatformFile($platformFile ?? $platformFileDefaults[$platform]);
18
        $this->event->setEventConfig($platform);
19
    }
20
21
    public function handleEventFromRequest(Request $request): ?string
22
    {
23
        foreach (EventConstant::WEBHOOK_EVENT_HEADER as $platform => $header) {
24
            $event = $request->server->get($header);
25
            if (!is_null($event)) {
26
                $this->event->platform = $platform;
27
                $this->setPlatFormForEvent($platform);
28
29
                return $event;
30
            }
31
        }
32
33
        return null;
34
    }
35
36
    public function validatePlatformFile(): void
37
    {
38
        if (empty($this->event->getEventConfig())) {
39
            throw ConfigFileException::platformFile(
40
                $this->event->platform,
41
                $this->event->getPlatformFile()
42
            );
43
        }
44
    }
45
}
46