Completed
Push — master ( e55059...ea0818 )
by Adrian
02:34
created

RelationBuilder   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

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

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
     * @var Orm
13
     */
14
    protected $orm;
15
16
    public function __construct(Orm $orm)
17
    {
18
        $this->orm = $orm;
19
    }
20
21
    public function newRelation(Mapper $nativeMapper, $name, $options): Relation
22
    {
23
        $foreignMapper = $options[RelationConfig::FOREIGN_MAPPER];
24
        if ($this->orm->has($foreignMapper)) {
25
            if (! $foreignMapper instanceof Mapper) {
26
                $foreignMapper = $this->orm->get($foreignMapper);
27
            }
28
        }
29
        $type          = $options[RelationConfig::TYPE];
30
        $relationClass = __NAMESPACE__ . '\\' . Str::className($type);
31
32
        if (! class_exists($relationClass)) {
33
            throw new \InvalidArgumentException("{$relationClass} does not exist");
34
        }
35
36
        return new $relationClass($name, $nativeMapper, $foreignMapper, $options);
37
    }
38
}