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

HasMany::attach()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
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\Model;
16
17
class HasMany extends Relation
18
{
19
    /**
20
     * @param Model  $localModel
21
     * @param string $localKey     identifying key on local model
22
     * @param string $foreignModel foreign model class
23
     * @param string $foreignKey   identifying key on foreign model
24
     */
25 View Code Duplication
    public function __construct(Model $localModel, $localKey, $foreignModel, $foreignKey)
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...
26
    {
27
        // the default foreign key would look like
28
        // `user_id` for a model named User
29
        if (!$foreignKey) {
30
            $inflector = Inflector::get();
31
            $foreignKey = strtolower($inflector->underscore($localModel::modelName())).'_id';
32
        }
33
34
        if (!$localKey) {
35
            $localKey = Model::DEFAULT_ID_PROPERTY;
36
        }
37
38
        parent::__construct($localModel, $localKey, $foreignModel, $foreignKey);
39
    }
40
41 View Code Duplication
    protected function initQuery()
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...
42
    {
43
        $localKey = $this->localKey;
44
        $id = $this->localModel->$localKey;
45
46
        if ($id === false) {
47
            $this->empty = true;
48
        }
49
50
        $this->query->where($this->foreignKey, $id);
51
    }
52
53
    public function getResults()
54
    {
55
        if ($this->empty) {
56
            return;
57
        }
58
59
        return $this->query->execute();
60
    }
61
62
    public function save(Model $model)
63
    {
64
        $model->{$this->foreignKey} = $this->localModel->{$this->localKey};
65
        $model->saveOrFail();
66
67
        return $model;
68
    }
69
70 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...
71
    {
72
        $class = $this->foreignModel;
73
        $model = new $class();
74
        $model->{$this->foreignKey} = $this->localModel->{$this->localKey};
75
        $model->create($values);
76
77
        return $model;
78
    }
79
80
    /**
81
     * Attaches a child model from this model.
82
     *
83
     * @param Model $model child model
84
     *
85
     * @throws \Pulsar\Exception\ModelException when the operation fails
86
     *
87
     * @return self
88
     */
89
    public function attach(Model $model)
90
    {
91
        $model->{$this->foreignKey} = $this->localModel->{$this->localKey};
92
        $model->saveOrFail();
93
94
        return $this;
95
    }
96
97
    /**
98
     * Detaches a child model from this model.
99
     *
100
     * @param Model $model child model
101
     *
102
     * @throws \Pulsar\Exception\ModelException when the operation fails
103
     *
104
     * @return self
105
     */
106
    public function detach(Model $model)
107
    {
108
        $model->{$this->foreignKey} = null;
109
        $model->saveOrFail();
110
111
        return $this;
112
    }
113
114
    /**
115
     * Removes any relationships that are not included
116
     * in the list of IDs.
117
     *
118
     * @param array $ids
119
     *
120
     * @throws \Pulsar\Exception\ModelException when the operation fails
121
     *
122
     * @return self
123
     */
124
    public function sync(array $ids)
125
    {
126
        $class = $this->foreignModel;
127
        $model = new $class();
128
        $query = $model::query();
129
130 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...
131
            $in = implode(',', $ids);
132
            $query->where("{$this->foreignKey} NOT IN ($in)");
133
        }
134
135
        $query->delete();
136
137
        return $this;
138
    }
139
}
140