Completed
Push — b0.27.0 ( c529b3...34cc26 )
by Sebastian
05:07
created

Model   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 9
eloc 12
c 1
b 1
f 0
dl 0
loc 80
ccs 20
cts 20
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 3 1
A set() 0 3 1
A attach() 0 4 2
A notify() 0 5 2
A detach() 0 4 2
A __construct() 0 3 1
1
<?php
2
3
/**
4
 * Linna Framework.
5
 *
6
 * @author Sebastian Rapetti <[email protected]>
7
 * @copyright (c) 2018, Sebastian Rapetti
8
 * @license http://opensource.org/licenses/MIT MIT License
9
 */
10
declare(strict_types=1);
11
12
namespace Linna\Mvc;
13
14
use SplObserver;
15
use SplObjectStorage;
16
use SplSubject;
17
18
/**
19
 * Parent class for model classes.
20
 *
21
 * This class was implemented like part of Observer pattern
22
 * https://en.wikipedia.org/wiki/Observer_pattern
23
 * http://php.net/manual/en/class.splsubject.php
24
 */
25
class Model implements SplSubject
26
{
27
    /**
28
     * @var SplObjectStorage List of attached observerer
29
     */
30
    private SplObjectStorage $observers;
31
32
    /**
33
     * @var array<mixed> Data for notify to observerer
34
     */
35
    private array $updates = [];
36
37
    /**
38
     * Constructor.
39
     */
40 24
    public function __construct()
41
    {
42 24
        $this->observers = new SplObjectStorage();
43 24
    }
44
45
    /**
46
     * Attach an Observer class to this Subject for updates
47
     * when occour a subject state change.
48
     *
49
     * @param SplObserver $observer
50
     *
51
     * @return void
52
     */
53 19
    public function attach(SplObserver $observer)
54
    {
55 19
        if ($observer instanceof View) {
56 19
            $this->observers->attach($observer);
57
        }
58 19
    }
59
60
    /**
61
     * Detach an Observer class from this Subject.
62
     *
63
     * @param SplObserver $observer
64
     *
65
     * @return void
66
     */
67 1
    public function detach(SplObserver $observer)
68
    {
69 1
        if ($observer instanceof View) {
70 1
            $this->observers->detach($observer);
71
        }
72 1
    }
73
74
    /**
75
     * Notify a state change of Subject to all registered Observeres.
76
     *
77
     * @return void
78
     */
79 19
    public function notify()
80
    {
81
        /** @var View $value Attached observers. */
82 19
        foreach ($this->observers as $value) {
83 19
            $value->update($this);
84
        }
85 19
    }
86
87
    /**
88
     * Set the data to notify to all registered Observeres.
89
     *
90
     * @param array<mixed> $data
91
     */
92 20
    public function set(array $data): void
93
    {
94 20
        $this->updates = \array_merge/*_recursive*/($this->updates, $data);
95 20
    }
96
97
    /**
98
     * Get the data to notify to all registered Observeres.
99
     *
100
     * @return array<mixed>
101
     */
102 20
    public function get(): array
103
    {
104 20
        return $this->updates;
105
    }
106
}
107