Completed
Push — master ( 3be072...ba2d3a )
by Marco
231:32 queued 209:55
created

AbstractBaseFactory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 8
dl 0
loc 87
ccs 31
cts 31
cp 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ProxyManager\Factory;
6
7
use OutOfBoundsException;
8
use ProxyManager\Configuration;
9
use ProxyManager\Generator\ClassGenerator;
10
use ProxyManager\ProxyGenerator\ProxyGeneratorInterface;
11
use ProxyManager\Signature\Exception\InvalidSignatureException;
12
use ProxyManager\Signature\Exception\MissingSignatureException;
13
use ProxyManager\Version;
14
use ReflectionClass;
15
use function array_key_exists;
16
use function class_exists;
17
18
/**
19
 * Base factory common logic
20
 */
21
abstract class AbstractBaseFactory
22
{
23
    protected Configuration $configuration;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
24
25
    /**
26
     * Cached checked class names
27
     *
28
     * @var string[]
29
     */
30
    private array $checkedClasses = [];
31
32
    public function __construct(?Configuration $configuration = null)
33
    {
34 15
        $this->configuration = $configuration ?: new Configuration();
35
    }
36 15
37 15
    /**
38
     * Generate a proxy from a class name
39
     *
40
     * @param mixed[] $proxyOptions
41
     *
42
     * @throws InvalidSignatureException
43
     * @throws MissingSignatureException
44
     * @throws OutOfBoundsException
45
     */
46
    protected function generateProxy(string $className, array $proxyOptions = []) : string
47
    {
48 1
        if (array_key_exists($className, $this->checkedClasses)) {
49
            return $this->checkedClasses[$className];
50 1
        }
51 1
52
        $proxyParameters = [
53
            'className'           => $className,
54
            'factory'             => static::class,
55 1
            'proxyManagerVersion' => Version::getVersion(),
56 1
        ];
57 1
        $proxyClassName  = $this
58
            ->configuration
59
            ->getClassNameInflector()
60 1
            ->getProxyClassName($className, $proxyParameters);
61 1
62 1
        if (! class_exists($proxyClassName)) {
63
            $this->generateProxyClass(
64 1
                $proxyClassName,
65 1
                $className,
66 1
                $proxyParameters,
67 1
                $proxyOptions
68 1
            );
69 1
        }
70
71
        $this
72
            ->configuration
73
            ->getSignatureChecker()
74 1
            ->checkSignature(new ReflectionClass($proxyClassName), $proxyParameters);
75 1
76 1
        return $this->checkedClasses[$className] = $proxyClassName;
77
    }
78 1
79
    abstract protected function getGenerator() : ProxyGeneratorInterface;
80
81
    /**
82
     * Generates the provided `$proxyClassName` from the given `$className` and `$proxyParameters`
83
     *
84
     * @param string[] $proxyParameters
85
     * @param mixed[]  $proxyOptions
86
     */
87
    private function generateProxyClass(
88
        string $proxyClassName,
89 1
        string $className,
90
        array $proxyParameters,
91
        array $proxyOptions = []
92
    ) : void {
93
        $className = $this->configuration->getClassNameInflector()->getUserClassName($className);
94
        $phpClass  = new ClassGenerator($proxyClassName);
95 1
96 1
        $this->getGenerator()->generate(new ReflectionClass($className), $phpClass, $proxyOptions);
97
98 1
        $phpClass = $this->configuration->getClassSignatureGenerator()->addSignature($phpClass, $proxyParameters);
99
100 1
        $this->configuration->getGeneratorStrategy()->generate($phpClass, $proxyOptions);
101
102 1
        $autoloader = $this->configuration->getProxyAutoloader();
103
104 1
        $autoloader($proxyClassName);
105
    }
106
}
107