Completed
Push — master ( 6e6d73...1dcfbd )
by Cesar
14s queued 11s
created

AbstractModel   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 39
dl 0
loc 100
rs 10
c 0
b 0
f 0
wmc 27

4 Methods

Rating   Name   Duplication   Size   Complexity  
B import() 0 16 8
A validateDate() 0 13 4
C parseValue() 0 21 12
A export() 0 10 3
1
<?php
2
3
namespace Pagantis\OrdersApiClient\Model;
4
5
use Nayjest\StrCaseConverter\Str;
6
7
/**
8
 * Class AbstractModel
9
 *
10
 * @package Pagantis\OrdersApiClient\Model
11
 */
12
abstract class AbstractModel implements ModelInterface
13
{
14
    /**
15
     * Export as Array the object recursively
16
     *
17
     * @param bool $validation
18
     *
19
     * @return \stdClass
20
     */
21
    public function export($validation = true)
22
    {
23
        $result = new \StdClass();
24
        foreach ($this as $key => $value) {
25
            if (!is_null($value)) {
26
                $result->{Str::toSnakeCase($key)} = $this->parseValue($value, $validation, $key);
0 ignored issues
show
Unused Code introduced by
The call to Pagantis\OrdersApiClient...ractModel::parseValue() has too many arguments starting with $key. ( Ignorable by Annotation )

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

26
                /** @scrutinizer ignore-call */ 
27
                $result->{Str::toSnakeCase($key)} = $this->parseValue($value, $validation, $key);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
27
            }
28
        }
29
30
        return $result;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $result returns the type StdClass which is incompatible with the return type mandated by Pagantis\OrdersApiClient...odelInterface::export() of array.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
31
    }
32
33
    /**
34
     * Parse the value of the object depending of type.
35
     *
36
     * @param $value
37
     * @param $validation
38
     *
39
     * @return array|string
40
     */
41
    protected function parseValue($value, $validation)
42
    {
43
        if (is_array($value) && !empty($value)) {
44
            $valueArray = array();
45
            foreach ($value as $subKey => $subValue) {
46
                if (is_object($subValue) && $subValue instanceof AbstractModel) {
47
                    $valueArray[Str::toSnakeCase($subKey)] = $subValue->export($validation);
48
                } else {
49
                    $valueArray[Str::toSnakeCase($subKey)] = $subValue;
50
                }
51
            }
52
            return $valueArray;
53
        }
54
        if (is_object($value) && $value instanceof AbstractModel && !empty($value)) {
55
            return $value->export();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $value->export() returns the type stdClass which is incompatible with the documented return type array|string.
Loading history...
56
        }
57
        if (is_object($value) && $value instanceof \DateTime && !empty($value)) {
58
            return $value->format('Y-m-d\Th:i:s');
59
        }
60
61
        return $value;
62
    }
63
64
    /**
65
     * Fill Up the Order from the json_decode(false) result of the API response.
66
     *
67
     * @param \stdClass $object
68
     *
69
     * @throws \Exception
70
     */
71
    public function import($object)
72
    {
73
        if (is_object($object)) {
74
            $properties = get_object_vars($object);
75
            foreach ($properties as $key => $value) {
76
                if (property_exists($this, lcfirst(Str::toCamelCase($key)))) {
77
                    if (is_object($value)) {
78
                        $objectProperty = $this->{lcfirst(Str::toCamelCase($key))};
79
                        if ($objectProperty instanceof AbstractModel) {
80
                            $objectProperty->import($value);
81
                        }
82
                    } else {
83
                        if (is_string($value) && $this->validateDate($value)) {
84
                            $this->{lcfirst(Str::toCamelCase($key))} = new \DateTime($value);
85
                        } else {
86
                            $this->{lcfirst(Str::toCamelCase($key))} = $value;
87
                        }
88
                    }
89
                }
90
            }
91
        }
92
    }
93
94
    /**
95
     * @param $date
96
     *
97
     * @return bool
98
     */
99
    private function validateDate($date)
100
    {
101
        try {
102
            $dateTime = new \DateTime($date);
103
        } catch (\Exception $exception) {
104
            return false;
105
        }
106
107
        if ($dateTime && substr($dateTime->format('c'), 0, 19) === substr($date, 0, 19)) {
108
            return true;
109
        }
110
111
        return false;
112
    }
113
}
114