PushEvent::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 2
dl 0
loc 8
ccs 7
cts 7
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace ekinhbayar\GitAmp\Event\GitHub;
4
5
use ekinhbayar\GitAmp\Event\BaseEvent;
6
use ekinhbayar\GitAmp\Presentation\Information;
7
use ekinhbayar\GitAmp\Presentation\Ring;
8
use ekinhbayar\GitAmp\Presentation\Sound\BaseSound;
9
use ekinhbayar\GitAmp\Presentation\Sound\Celesta;
10
use ekinhbayar\GitAmp\Presentation\Sound\CelestaEgg;
11
use ekinhbayar\GitAmp\Presentation\Type;
12
13
class PushEvent extends BaseEvent
14
{
15
    /**
16
     * @param array<string,mixed> $event
17
     * @param array<string> $specialRepositories
18
     */
19 4
    public function __construct(array $event, array $specialRepositories)
20
    {
21 4
        parent::__construct(
22 4
            (int) $event['id'],
23 4
            new Type(Type::PUSH_TO_REPOSITORY),
24 4
            new Information($this->buildUrl($event), $this->buildPayload($event), $this->buildMessage($event)),
25 4
            new Ring(3000, 80),
26 4
            $this->buildSound($event, $specialRepositories),
27
        );
28 4
    }
29
30
    /**
31
     * @param array<string,mixed> $event
32
     */
33 4
    private function buildUrl(array $event): string
34
    {
35 4
        return 'https://github.com/' . $event['repo']['name'];
36
    }
37
38
    /**
39
     * @param array<string,mixed> $event
40
     */
41 4
    private function buildPayload(array $event): string
42
    {
43 4
        if (isset($event['payload']['commits'][0]['message'])) {
44 2
            return $event['payload']['commits'][0]['message'];
45
        }
46
47 2
        return 'https://github.com/' . $event['actor']['login'];
48
    }
49
50
    /**
51
     * @param array<string,mixed> $event
52
     */
53 4
    private function buildMessage(array $event): string
54
    {
55 4
        return \sprintf('%s pushed to %s', $event['actor']['login'], $event['repo']['name']);
56
    }
57
58
    /**
59
     * @param array<string,mixed> $event
60
     * @param array<string> $specialRepositories
61
     */
62 4
    private function buildSound(array $event, array $specialRepositories): BaseSound
63
    {
64 4
        if (in_array($event['repo']['name'], $specialRepositories, true)) {
65 2
            return new CelestaEgg();
66
        }
67
68 2
        return new Celesta(\strlen($this->buildPayload($event)) * 1.1);
69
    }
70
}
71