RelationBuilder   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 5
eloc 12
c 2
b 0
f 1
dl 0
loc 29
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A newRelation() 0 16 4
A __construct() 0 3 1
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