NOSQLStatusTrait::invokeHook()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 4
1
<?php
2
namespace NOSQL\Models\base;
3
4
use MongoDB\Database;
5
use NOSQL\Dto\Model\NOSQLModelDto;
6
use NOSQL\Models\NOSQLActiveRecord;
7
8
/**
9
 * Trait NOSQLStatusTrail
10
 * @package NOSQL\Models\base
11
 */
12
trait NOSQLStatusTrait {
13
    /**
14
     * @var int
15
     */
16
    protected $actionCount = 0;
17
    /**
18
     * @var bool
19
     */
20
    protected $isNew = true;
21
    /**
22
     * @var bool
23
     */
24
    protected $isModified = false;
25
    /**
26
     * @var array
27
     */
28
    protected $changes = [];
29
30
    /**
31
     * @return bool
32
     */
33
    public function isNew()
34
    {
35
        return $this->isNew;
36
    }
37
38
    /**
39
     * @param bool $isNew
40
     */
41
    public function setIsNew($isNew)
42
    {
43
        $this->isNew = $isNew;
44
    }
45
46
    /**
47
     * @return bool
48
     */
49
    public function isModified()
50
    {
51
        return $this->isModified;
52
    }
53
54
    /**
55
     * @param bool $isModified
56
     */
57
    public function setIsModified($isModified)
58
    {
59
        $this->isModified = $isModified;
60
    }
61
62
    /**
63
     * @return array
64
     */
65
    public function getChanges()
66
    {
67
        return $this->changes;
68
    }
69
70
    /**
71
     * @param array $changes
72
     */
73
    public function setChanges($changes)
74
    {
75
        $this->changes = $changes;
76
    }
77
78
    /**
79
     * @param string $property
80
     */
81
    public function addChanges($property) {
82
        if(!in_array($property, $this->changes)) {
83
            $this->changes[] = $property;
84
        }
85
    }
86
    /**
87
     * @return int
88
     */
89
    protected function getActionCount()
90
    {
91
        return $this->actionCount;
92
    }
93
94
    /**
95
     * @param int $actionCount
96
     */
97
    protected function setActionCount($actionCount)
98
    {
99
        $this->actionCount = $actionCount;
100
    }
101
102
    protected function countAction() {
103
        $this->actionCount++;
104
    }
105
106
    /**
107
     * @param NOSQLActiveRecord $model
108
     * @param NOSQLModelDto $dto
109
     * @param $hook
110
     * @param Database|null $con
111
     * @throws \NOSQL\Exceptions\NOSQLValidationException
112
     */
113
    public static function invokeHook(NOSQLActiveRecord $model, NOSQLModelDto $dto, $hook, Database $con = null) {
114
        if(method_exists($model, $hook)) {
115
            $con = self::initConnection($model, $con);
116
            $model->feed($dto->toArray());
117
            $model->$hook($con);
118
        }
119
        unset($model);
120
    }
121
122
}