RelationBuilder::newRelation()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 9
c 1
b 0
f 0
nc 6
nop 3
dl 0
loc 16
rs 9.9666
1
<?php
2
3
namespace Sirius\Orm\Relation;
4
5
use Sirius\Orm\Helpers\Str;
6
use Sirius\Orm\Mapper;
7
use Sirius\Orm\Orm;
8
9
class RelationBuilder
10
{
11
12
    /**
13
     * @var Orm
14
     */
15
    protected $orm;
16
17
    public function __construct(Orm $orm)
18
    {
19
        $this->orm = $orm;
20
    }
21
22
    public function newRelation(Mapper $nativeMapper, $name, $options): Relation
23
    {
24
        $foreignMapper = $options[RelationConfig::FOREIGN_MAPPER];
25
        if ($this->orm->has($foreignMapper)) {
26
            if (! $foreignMapper instanceof Mapper) {
27
                $foreignMapper = $this->orm->get($foreignMapper);
28
            }
29
        }
30
        $type          = $options[RelationConfig::TYPE];
31
        $relationClass = __NAMESPACE__ . '\\' . Str::className($type);
32
33
        if (! class_exists($relationClass)) {
34
            throw new \InvalidArgumentException("{$relationClass} does not exist");
35
        }
36
37
        return new $relationClass($name, $nativeMapper, $foreignMapper, $options);
38
    }
39
}
40