Completed
Push — master ( 3bd4e2...776f46 )
by Jesse
04:06
created

BasicProxyFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

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

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
 * BasicProxyFactory.
11
 *
12
 * @author Stratadox
13
 */
14
final class BasicProxyFactory implements ProxyFactory
15
{
16
    private $class;
17
    private $loader;
18
19
    private function __construct(
20
        string $class,
21
        ProxyLoader $loader
22
    ) {
23
        $this->class = $class;
24
        $this->loader = $loader;
25
        if (!is_a($class, Proxy::class, true)) {
26
            throw new InvalidArgumentException(sprintf(
27
                'Cannot use non-proxy class `%s` as proxy.',
28
                $class
29
            ));
30
        }
31
    }
32
33
    public static function for(string $class, ProxyLoader $loader): ProxyFactory
34
    {
35
        return new self($class, $loader);
36
    }
37
38
    public function create(array $knownData = []): Proxy
39
    {
40
        return new $this->class($this->loader, $knownData);
41
    }
42
}
43