Completed
Push — master ( a3416c...a17b49 )
by Jared
02:25
created

BelongsToMany::__construct()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 30
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 14
nc 8
nop 5
1
<?php
2
3
/**
4
 * @author Jared King <[email protected]>
5
 *
6
 * @see http://jaredtking.com
7
 *
8
 * @copyright 2015 Jared King
9
 * @license MIT
10
 */
11
12
namespace Pulsar\Relation;
13
14
use ICanBoogie\Inflector;
15
use Pulsar\Exception\ModelException;
16
use Pulsar\Model;
17
use Pulsar\Query;
18
19
class BelongsToMany extends Relation
20
{
21
    /**
22
     * @var string
23
     */
24
    protected $tablename;
25
26
    /**
27
     * @param Model  $localModel
28
     * @param string $localKey     identifying key on local model
29
     * @param string $tablename    pivot table name
30
     * @param string $foreignModel foreign model class
31
     * @param string $foreignKey   identifying key on foreign model
32
     */
33
    public function __construct(Model $localModel, $localKey, $tablename, $foreignModel, $foreignKey)
34
    {
35
        // the default local key would look like `user_id`
36
        // for a model named User
37
        if (!$localKey) {
38
            $inflector = Inflector::get();
39
            $localKey = strtolower($inflector->underscore($foreignModel::modelName())).'_id';
40
        }
41
42
        if (!$foreignKey) {
43
            $foreignKey = Model::DEFAULT_ID_PROPERTY;
44
        }
45
46
        // the default pivot table name looks like
47
        // RoleUser for models named Role and User.
48
        // the tablename is built from the model names
49
        // in alphabetic order.
50
        if (!$tablename) {
51
            $names = [
52
                $localModel::modelName(),
53
                $foreignModel::modelName(),
54
            ];
55
            sort($names);
56
            $tablename = implode($names);
57
        }
58
59
        $this->tablename = $tablename;
60
61
        parent::__construct($localModel, $localKey, $foreignModel, $foreignKey);
62
    }
63
64
    protected function initQuery()
65
    {
66
        $pivot = new Pivot();
67
        $pivot->setTablename($this->tablename);
68
69
        $ids = $this->localModel->ids();
70
        foreach ($ids as $idProperty => $id) {
71
            if ($id === false) {
72
                $this->empty = true;
73
            }
74
75
            $this->query->where($this->localKey, $id);
76
            $this->query->join($pivot, $this->foreignKey, $idProperty);
77
        }
78
    }
79
80
    public function getResults()
81
    {
82
        if ($this->empty) {
83
            return;
84
        }
85
86
        return $this->query->execute();
87
    }
88
89
    /**
90
     * Gets the pivot tablename.
91
     *
92
     * @return string
93
     */
94
    public function getTablename()
95
    {
96
        return $this->tablename;
97
    }
98
99
    public function save(Model $model)
100
    {
101
        $model->saveOrFail();
102
        $this->attach($model);
103
104
        return $model;
105
    }
106
107 View Code Duplication
    public function create(array $values = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
108
    {
109
        $class = $this->foreignModel;
110
        $model = new $class();
111
        $model->create($values);
112
113
        $this->attach($model);
114
115
        return $model;
116
    }
117
118
    /**
119
     * Attaches a model to the relationship by creating
120
     * a pivot model.
121
     *
122
     * @param Model $model
123
     *
124
     * @throws ModelException when the operation fails
125
     *
126
     * @return self
127
     */
128
    public function attach(Model $model)
129
    {
130
        // create pivot relation
131
        $pivot = new Pivot();
132
        $pivot->setTablename($this->tablename);
133
        $pivot->setProperties($this->localKey, $this->foreignKey);
134
135
        // build the local side
136
        $ids = $this->localModel->ids();
137
        foreach ($ids as $property => $id) {
138
            $pivot->{$this->localKey} = $id;
139
        }
140
141
        // build the foreign side
142
        $ids = $model->ids();
143
        foreach ($ids as $property => $id) {
144
            $pivot->{$this->foreignKey} = $id;
145
        }
146
147
        $pivot->saveOrFail();
148
        $model->pivot = $pivot;
0 ignored issues
show
Documentation introduced by
The property pivot does not exist on object<Pulsar\Model>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
149
150
        return $this;
151
    }
152
153
    /**
154
     * Detaches a model from the relationship by deleting
155
     * the pivot model.
156
     *
157
     * @param Model $model
158
     *
159
     * @throws ModelException when the operation fails
160
     *
161
     * @return self
162
     */
163
    public function detach(Model $model)
164
    {
165
        $model->pivot->delete();
0 ignored issues
show
Documentation introduced by
The property pivot does not exist on object<Pulsar\Model>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
166
        unset($model->pivot);
167
168
        return $this;
169
    }
170
171
    /**
172
     * Removes any relationships that are not included
173
     * in the list of IDs.
174
     *
175
     * @param array $ids
176
     *
177
     * @throws ModelException when the operation fails
178
     *
179
     * @return self
180
     */
181
    public function sync(array $ids)
182
    {
183
        $pivot = new Pivot();
184
        $pivot->setTablename($this->tablename);
185
        $query = new Query($pivot);
186
187
        $localIds = $this->localModel->ids();
188
        foreach ($localIds as $property => $id) {
189
            $query->where($this->localKey, $id);
190
        }
191
192 View Code Duplication
        if (count($ids) > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
193
            $in = implode(',', $ids);
194
            $query->where("{$this->foreignKey} NOT IN ($in)");
195
        }
196
197
        $query->delete();
198
199
        return $this;
200
    }
201
}
202