Pivot   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 160
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 5
Bugs 3 Features 0
Metric Value
c 5
b 3
f 0
dl 0
loc 160
rs 10
wmc 7
lcom 2
cbo 2

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 20 1
A getForeignKey() 0 4 1
A getOtherKey() 0 4 1
A setPivotKeys() 0 8 1
A hasTimestampAttributes() 0 4 1
A getCreatedAtColumn() 0 4 1
A getUpdatedAtColumn() 0 4 1
1
<?php
2
3
namespace Analogue\ORM\Relationships;
4
5
use Analogue\ORM\Entity;
6
7
class Pivot extends Entity
8
{
9
    /**
10
     * @var bool
11
     */
12
    protected $exists;
13
14
    /**
15
     * Pivot's table
16
     *
17
     * @var string
18
     */
19
    protected $table;
20
21
    /**
22
     * The parent entity of the relationship.
23
     *
24
     * @var object
25
     */
26
    protected $parent;
27
28
    /**
29
     * The parent entity of the relationship.
30
     *
31
     * @var \Analogue\ORM\EntityMap
32
     */
33
    protected $parentMap;
34
35
    /**
36
     * We may define a pivot mapping to deal with
37
     * soft deletes, timestamps, etc.
38
     *
39
     * @var \Analogue\ORM\EntityMap
40
     */
41
    protected $pivotMap;
42
43
    /**
44
     * The name of the foreign key column.
45
     *
46
     * @var string
47
     */
48
    protected $foreignKey;
49
50
    /**
51
     * The name of the "other key" column.
52
     *
53
     * @var string
54
     */
55
    protected $otherKey;
56
57
    /**
58
     * The attributes that aren't mass assignable.
59
     *
60
     * @var array
61
     */
62
    protected $guarded = [];
63
64
    /**
65
     * Pivot uses timestamps ?
66
     *
67
     * @var boolean
68
     */
69
    protected $timestamps;
70
71
    /**
72
     * Create a new pivot model instance.
73
     *
74
     * @param \Analogue\ORM\System\InternallyMappable $parent
75
     * @param \Analogue\ORM\EntityMap                 $parentMap
76
     * @param array                                   $attributes
77
     * @param string                                  $table
78
     * @param bool                                    $exists
79
     */
80
    public function __construct($parent, $parentMap, $attributes, $table, $exists = false)
81
    {
82
        // The pivot model is a "dynamic" model since we will set the tables dynamically
83
        // for the instance. This allows it work for any intermediate tables for the
84
        // many to many relationship that are defined by this developer's classes.
85
        $this->setEntityAttributes($attributes, true);
0 ignored issues
show
Unused Code introduced by
The call to Pivot::setEntityAttributes() has too many arguments starting with true.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
86
87
        $this->table = $table;
88
89
        // We store off the parent instance so we will access the timestamp column names
90
        // for the model, since the pivot model timestamps aren't easily configurable
91
        // from the developer's point of view. We can use the parents to get these.
92
        $this->parent = $parent;
93
94
        $this->parentMap = $parentMap;
95
96
        $this->exists = $exists;
97
98
        $this->timestamps = $this->hasTimestampAttributes();
99
    }
100
101
    /**
102
     * Get the foreign key column name.
103
     *
104
     * @return string
105
     */
106
    public function getForeignKey()
107
    {
108
        return $this->foreignKey;
109
    }
110
111
    /**
112
     * Get the "other key" column name.
113
     *
114
     * @return string
115
     */
116
    public function getOtherKey()
117
    {
118
        return $this->otherKey;
119
    }
120
121
    /**
122
     * Set the key names for the pivot model instance.
123
     *
124
     * @param  string $foreignKey
125
     * @param  string $otherKey
126
     * @return $this
127
     */
128
    public function setPivotKeys($foreignKey, $otherKey)
129
    {
130
        $this->foreignKey = $foreignKey;
131
132
        $this->otherKey = $otherKey;
133
134
        return $this;
135
    }
136
137
    /**
138
     * Determine if the pivot model has timestamp attributes.
139
     *
140
     * @return bool
141
     */
142
    public function hasTimestampAttributes()
143
    {
144
        return array_key_exists($this->getCreatedAtColumn(), $this->attributes);
145
    }
146
147
    /**
148
     * Get the name of the "created at" column.
149
     *
150
     * @return string
151
     */
152
    public function getCreatedAtColumn()
153
    {
154
        return $this->parentMap->getCreatedAtColumn();
155
    }
156
157
    /**
158
     * Get the name of the "updated at" column.
159
     *
160
     * @return string
161
     */
162
    public function getUpdatedAtColumn()
163
    {
164
        return $this->parentMap->getUpdatedAtColumn();
165
    }
166
}
167