ProxyFactory   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
dl 0
loc 37
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A fromThis() 0 7 1
A __construct() 0 8 1
A createFor() 0 12 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stratadox\Hydration\Proxying;
6
7
use Stratadox\Hydration\Hydrates;
8
use Stratadox\Hydration\ProducesOwnerUpdaters;
9
use Stratadox\Hydration\ProducesProxies;
10
use Stratadox\Hydration\ProducesProxyLoaders;
11
use Stratadox\Hydration\Proxy;
12
13
/**
14
 * Instantiates proxy objects, providing them with a loader.
15
 *
16
 * @author Stratadox
17
 * @package Stratadox/Hydrate
18
 */
19
class ProxyFactory implements ProducesProxies
20
{
21
    private $makeProxy;
22
    private $loaderFactory;
23
    private $updaterFactory;
24
25
    private function __construct(
26
        Hydrates $proxies,
27
        ProducesProxyLoaders $loaderFactory,
28
        ProducesOwnerUpdaters $updaterFactory
29
    ) {
30
        $this->makeProxy = $proxies;
31
        $this->loaderFactory = $loaderFactory;
32
        $this->updaterFactory = $updaterFactory;
33
    }
34
35
    public static function fromThis(
36
        Hydrates $proxies,
37
        ProducesProxyLoaders $loaderFactory,
38
        ProducesOwnerUpdaters $updaterFactory
39
    ) : ProxyFactory
40
    {
41
        return new static($proxies, $loaderFactory, $updaterFactory);
42
    }
43
44
    public function createFor(
45
        $theOwner,
46
        string $ofTheProperty,
47
        $atPosition = null
48
    ) : Proxy
49
    {
50
        $loader = $this->loaderFactory->makeLoaderFor($theOwner, $ofTheProperty, $atPosition);
51
        $loader->attach(
52
            $this->updaterFactory->makeUpdaterFor($theOwner, $ofTheProperty, $atPosition)
53
        );
54
        return $this->makeProxy->fromArray([
55
            'loader' => $loader
56
        ]);
57
    }
58
}
59