BasicProxyFactory   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 11
c 1
b 0
f 0
dl 0
loc 35
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A create() 0 3 1
A for() 0 3 1
1
<?php declare(strict_types=1);
2
3
namespace Stratadox\Proxy;
4
5
use InvalidArgumentException;
6
use function is_a;
7
use function sprintf;
8
9
/**
10
 * Basic Proxy Factory; Produces proxies, feeding them with whatever data we
11
 * have on the object they represent.
12
 *
13
 * @author Stratadox
14
 */
15
final class BasicProxyFactory implements ProxyFactory
16
{
17
    private $class;
18
    private $loader;
19
20
    private function __construct(
21
        string $class,
22
        ProxyLoader $loader
23
    ) {
24
        $this->class = $class;
25
        $this->loader = $loader;
26
        if (!is_a($class, Proxy::class, true)) {
27
            throw new InvalidArgumentException(sprintf(
28
                'Cannot use non-proxy class `%s` as proxy.',
29
                $class
30
            ));
31
        }
32
    }
33
34
    /**
35
     * Makes a proxy factory for a particular proxy class.
36
     *
37
     * @param string      $class  The proxy class to instantiate.
38
     * @param ProxyLoader $loader The loader to inject into the proxy.
39
     * @return ProxyFactory       The factory for this proxy class.
40
     */
41
    public static function for(string $class, ProxyLoader $loader): ProxyFactory
42
    {
43
        return new self($class, $loader);
44
    }
45
46
    /** @inheritdoc */
47
    public function create(array $knownData = []): Proxy
48
    {
49
        return new $this->class($this->loader, $knownData);
50
    }
51
}
52