Completed
Push — master ( dbd6b1...1c86dd )
by Jared
04:09
created

BelongsTo::initQuery()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 11
Ratio 100 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 11
loc 11
rs 9.4285
cc 2
eloc 6
nc 2
nop 0
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 View Code Duplication
class BelongsTo extends Relation
0 ignored issues
show
Duplication introduced by
This class 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...
14
{
15
    protected function initQuery()
16
    {
17
        $value = $this->relation->{$this->localKey};
18
19
        if ($value === null) {
20
            $this->empty = true;
21
        }
22
23
        $this->query->where($this->foreignKey, $value)
24
                    ->limit(1);
25
    }
26
27
    public function getResults()
28
    {
29
        if ($this->empty) {
30
            return;
31
        }
32
33
        return $this->query->first();
34
    }
35
36
    public function create(array $values = [])
37
    {
38
        $class = $this->model;
39
        $model = new $class($values);
40
        $model->save();
41
42
        $this->relation->{$this->localKey} = $model->{$this->foreignKey};
43
        $this->relation->save();
44
45
        return $model;
46
    }
47
}
48