Passed
Push — master ( 4b5a85...fa5906 )
by Rustam
02:30
created

ProxyManager::generateProxyClassConfig()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 11
nc 8
nop 2
dl 0
loc 22
ccs 12
cts 12
cp 1
crap 5
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Proxy;
6
7
use Yiisoft\Proxy\Config\ClassConfig;
8
9
final class ProxyManager
10
{
11
    private ?string $cachePath;
12
13
    private ClassRenderer $classRenderer;
14
15
    private ClassConfigFactory $classConfigFactory;
16
17
    private ClassCache $classCache;
18
19 8
    public function __construct(string $cachePath = null)
20
    {
21 8
        $this->cachePath = $cachePath;
22 8
        $this->classCache = new ClassCache($cachePath);
23 8
        $this->classRenderer = new ClassRenderer();
24 8
        $this->classConfigFactory = new ClassConfigFactory();
25
    }
26
27 8
    public function createObjectProxy(
28
        string $baseStructure,
29
        string $parentProxyClass,
30
        array $constructorArguments
31
    ): ?object {
32 8
        $className = $baseStructure . 'Proxy';
33 8
        $shortClassName = $this->getProxyClassName($className);
34
35 8
        if (class_exists($shortClassName)) {
36 5
            return new $shortClassName(...$constructorArguments);
37
        }
38
39 3
        if (!($classDeclaration = $this->classCache->get($className, $parentProxyClass))) {
40 3
            $classConfig = $this->classConfigFactory->getClassConfig($baseStructure);
41 3
            $classConfig = $this->generateProxyClassConfig($classConfig, $parentProxyClass);
42 3
            $classDeclaration = $this->classRenderer->render($classConfig);
43 3
            $this->classCache->set($className, $parentProxyClass, $classDeclaration);
44
        }
45 3
        if ($this->cachePath === null) {
46 1
            eval(str_replace('<?php', '', $classDeclaration));
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
47
        } else {
48 2
            $path = $this->classCache->getClassPath($className, $parentProxyClass);
49 2
            require $path;
50
        }
51 3
        return new $shortClassName(...$constructorArguments);
52
    }
53
54 3
    private function generateProxyClassConfig(
55
        ClassConfig $classConfig,
56
        string $parentProxyClass
57
    ): ClassConfig {
58 3
        if ($classConfig->isInterface) {
59 2
            $classConfig->isInterface = false;
60 2
            $classConfig->interfaces = [$classConfig->name];
61
        }
62
63 3
        $classConfig->parent = $parentProxyClass;
64 3
        $classConfig->name .= 'Proxy';
65 3
        $classConfig->shortName = $this->getProxyClassName($classConfig->name);
66
67 3
        foreach ($classConfig->methods as $methodIndex => $method) {
68 3
            foreach ($method->modifiers as $index => $modifier) {
69 3
                if ($modifier === 'abstract') {
70 2
                    unset($classConfig->methods[$methodIndex]->modifiers[$index]);
71
                }
72
            }
73
        }
74
75 3
        return $classConfig;
76
    }
77
78 8
    private function getProxyClassName(string $fullClassName): string
79
    {
80 8
        return str_replace('\\', '_', $fullClassName);
81
    }
82
}
83