PushEvent   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 56
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A buildSound() 0 7 2
A __construct() 0 8 1
A buildMessage() 0 3 1
A buildPayload() 0 7 2
A buildUrl() 0 3 1
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