EventObserver   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 46
ccs 8
cts 8
cp 1
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A update() 0 6 3
A shouldProcess() 0 3 1
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
}