Passed
Push — master ( 336aa4...192c12 )
by Fran
02:09
created

NOSQLStatusTrait::invokeHook()   A

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 bool
15
     */
16
    protected $isNew = true;
17
    /**
18
     * @var bool
19
     */
20
    protected $isModified = false;
21
    /**
22
     * @var array
23
     */
24
    protected $changes = [];
25
26
    /**
27
     * @return bool
28
     */
29
    public function isNew()
30
    {
31
        return $this->isNew;
32
    }
33
34
    /**
35
     * @param bool $isNew
36
     */
37
    public function setIsNew($isNew)
38
    {
39
        $this->isNew = $isNew;
40
    }
41
42
    /**
43
     * @return bool
44
     */
45
    public function isModified()
46
    {
47
        return $this->isModified;
48
    }
49
50
    /**
51
     * @param bool $isModified
52
     */
53
    public function setIsModified($isModified)
54
    {
55
        $this->isModified = $isModified;
56
    }
57
58
    /**
59
     * @return array
60
     */
61
    public function getChanges()
62
    {
63
        return $this->changes;
64
    }
65
66
    /**
67
     * @param array $changes
68
     */
69
    public function setChanges($changes)
70
    {
71
        $this->changes = $changes;
72
    }
73
74
    /**
75
     * @param string $property
76
     */
77
    public function addChanges($property) {
78
        if(!in_array($property, $this->changes)) {
79
            $this->changes[] = $property;
80
        }
81
    }
82
83
    /**
84
     * @param NOSQLActiveRecord $model
85
     * @param NOSQLModelDto $dto
86
     * @param $hook
87
     * @param Database|null $con
88
     * @throws \NOSQL\Exceptions\NOSQLValidationException
89
     */
90
    public static function invokeHook(NOSQLActiveRecord $model, NOSQLModelDto $dto, $hook, Database $con = null) {
91
        if(method_exists($model, $hook)) {
92
            $con = self::initConnection($con, $model);
93
            $model->feed($dto->toArray());
94
            $model->$hook($con);
95
        }
96
        unset($model);
97
    }
98
99
}