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

BelongsTo::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 15
Ratio 100 %

Importance

Changes 0
Metric Value
dl 15
loc 15
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 4
nop 4
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 BelongsTo 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
        if (!$foreignKey) {
28
            $foreignKey = Model::DEFAULT_ID_PROPERTY;
29
        }
30
31
        // the default local key would look like `user_id`
32
        // for a model named User
33
        if (!$localKey) {
34
            $inflector = Inflector::get();
35
            $localKey = strtolower($inflector->underscore($foreignModel::modelName())).'_id';
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
        $id = $this->localModel->{$this->localKey};
44
45
        if ($id === null) {
46
            $this->empty = true;
47
        }
48
49
        $this->query->where($this->foreignKey, $id)
50
            ->limit(1);
51
    }
52
53
    public function getResults()
54
    {
55
        if ($this->empty) {
56
            return;
57
        }
58
59
        return $this->query->first();
60
    }
61
62
    public function save(Model $model)
63
    {
64
        $model->saveOrFail();
65
        $this->attach($model);
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->create($values);
75
76
        $this->attach($model);
77
78
        return $model;
79
    }
80
81
    /**
82
     * Attaches this model to an owning model.
83
     *
84
     * @param Model $model owning model
85
     *
86
     * @return self
87
     */
88
    public function attach(Model $model)
89
    {
90
        $this->localModel->{$this->localKey} = $model->{$this->foreignKey};
91
        $this->localModel->saveOrFail();
92
93
        return $this;
94
    }
95
96
    /**
97
     * Detaches this model from the owning model.
98
     *
99
     * @return self
100
     */
101
    public function detach()
102
    {
103
        $this->localModel->{$this->localKey} = null;
104
        $this->localModel->saveOrFail();
105
106
        return $this;
107
    }
108
}
109