Completed
Push — master ( 06a7b2...aedaa6 )
by Valentin
03:01
created

Factory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 8
dl 0
loc 38
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A promise() 0 14 2
A instantiate() 0 4 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\Promise\Naming\DatetimeNaming;
9
use Cycle\ORM\PromiseFactoryInterface;
10
use Cycle\ORM\Schema;
11
12
class Factory implements PromiseFactoryInterface
13
{
14
    /** @var ProxyPrinter */
15
    private $creator;
16
17
    /** @var MaterializerInterface */
18
    private $materializer;
19
20
    /** @var NamingInterface */
21
    private $naming;
22
23
    public function __construct(ProxyPrinter $creator, MaterializerInterface $materializer, ?NamingInterface $naming)
24
    {
25
        $this->creator = $creator;
26
        $this->materializer = $materializer;
27
        $this->naming = $naming ?? new DatetimeNaming();
28
    }
29
30
    public function promise(ORMInterface $orm, string $role, array $scope): ?ReferenceInterface
31
    {
32
        $class = $orm->getSchema()->define($role, Schema::ENTITY);
33
        if (empty($class)) {
34
            return null;
35
        }
36
37
        $declaration = new Declaration($class, $this->naming->name($class));
38
        $output = $this->creator->make($declaration);
39
40
        $this->materializer->materialize($output, $declaration);
41
42
        return $this->instantiate($orm, $role, $scope, $declaration->class->getNamespacesName());
43
    }
44
45
    private function instantiate(ORMInterface $orm, string $role, array $scope, string $className): PromiseInterface
46
    {
47
        return new $className($orm, $role, $scope);
48
    }
49
}