ActiveRecordTrait   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Test Coverage

Coverage 39.38%

Importance

Changes 3
Bugs 2 Features 0
Metric Value
eloc 22
c 3
b 2
f 0
dl 0
loc 89
ccs 13
cts 33
cp 0.3938
rs 10
wmc 14

10 Methods

Rating   Name   Duplication   Size   Complexity  
A writeDBData() 0 4 2
A getDBData() 0 3 1
A fieldUpdatedFromDb() 0 12 4
A insert() 0 3 1
A saveRecord() 0 3 1
A isInDB() 0 4 1
A delete() 0 3 1
A exists() 0 3 1
A update() 0 4 1
A save() 0 3 1
1
<?php
2
3
namespace Nip\Records\Traits\ActiveRecord;
4
5
use Nip\Records\AbstractModels\Record;
6
use Nip\Records\Traits\HasPrimaryKey\RecordTrait as HasPrimaryKeyTrait;
7
8
/**
9
 * Class ActiveRecordTrait
10
 * @package Nip\Records\Traits\ActiveRecord
11
 */
12
trait ActiveRecordTrait
13
{
14
    use HasPrimaryKeyTrait;
15
16
    protected $dbData = [];
17
18
    /**
19
     * @param bool|array $data
20
     */
21 4
    public function writeDBData($data = false)
22
    {
23 4
        foreach ($data as $key => $value) {
24 1
            $this->dbData[$key] = $value;
25
        }
26 4
    }
27
28
    /**
29
     * @return array
30
     */
31
    public function getDBData()
32
    {
33
        return $this->dbData;
34
    }
35
36
    /**
37
     * @param $field
38
     * @return bool
39
     */
40 1
    public function fieldUpdatedFromDb($field)
41
    {
42 1
        if (!isset($this->{$field})) {
43 1
            return false;
44
        }
45 1
        if (!isset($this->dbData[$field])) {
46
            return false;
47
        }
48 1
        if ($this->{$field} == $this->dbData[$field]) {
49 1
            return false;
50
        }
51 1
        return true;
52
    }
53
54
    /**
55
     * @return bool
56
     */
57 1
    public function insert()
58
    {
59 1
        return $this->getManager()->insert($this);
0 ignored issues
show
Bug introduced by
It seems like getManager() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

59
        return $this->/** @scrutinizer ignore-call */ getManager()->insert($this);
Loading history...
60
    }
61
62
    /**
63
     * @return bool|\Nip\Database\Result
64
     */
65
    public function update()
66
    {
67
        $return = $this->getManager()->update($this);
68
        return $return;
69
    }
70
71
    public function save()
72
    {
73
        $this->getManager()->save($this);
74
    }
75
76
    public function saveRecord()
77
    {
78
        $this->getManager()->save($this);
79
    }
80
81
    public function delete()
82
    {
83
        $this->getManager()->delete($this);
84
    }
85
86
    /**
87
     * @return bool
88
     */
89
    public function isInDB()
90
    {
91
        $primaryKey = $this->getManager()->getPrimaryKey();
92
        return $this->{$primaryKey} > 0;
93
    }
94
95
    /**
96
     * @return bool|false|Record
97
     */
98
    public function exists()
99
    {
100
        return $this->getManager()->exists($this);
101
    }
102
}
103