Loader::loadTheInstance()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stratadox\Hydration\Proxying;
6
7
use Stratadox\Hydration\LoadsProxiedObjects;
8
use Stratadox\Hydration\ObservesProxyLoading;
9
10
abstract class Loader implements LoadsProxiedObjects
11
{
12
    /** @var ObservesProxyLoading[] */
13
    private $observers = [];
14
15
    /** @var mixed|object */
16
    private $forWhom;
17
18
    /** @var string */
19
    private $property;
20
21
    /** @var string|null */
22
    private $position;
23
24
    public function __construct($forWhom, string $property, $position = null)
25
    {
26
        $this->forWhom = $forWhom;
27
        $this->property = $property;
28
        $this->position = $position;
29
    }
30
31
    public function attach(ObservesProxyLoading $observer) : void
32
    {
33
        $this->observers[] = $observer;
34
    }
35
36
    public function detach(ObservesProxyLoading $observer) : void
37
    {
38
        unset($this->observers[array_search($observer, $this->observers, true)]);
39
    }
40
41
    final public function loadTheInstance()
42
    {
43
        $instance = $this->doLoad($this->forWhom, $this->property, $this->position);
44
        $this->tellThemWeMadeThis($instance);
45
        return $instance;
46
    }
47
48
    protected function tellThemWeMadeThis($instance)
49
    {
50
        foreach ($this->observers as $observer) {
51
            $observer->updateWith($instance);
52
        }
53
    }
54
55
    protected abstract function doLoad($forWhom, string $property, $position = null);
56
}
57