Completed
Push — master ( 4ac004...470d43 )
by Gabriel
06:41
created

HasPivotPrimaryKey::setPivotPrimaryKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Nip\Records\Relations\Traits;
4
5
use Nip\Records\AbstractModels\RecordManager;
6
7
/**
8
 * Trait HasPivotPrimaryKey
9
 * @package Nip\Records\Relations\Traits
10
 *
11
 * @method RecordManager getWith
12
 */
13
trait HasPivotPrimaryKey
14
{
15
    /**
16
     * @var null|string
17
     */
18
    protected $pivotPrimaryKey = null;
19
20
    /**
21
     * @param $params
22
     */
23
    public function checkParamPivotPrimaryKey($params)
24
    {
25
        if (isset($params['pivotPrimaryKey'])) {
26
            $this->setPivotPrimaryKey($params['pivotPrimaryKey']);
27
            unset($params['pivotPrimaryKey']);
28
        }
29
    }
30
31
32
    /**
33
     * @return string
34
     */
35 1
    public function getPivotPrimaryKey()
36
    {
37 1
        if ($this->pivotPrimaryKey == null) {
38 1
            $this->initPivotPrimaryKey();
39
        }
40
41 1
        return $this->pivotPrimaryKey;
42
    }
43
44
    /**
45
     * @param $name
46
     */
47 1
    public function setPivotPrimaryKey($name)
48
    {
49 1
        $this->pivotPrimaryKey = $name;
50 1
    }
51
52 1
    protected function initPivotPrimaryKey()
53
    {
54 1
        $this->setPivotPrimaryKey($this->generatePivotPrimaryKey());
55 1
    }
56
57
    /**
58
     * @return string
59
     */
60 1
    protected function generatePivotPrimaryKey()
61
    {
62 1
        return $this->getWith()->getPrimaryKey();
0 ignored issues
show
Bug introduced by
It seems like getWith() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
63
    }
64
}
65