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

NOSQLModelDto::resetPk()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
namespace NOSQL\Dto\Model;
3
4
use NOSQL\Exceptions\NOSQLValidationException;
5
use NOSQL\Services\base\NOSQLBase;
6
use PSFS\base\dto\Dto;
7
use PSFS\base\types\helpers\InjectorHelper;
8
9
/**
10
 * Class NOSQLModelDto
11
 * @package NOSQL\Dto\Model
12
 */
13
abstract class NOSQLModelDto extends Dto {
14
    /**
15
     * @var string
16
     * @label Model identifier
17
     */
18
    protected $_id;
19
20
    /**
21
     * @return string
22
     */
23
    public function getPk()
24
    {
25
        return $this->_id;
26
    }
27
28
    public function resetPk() {
29
        $this->_id = null;
30
        $this->_last_update = null;
31
    }
32
33
    /**
34
     * @param string $id
35
     * @throws NOSQLValidationException
36
     */
37
    public function setPk(string $id)
38
    {
39
        if(!empty($this->_id)) {
40
            throw new NOSQLValidationException(t('Primary key already defined'), NOSQLValidationException::NOSQL_VALIDATION_ID_ALREADY_DEFINED);
41
        }
42
        $this->_id = $id;
43
    }
44
45
    /**
46
     * @param string $format
47
     * @return \DateTime|string
48
     */
49
    public function getLastUpdate($format = null)
50
    {
51
        return null !== $format && null !== $this->_last_update ? $this->_last_update->format($format) : $this->_last_update;
52
    }
53
54
    /**
55
     * @param \DateTime|null $last_update
56
     * @throws \Exception
57
     */
58
    public function setLastUpdate(\DateTime $last_update = null)
59
    {
60
        $this->_last_update = $last_update ?: new \DateTime();
61
    }
62
    /**
63
     * @var \DateTime
64
     * @label Last update at
65
     */
66
    protected $_last_update;
67
68
    /**
69
     * @param bool $throwException
70
     * @return array
71
     * @throws NOSQLValidationException
72
     * @throws \ReflectionException
73
     */
74
    public function validate($throwException = false) {
75
        $errors = [];
76
        $reflection = new \ReflectionClass(get_called_class());
77
        foreach($reflection->getProperties() as $property) {
78
            $required = InjectorHelper::checkIsRequired($property->getDocComment());
79
            $value = $property->getValue($this);
80
            if($required && empty($value)) {
81
                if($throwException) {
82
                    throw new NOSQLValidationException(t('Empty value for property ') . $property->getName(), NOSQLValidationException::NOSQL_VALIDATION_REQUIRED);
83
                } else {
84
                    $errors[] = $property->getName();
85
                }
86
            } else {
87
                $this->checkType($throwException, $property, $value, $errors);
88
            }
89
        }
90
        return $errors;
91
    }
92
93
    public function toArray()
94
    {
95
        $array = parent::toArray();
96
        if(null !== $this->getPk()) {
0 ignored issues
show
introduced by
The condition null !== $this->getPk() is always true.
Loading history...
97
            $array['_id'] = $this->getPk();
98
        }
99
        $array['_last_update'] = $this->getLastUpdate(\DateTime::ATOM);
100
        return $array;
101
    }
102
103
    /**
104
     * @param $throwException
105
     * @param \ReflectionProperty $property
106
     * @param $value
107
     * @param array $errors
108
     * @throws NOSQLValidationException
109
     */
110
    private function checkType($throwException, \ReflectionProperty $property, $value, array &$errors)
111
    {
112
        $type = InjectorHelper::extractVarType($property->getDocComment());
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $type is correct as PSFS\base\types\helpers\...perty->getDocComment()) targeting PSFS\base\types\helpers\...elper::extractVarType() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
113
        switch (strtolower($type)) {
114
            case NOSQLBase::NOSQL_TYPE_LONG:
115
            case NOSQLBase::NOSQL_TYPE_INTEGER:
116
            case NOSQLBase::NOSQL_TYPE_DOUBLE:
117
                if (!is_numeric($value)) {
118
                    $errors[] = $property->getName();
119
                }
120
                break;
121
            case NOSQLBase::NOSQL_TYPE_ENUM:
122
                $values = explode('|', InjectorHelper::getValues($property->getDocComment()));
0 ignored issues
show
Bug introduced by
The method getValues() does not exist on PSFS\base\types\helpers\InjectorHelper. ( Ignorable by Annotation )

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

122
                $values = explode('|', InjectorHelper::/** @scrutinizer ignore-call */ getValues($property->getDocComment()));

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...
123
                if (!in_array($value, $values)) {
124
                    $errors[] = $property->getName();
125
                }
126
                break;
127
            case NOSQLBase::NOSQL_TYPE_ARRAY:
128
                if (!is_array($value)) {
129
                    $errors[] = $property->getName();
130
                }
131
                break;
132
            case NOSQLBase::NOSQL_TYPE_BOOLEAN:
133
                if (!in_array($value, [true, false, 0, 1])) {
134
                    $errors[] = $property->getName();
135
                }
136
                break;
137
        }
138
        if (in_array($property->getName(), $errors) && $throwException) {
139
            throw new NOSQLValidationException(t('Format not valid for property ') . $property->getName(), NOSQLValidationException::NOSQL_VALIDATION_NOT_VALID);
140
        }
141
    }
142
}