Passed
Push — master ( 6ea451...1ef48a )
by Anton
01:35
created

Factory::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
namespace Cycle\ORM;
11
12
use Cycle\ORM\Config\RelationConfig;
13
use Cycle\ORM\Mapper\Mapper;
14
use Cycle\ORM\Relation\RelationInterface;
15
use Cycle\ORM\Select\LoaderInterface;
16
use Psr\Container\ContainerInterface;
17
use Spiral\Core\Container;
18
use Spiral\Core\FactoryInterface as CoreFactory;
19
use Spiral\Database\DatabaseInterface;
20
use Spiral\Database\DatabaseManager;
21
22
class Factory implements FactoryInterface
23
{
24
    /** @var RelationConfig */
25
    private $config;
26
27
    /** @var CoreFactory */
28
    private $factory;
29
30
    /** @var ContainerInterface */
31
    private $container;
32
33
    /** @var DatabaseManager */
34
    private $dbal;
35
36
    /**
37
     * @param DatabaseManager         $dbal
38
     * @param RelationConfig          $config
39
     * @param CoreFactory|null        $factory
40
     * @param ContainerInterface|null $container
41
     */
42
    public function __construct(
43
        DatabaseManager $dbal,
44
        RelationConfig $config,
45
        CoreFactory $factory = null,
46
        ContainerInterface $container = null
47
    ) {
48
        $this->dbal = $dbal;
49
        $this->config = $config;
50
        $this->factory = $factory ?? new Container();
51
        $this->container = $container ?? new Container();
52
    }
53
54
    /**
55
     * @inheritdoc
56
     */
57
    public function has($id)
58
    {
59
        return $this->container->has($id);
60
    }
61
62
    /**
63
     * @inheritdoc
64
     */
65
    public function get($id)
66
    {
67
        return $this->container->get($id);
68
    }
69
70
    /**
71
     * @inheritdoc
72
     */
73
    public function mapper(
74
        ORMInterface $orm,
75
        SchemaInterface $schema,
76
        string $role
77
    ): MapperInterface {
78
        $class = $schema->define($role, Schema::MAPPER) ?? Mapper::class;
79
80
        return $this->factory->make($class, [
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->factory->m...e\ORM\Schema::SCHEMA))) could return the type null which is incompatible with the type-hinted return Cycle\ORM\MapperInterface. Consider adding an additional type-check to rule them out.
Loading history...
81
            'orm'    => $orm,
82
            'role'   => $role,
83
            'schema' => $schema->define($role, Schema::SCHEMA)
84
        ]);
85
    }
86
87
    /**
88
     * @inheritdoc
89
     */
90
    public function loader(
91
        ORMInterface $orm,
92
        SchemaInterface $schema,
93
        string $role,
94
        string $relation
95
    ): LoaderInterface {
96
        $schema = $schema->defineRelation($role, $relation);
97
98
        return $this->config->getLoader($schema[Relation::TYPE])->resolve($this->factory, [
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->config->ge...ORM\Relation::SCHEMA])) could return the type null which is incompatible with the type-hinted return Cycle\ORM\Select\LoaderInterface. Consider adding an additional type-check to rule them out.
Loading history...
99
            'orm'    => $orm,
100
            'name'   => $relation,
101
            'target' => $schema[Relation::TARGET],
102
            'schema' => $schema[Relation::SCHEMA]
103
        ]);
104
    }
105
106
    /**
107
     * @inheritdoc
108
     */
109
    public function relation(
110
        ORMInterface $orm,
111
        SchemaInterface $schema,
112
        string $role,
113
        string $relation
114
    ): RelationInterface {
115
        $relSchema = $schema->defineRelation($role, $relation);
116
        $type = $relSchema[Relation::TYPE];
117
118
        return $this->config->getRelation($type)->resolve($this->factory, [
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->config->ge...ORM\Relation::SCHEMA])) could return the type null which is incompatible with the type-hinted return Cycle\ORM\Relation\RelationInterface. Consider adding an additional type-check to rule them out.
Loading history...
119
            'orm'    => $orm,
120
            'name'   => $relation,
121
            'target' => $relSchema[Relation::TARGET],
122
            'schema' => $relSchema[Relation::SCHEMA]
123
        ]);
124
    }
125
126
    /**
127
     * @inheritdoc
128
     */
129
    public function database(string $database): DatabaseInterface
130
    {
131
        return $this->dbal->database($database);
132
    }
133
}