HasOneOrMany   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 64
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A addConstraints() 0 6 2
A getForeignKey() 0 4 1
A getParentKey() 0 4 1
1
<?php
2
3
namespace Magister\Services\Database\Elegant\Relations;
4
5
use Magister\Services\Database\Elegant\Builder;
6
use Magister\Services\Database\Elegant\Model;
7
8
/**
9
 * Class HasOneOrMany.
10
 */
11
abstract class HasOneOrMany extends Relation
12
{
13
    /**
14
     * The foreign key of the parent model.
15
     *
16
     * @var string
17
     */
18
    protected $foreignKey;
19
20
    /**
21
     * The local key of the parent model.
22
     *
23
     * @var string
24
     */
25
    protected $localKey;
26
27
    /**
28
     * Create a new has one or many relationship instance.
29
     *
30
     * @param \Magister\Services\Database\Elegant\Builder $query
31
     * @param \Magister\Services\Database\Elegant\Model   $parent
32
     * @param string                                      $foreignKey
33
     * @param string                                      $localKey
34
     */
35
    public function __construct(Builder $query, Model $parent, $foreignKey, $localKey)
36
    {
37
        $this->foreignKey = $foreignKey;
38
        $this->localKey = $localKey;
39
40
        parent::__construct($query, $parent);
41
    }
42
43
    /**
44
     * Set the base constraints on the relation query.
45
     *
46
     * @return void
47
     */
48
    public function addConstraints()
49
    {
50
        if (static::$constraints) {
51
            $this->query->where($this->foreignKey, $this->getParentKey());
0 ignored issues
show
Documentation Bug introduced by
The method where does not exist on object<Magister\Services...tabase\Elegant\Builder>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
52
        }
53
    }
54
55
    /**
56
     * Get the foreign key for the relationship.
57
     *
58
     * @return string
59
     */
60
    public function getForeignKey()
61
    {
62
        return $this->foreignKey;
63
    }
64
65
    /**
66
     * Get the key value of the parent's local key.
67
     *
68
     * @return mixed
69
     */
70
    public function getParentKey()
71
    {
72
        return $this->parent->getAttribute($this->localKey);
73
    }
74
}
75