Completed
Push — master ( febdbe...957a77 )
by Fran
03:33
created

NOSQLModelTrait::preDelete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 1
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 0
dl 0
loc 1
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 1
1
<?php
2
namespace NOSQL\Models\base;
3
4
use MongoDB\BSON\ObjectId;
5
use MongoDB\BSON\UTCDateTime;
6
use MongoDB\Database;
7
use NOSQL\Dto\Model\NOSQLModelDto;
8
9
/**
10
 * Trait NOSQLModelTrait
11
 * @package NOSQL\Models\base
12
 */
13
trait NOSQLModelTrait {
14
    use NOSQLStatusTrait;
15
    /**
16
     * @var NOSQLModelDto
17
     */
18
    protected $dto;
19
20
    /**
21
     * @param string $name
22
     * @return mixed|null
23
     */
24
    public function __get($name)
25
    {
26
        $value = null;
27
        if(null !== $this->dto && property_exists($this->dto, $name)) {
28
            $value = $this->dto->$name;
29
        }
30
        return $value;
31
    }
32
33
    /**
34
     * @param string $name
35
     * @param mixed $value
36
     */
37
    public function __set($name, $value)
38
    {
39
        if(null !== $this->dto && property_exists($this->dto, $name)) {
40
            $this->dto->$name = $value;
41
            $this->addChanges($name);
42
        }
43
        $this->setIsModified(true);
44
    }
45
46
    public function __call($name, $arguments)
47
    {
48
        if(preg_match('/^(set|get)/', $name)) {
49
            $property = str_replace(['set', 'Set', 'get', 'Get'], '', $name);
50
            if(false !== stripos($name, 'set')) {
51
                $this->$property = $arguments[0];
52
            } else {
53
                return $this->$property;
54
            }
55
56
        }
57
    }
58
59
    /**
60
     * @param array $data
61
     * @param bool $withName
62
     * @throws \NOSQL\Exceptions\NOSQLValidationException
63
     */
64
    public function feed(array $data, $withName = false) {
65
        $name = '';
66
        $sep = '';
67
        foreach($data as $key => $value) {
68
            if($value instanceof ObjectId) {
69
                $this->dto->setPk($value->jsonSerialize()['$oid']);
0 ignored issues
show
Bug introduced by
The method jsonSerialize() does not exist on MongoDB\BSON\ObjectId. ( Ignorable by Annotation )

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

69
                $this->dto->setPk($value->/** @scrutinizer ignore-call */ jsonSerialize()['$oid']);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
70
            } elseif($key === '_last_update') {
71
                $this->dto->setLastUpdate($value instanceof UTCDateTime ? $value : null);
0 ignored issues
show
Bug introduced by
It seems like $value instanceof MongoD...ateTime ? $value : null can also be of type MongoDB\BSON\UTCDateTime; however, parameter $last_update of NOSQL\Dto\Model\NOSQLModelDto::setLastUpdate() does only seem to accept DateTime|null, maybe add an additional type check? ( Ignorable by Annotation )

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

71
                $this->dto->setLastUpdate(/** @scrutinizer ignore-type */ $value instanceof UTCDateTime ? $value : null);
Loading history...
72
            } else {
73
                switch(get_class($value)) {
74
                    case UTCDateTime::class:
75
                        $value = $value->toDateTime()->format('Y-m-d H:i:s');
76
                        break;
77
                }
78
                $this->$key = $value;
79
                if(in_array(strtolower($key), ['name', 'label', 'title', 'method'])) {
80
                    $name .= $sep . $value;
81
                    $sep = ' ';
82
                }
83
            }
84
        }
85
        if($withName) {
86
            $this->dto->setName($name);
87
        }
88
    }
89
90
    /**
91
     * @param array $data
92
     * @return NOSQLModelTrait
93
     * @throws \NOSQL\Exceptions\NOSQLValidationException
94
     */
95
    public static function fromArray(array $data) {
96
        $modelName = get_called_class();
97
        /** @var NOSQLModelTrait $model */
98
        $model = new $modelName();
99
        $model->feed($data);
100
        return $model;
101
    }
102
103
    /**
104
     * Before insert hook
105
     * @param Database $con
106
     */
107
    protected function preInsert(Database $con = null) {}
0 ignored issues
show
Unused Code introduced by
The parameter $con is not used and could be removed. ( Ignorable by Annotation )

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

107
    protected function preInsert(/** @scrutinizer ignore-unused */ Database $con = null) {}

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
108
109
    /**
110
     * Before update hook
111
     * @param Database $con
112
     */
113
    protected function preUpdate(Database $con = null) {}
0 ignored issues
show
Unused Code introduced by
The parameter $con is not used and could be removed. ( Ignorable by Annotation )

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

113
    protected function preUpdate(/** @scrutinizer ignore-unused */ Database $con = null) {}

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
114
115
    /**
116
     * Before save hook
117
     * @param Database $con
118
     */
119
    protected function preSave(Database $con = null) {}
0 ignored issues
show
Unused Code introduced by
The parameter $con is not used and could be removed. ( Ignorable by Annotation )

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

119
    protected function preSave(/** @scrutinizer ignore-unused */ Database $con = null) {}

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
120
121
    /**
122
     * Before delete hook
123
     * @param Database $con
124
     */
125
    protected function preDelete(Database $con = null) {}
0 ignored issues
show
Unused Code introduced by
The parameter $con is not used and could be removed. ( Ignorable by Annotation )

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

125
    protected function preDelete(/** @scrutinizer ignore-unused */ Database $con = null) {}

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
126
127
    /**
128
     * After insert hook
129
     * @param Database $con
130
     */
131
    protected function postInsert(Database $con = null) {}
0 ignored issues
show
Unused Code introduced by
The parameter $con is not used and could be removed. ( Ignorable by Annotation )

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

131
    protected function postInsert(/** @scrutinizer ignore-unused */ Database $con = null) {}

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
132
133
    /**
134
     * After update hook
135
     * @param Database $con
136
     */
137
    protected function postUpdate(Database $con = null) {}
0 ignored issues
show
Unused Code introduced by
The parameter $con is not used and could be removed. ( Ignorable by Annotation )

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

137
    protected function postUpdate(/** @scrutinizer ignore-unused */ Database $con = null) {}

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
138
139
    /**
140
     * After save hook
141
     * @param Database $con
142
     */
143
    protected function postSave(Database $con = null) {}
0 ignored issues
show
Unused Code introduced by
The parameter $con is not used and could be removed. ( Ignorable by Annotation )

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

143
    protected function postSave(/** @scrutinizer ignore-unused */ Database $con = null) {}

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
144
145
    /**
146
     * After delete hook
147
     * @param Database $con
148
     */
149
    protected function postDelete(Database $con = null) {}
0 ignored issues
show
Unused Code introduced by
The parameter $con is not used and could be removed. ( Ignorable by Annotation )

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

149
    protected function postDelete(/** @scrutinizer ignore-unused */ Database $con = null) {}

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
150
}