HasPivotPrimaryKey::getPivotPrimaryKey()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 3
c 1
b 0
f 1
nc 2
nop 0
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
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) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $this->pivotPrimaryKey of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
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();
63
    }
64
}
65