Overrider   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 19
c 1
b 0
f 1
dl 0
loc 60
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 3
A unwatch() 0 7 2
1
<?php
2
/**
3
 * Overrider of multi-key watching
4
 * User: moyo
5
 * Date: 2018/8/20
6
 * Time: 5:27 PM
7
 */
8
9
namespace Carno\Config\Features;
10
11
use Carno\Config\Config;
12
use Closure;
13
14
class Overrider
15
{
16
    /**
17
     * @var array
18
     */
19
    private $ordered = [];
20
21
    /**
22
     * @var array
23
     */
24
    private $watches = [];
25
26
    /**
27
     * @var int
28
     */
29
    private $priority = 0;
30
31
    /**
32
     * @var Closure
33
     */
34
    private $observer = null;
35
36
    /**
37
     * @var Config
38
     */
39
    private $source = null;
40
41
    /**
42
     * Overrider constructor.
43
     * @param Config $source
44
     * @param Closure $observer
45
     * @param array $keys
46
     */
47
    public function __construct(Config $source, Closure $observer, array $keys)
48
    {
49
        $this->source = $source;
50
51
        $this->observer = function ($value, string $key) use ($observer) {
52
            $priority = $this->ordered[$key];
53
            if ($priority >= $this->priority) {
54
                $this->priority = $priority;
55
                $observer($value, $key);
56
            }
57
        };
58
59
        foreach ($keys as $priority => $key) {
60
            $this->ordered[$key] = $priority;
61
            $this->watches[$priority] = $this->source->watching($key, $this->observer);
62
        }
63
    }
64
65
    /**
66
     */
67
    public function unwatch() : void
68
    {
69
        foreach ($this->watches as $wid) {
70
            $this->source->unwatch($wid);
71
        }
72
        unset($this->source);
73
        unset($this->observer);
74
    }
75
}
76