Completed
Push — master ( 9b95b7...985d72 )
by Jared
02:36
created

BelongsToMany::sync()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 20
Code Lines 12

Duplication

Lines 4
Ratio 20 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 4
loc 20
rs 9.4285
cc 3
eloc 12
nc 4
nop 1
1
<?php
2
3
/**
4
 * @author Jared King <[email protected]>
5
 *
6
 * @link http://jaredtking.com
7
 *
8
 * @copyright 2015 Jared King
9
 * @license MIT
10
 */
11
namespace Pulsar\Relation;
12
13
use Pulsar\Model;
14
use Pulsar\Query;
15
16
class BelongsToMany extends Relation
17
{
18
    /**
19
     * @var string
20
     */
21
    protected $tablename;
22
23
    /**
24
     * @param Model  $localModel
25
     * @param string $localKey     identifying key on local model
26
     * @param string $tablename    pivot table name
27
     * @param string $foreignModel foreign model class
28
     * @param string $foreignKey   identifying key on foreign model
29
     */
30
    public function __construct(Model $localModel, $localKey, $tablename, $foreignModel, $foreignKey)
31
    {
32
        $this->tablename = $tablename;
33
34
        parent::__construct($localModel, $localKey, $foreignModel, $foreignKey);
35
    }
36
37
    protected function initQuery()
38
    {
39
        $pivot = new Pivot();
40
        $pivot->setTablename($this->tablename);
41
42
        $ids = $this->localModel->ids();
43
        foreach ($ids as $idProperty => $id) {
44
            if ($id === null) {
45
                $this->empty = true;
46
            }
47
48
            $this->query->where($this->localKey, $id);
49
            $this->query->join($pivot, $this->foreignKey, $idProperty);
50
        }
51
    }
52
53
    public function getResults()
54
    {
55
        if ($this->empty) {
56
            return;
57
        }
58
59
        return $this->query->execute();
60
    }
61
62
    /**
63
     * Gets the pivot tablename.
64
     *
65
     * @return string
66
     */
67
    public function getTablename()
68
    {
69
        return $this->tablename;
70
    }
71
72
    public function save(Model $model)
73
    {
74
        $model->save();
75
        $this->attach($model);
76
77
        return $model;
78
    }
79
80 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...
81
    {
82
        $class = $this->foreignModel;
83
        $model = new $class();
84
        $model->create($values);
85
86
        $this->attach($model);
87
88
        return $model;
89
    }
90
91
    /**
92
     * Attaches a model to the relationship by creating
93
     * a pivot model.
94
     *
95
     * @param Model $model
96
     *
97
     * @return self
98
     */
99
    public function attach(Model $model)
100
    {
101
        // create pivot relation
102
        $pivot = new Pivot();
103
        $pivot->setTablename($this->tablename);
104
105
        // build the local side
106
        $ids = $this->localModel->ids();
107
        foreach ($ids as $property => $id) {
108
            $pivot->{$this->localKey} = $id;
109
        }
110
111
        // build the foreign side
112
        $ids = $model->ids();
113
        foreach ($ids as $property => $id) {
114
            $pivot->{$this->foreignKey} = $id;
115
        }
116
117
        $pivot->save();
118
        $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...
119
120
        return $this;
121
    }
122
123
    /**
124
     * Detaches a model from the relationship by deleting
125
     * the pivot model.
126
     *
127
     * @param Model $model
128
     *
129
     * @return self
130
     */
131
    public function detach(Model $model)
132
    {
133
        $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...
134
        unset($model->pivot);
135
136
        return $this;
137
    }
138
139
    /**
140
     * Removes any relationships that are not included
141
     * in the list of IDs.
142
     * 
143
     * @param array $ids
144
     *
145
     * @return self
146
     */
147
    public function sync(array $ids)
148
    {
149
        $pivot = new Pivot();
150
        $pivot->setTablename($this->tablename);
151
        $query = new Query($pivot);
152
153
        $localIds = $this->localModel->ids();
154
        foreach ($localIds as $property => $id) {
155
            $query->where($this->localKey, $id);
156
        }
157
158 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...
159
            $in = implode(',', $ids);
160
            $query->where("{$this->foreignKey} NOT IN ($in)");
161
        }
162
163
        $query->delete();
164
165
        return $this;
166
    }
167
}
168