EventObserver::update()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 1
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Fatindeed\GitlabWebhookHandler;
4
5
use SplSubject, SplObserver;
6
use TimoReymann\GitlabWebhookLibrary\Specification\Event;
7
8
/**
9
 * @see https://www.php.net/manual/zh/class.splobserver.php
10
 */
11
abstract class EventObserver implements SplObserver
12
{
13
    /**
14
     * Event type to be handled
15
     * 
16
     * @var array
17
     */
18
    protected $eventTypes = [];
19
20
    /**
21
     * @var \TimoReymann\GitlabWebhookLibrary\Specification\Event
22
     */
23
    protected $event;
24
25
    /**
26
     * Receive update from subject
27
     * 
28
     * @param  \SplSubject $subject
29
     * @return void
30
     */
31 2
    public function update(SplSubject $subject): void
32
    {
33 2
        if ($subject instanceof EventSubject) {
34 2
            $this->event = $subject->getEvent();
35 2
            if ($this->shouldProcess()) {
36 2
                $this->process();
37
            }
38
        }
39 2
    }
40
41
    /**
42
     * Should process webhook event
43
     *
44
     * @return bool
45
     */
46 2
    protected function shouldProcess(): bool
47
    {
48 2
        return in_array(get_class($this->event), $this->eventTypes);
49
    }
50
51
    /**
52
     * Process webhook event
53
     * 
54
     * @return void
55
     */
56
    abstract protected function process(): void;
57
}