Completed
Push — master ( 62d3ac...e8b70a )
by Jared
02:18
created

BelongsToMany::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 10
Ratio 100 %

Importance

Changes 3
Bugs 0 Features 2
Metric Value
c 3
b 0
f 2
dl 10
loc 10
rs 9.4285
cc 1
eloc 6
nc 1
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
15
class BelongsToMany extends Relation
16
{
17
    /**
18
     * @var string
19
     */
20
    protected $tablename;
21
22
    /**
23
     * @param Model  $localModel
24
     * @param string $localKey     identifying key on local model
25
     * @param string $tablename    pivot table name
26
     * @param string $foreignModel foreign model class
27
     * @param string $foreignKey   identifying key on foreign model
28
     */
29
    public function __construct(Model $localModel, $localKey, $tablename, $foreignModel, $foreignKey)
30
    {
31
        $this->tablename = $tablename;
32
33
        parent::__construct($localModel, $localKey, $foreignModel, $foreignKey);
34
    }
35
36
    protected function initQuery()
37
    {
38
        $pivot = new Pivot();
39
        $pivot->setTablename($this->tablename);
40
41
        $ids = $this->localModel->ids();
42
        foreach ($ids as $idProperty => $id) {
43
            if ($id === null) {
44
                $this->empty = true;
45
            }
46
47
            $this->query->where($this->localKey, $id);
48
            $this->query->join($pivot, $this->foreignKey, $idProperty);
49
        }
50
    }
51
52
    public function getResults()
53
    {
54
        if ($this->empty) {
55
            return;
56
        }
57
58
        return $this->query->execute();
59
    }
60
61
    /**
62
     * Gets the pivot tablename.
63
     *
64
     * @return string
65
     */
66
    public function getTablename()
67
    {
68
        return $this->tablename;
69
    }
70
71 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...
72
    {
73
        $class = $this->foreignModel;
74
        $model = new $class();
75
        $model->create($values);
76
77
        $this->attach($model);
78
79
        return $model;
80
    }
81
82
    /**
83
     * Attaches a model to the relationship by creating
84
     * a pivot model.
85
     *
86
     * @param Model $model
87
     *
88
     * @return self
89
     */
90
    public function attach(Model $model)
91
    {
92
        // create pivot relation
93
        $pivot = new Pivot();
94
        $pivot->setTablename($this->tablename);
95
96
        // build the local side
97
        $ids = $this->localModel->ids();
98
        foreach ($ids as $property => $id) {
99
            $pivot->{$this->localKey} = $id;
100
        }
101
102
        // build the foreign side
103
        $ids = $model->ids();
104
        foreach ($ids as $property => $id) {
105
            $pivot->{$this->foreignKey} = $id;
106
        }
107
108
        $pivot->save();
109
        $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...
110
111
        return $this;
112
    }
113
114
    /**
115
     * Detaches a model from the relationship by deleting
116
     * the pivot model.
117
     *
118
     * @param Model $model
119
     *
120
     * @return self
121
     */
122
    public function detach(Model $model)
123
    {
124
        $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...
125
        unset($model->pivot);
126
127
        return $this;
128
    }
129
}
130