Passed
Push — master ( b10a5e...4ba5d0 )
by Valentin
02:10
created

Factory::createName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Cycle\ORM\Promise;
5
6
use Cycle\ORM\ORMInterface;
7
use Cycle\ORM\Promise\Declaration\Declaration;
8
use Cycle\ORM\PromiseFactoryInterface;
9
use Cycle\ORM\Schema;
10
use Spiral\Core\Container\SingletonInterface;
11
12
class Factory implements PromiseFactoryInterface, SingletonInterface
13
{
14
    /** @var ProxyPrinter */
15
    private $printer;
16
17
    /** @var MaterializerInterface */
18
    private $materializer;
19
20
    /** @var array */
21
    private $resolved = [];
22
23
    public function __construct(ProxyPrinter $printer, MaterializerInterface $materializer)
24
    {
25
        $this->printer = $printer;
26
        $this->materializer = $materializer;
27
    }
28
29
    /**
30
     * @param ORMInterface $orm
31
     * @param string       $role
32
     * @param array        $scope
33
     *
34
     * @return ReferenceInterface|null
35
     * @throws ProxyFactoryException
36
     */
37
    public function promise(ORMInterface $orm, string $role, array $scope): ?ReferenceInterface
38
    {
39
        if (isset($this->resolved[$role])) {
40
            return $this->instantiate($this->resolved[$role], $orm, $role, $scope);
41
        }
42
43
        $class = $orm->getSchema()->define($role, Schema::ENTITY);
44
        if (empty($class)) {
45
            return null;
46
        }
47
48
        try {
49
            $r = new \ReflectionClass($class);
50
        } catch (\ReflectionException $e) {
51
            throw new ProxyFactoryException($e->getMessage(), $e->getCode(), $e);
52
        }
53
54
        $declaration = new Declaration($r, $this->createName($r));
55
        $this->materializer->materialize($this->printer->make($declaration), $declaration);
56
57
        $this->resolved[$role] = $declaration->class->getFullName();
58
59
        return $this->instantiate($this->resolved[$role], $orm, $role, $scope);
60
    }
61
62
    /**
63
     * @param string                  $className
64
     * @param \Cycle\ORM\ORMInterface $orm
65
     * @param string                  $role
66
     * @param array                   $scope
67
     *
68
     * @return \Cycle\ORM\Promise\PromiseInterface
69
     */
70
    private function instantiate(string $className, ORMInterface $orm, string $role, array $scope): PromiseInterface
71
    {
72
        return new $className($orm, $role, $scope);
73
    }
74
75
    private function createName(\ReflectionClass $reflection): string
76
    {
77
        return "{$reflection->getShortName()}_{$reflection->getName()}{$reflection->getFileName()}";
0 ignored issues
show
Bug introduced by
Consider using $reflection->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
78
    }
79
}