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

HasForeignKeyTrait   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 76.47%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 51
ccs 13
cts 17
cp 0.7647
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A checkParamFk() 0 7 2
A getFK() 0 8 2
A setFK() 0 4 1
A initFK() 0 4 1
A generateFK() 0 4 1
1
<?php
2
3
namespace Nip\Records\Relations\Traits;
4
5
use Nip\Records\RecordManager;
6
7
/**
8
 * Trait HasForeignKeyTrait
9
 * @package Nip\Records\Relations\Traits
10
 *
11
 * @method RecordManager getManager()
12
 */
13
trait HasForeignKeyTrait
14
{
15
    /**
16
     * @var null|string
17
     */
18
    protected $fk = null;
19
20
    /**
21
     * @param $params
22
     */
23 3
    public function checkParamFk($params)
24
    {
25 3
        if (isset($params['fk'])) {
26
            $this->setFK($params['fk']);
27
            unset($params['fk']);
28
        }
29 3
    }
30
31
    /**
32
     * @return string
33
     */
34 12
    public function getFK()
35
    {
36 12
        if ($this->fk == null) {
37 12
            $this->initFK();
38
        }
39
40 12
        return $this->fk;
41
    }
42
43
    /**
44
     * @param $name
45
     */
46 12
    public function setFK($name)
47
    {
48 12
        $this->fk = $name;
49 12
    }
50
51 12
    protected function initFK()
52
    {
53 12
        $this->setFK($this->generateFK());
54 12
    }
55
56
    /**
57
     * @return string
58
     */
59
    protected function generateFK()
60
    {
61
        return $this->getManager()->getPrimaryFK();
62
    }
63
}
64