Test Failed
Push — main ( f1b82a...d2d042 )
by Bingo
06:24
created

JsonQueryOrderingPropertyConverter   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
eloc 52
c 1
b 0
f 0
dl 0
loc 94
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A arrayConverter() 0 6 2
A instance() 0 6 2
A toJsonObject() 0 24 4
B toObject() 0 39 7
1
<?php
2
3
namespace Jabe\Engine\Impl\Json;
4
5
use Jabe\Engine\Impl\{
6
    Direction,
7
    QueryEntityRelationCondition,
8
    QueryOrderingProperty,
9
    VariableOrderProperty
10
};
11
use Jabe\Engine\Impl\Util\JsonUtil;
12
13
class JsonQueryOrderingPropertyConverter extends JsonObjectConverter
14
{
15
16
    protected static $INSTANCE;
17
18
    protected static $ARRAY_CONVERTER;
19
20
    public const RELATION = "relation";
21
    public const QUERY_PROPERTY = "queryProperty";
22
    public const QUERY_PROPERTY_FUNCTION = "queryPropertyFunction";
23
    public const DIRECTION = "direction";
24
    public const RELATION_CONDITIONS = "relationProperties";
25
26
    public static function instance(): JsonQueryOrderingPropertyConverter
27
    {
28
        if (self::$INSTANCE === null) {
29
            self::$INSTANCE = new JsonQueryOrderingPropertyConverter();
30
        }
31
        return self::$INSTANCE;
32
    }
33
34
    public static function arrayConverter(): JsonArrayOfObjectsConverter
35
    {
36
        if (self::$ARRAY_CONVERTER === null) {
37
            self::$ARRAY_CONVERTER = new JsonArrayOfObjectsConverter(self::instance());
38
        }
39
        return self::$ARRAY_CONVERTER;
40
    }
41
42
    public function toJsonObject(/*QueryOrderingProperty*/$property, bool $isOrQueryActive = false): ?\stdClass
43
    {
44
        $jsonObject = JsonUtil::createObject();
45
46
        JsonUtil::addField($jsonObject, self::RELATION, $property->getRelation());
47
48
        $queryProperty = $property->getQueryProperty();
49
        if ($queryProperty !== null) {
50
            JsonUtil::addField($jsonObject, self::QUERY_PROPERTY, $queryProperty->getName());
51
            JsonUtil::addField($jsonObject, self::QUERY_PROPERTY_FUNCTION, $queryProperty->getFunction());
52
        }
53
54
        $direction = $property->getDirection();
55
        if ($direction !== null) {
56
            JsonUtil::addField($jsonObject, self::DIRECTION, $direction->getName());
57
        }
58
59
        if ($property->hasRelationConditions()) {
60
            $relationConditionsJson = JsonQueryFilteringPropertyConverter::arrayConverter()
61
                                      ->toJsonArray($property->getRelationConditions());
62
            JsonUtil::addField($jsonObject, self::RELATION_CONDITIONS, $relationConditionsJson);
63
        }
64
65
        return $jsonObject;
66
    }
67
68
    public function toObject(\stdClass $jsonObject, bool $isOrQuery = false)
69
    {
70
        $relation = null;
71
        if (property_exists($jsonObject, self::RELATION)) {
72
            $relation = JsonUtil::getString($jsonObject, self::RELATION);
73
        }
74
75
        $property = null;
76
        if (QueryOrderingProperty::RELATION_VARIABLE == $relation) {
77
            $property = new VariableOrderProperty();
78
        } else {
79
            $property = new QueryOrderingProperty();
80
        }
81
82
        $property->setRelation($relation);
0 ignored issues
show
Bug introduced by
It seems like $relation can also be of type null; however, parameter $relation of Jabe\Engine\Impl\QueryOr...Property::setRelation() does only seem to accept string, 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

82
        $property->setRelation(/** @scrutinizer ignore-type */ $relation);
Loading history...
83
84
        if (property_exists($jsonObject, self::QUERY_PROPERTY)) {
85
            $propertyName = JsonUtil::getString($jsonObject, self::QUERY_PROPERTY);
86
            $propertyFunction = null;
87
            if (property_exists($jsonObject, self::QUERY_PROPERTY_FUNCTION)) {
88
                $propertyFunction = JsonUtil::getString($jsonObject, self::QUERY_PROPERTY_FUNCTION);
89
            }
90
91
            $queryProperty = new QueryPropertyImpl($propertyName, $propertyFunction);
92
            $property->setQueryProperty($queryProperty);
93
        }
94
95
        if (property_exists($jsonObject, self::DIRECTION)) {
96
            $direction = JsonUtil::getString($jsonObject, self::DIRECTION);
97
            $property->setDirection(Direction::findByName($direction));
0 ignored issues
show
Bug introduced by
It seems like Jabe\Engine\Impl\Direction::findByName($direction) can also be of type null; however, parameter $direction of Jabe\Engine\Impl\QueryOr...roperty::setDirection() does only seem to accept Jabe\Engine\Impl\Direction, 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

97
            $property->setDirection(/** @scrutinizer ignore-type */ Direction::findByName($direction));
Loading history...
98
        }
99
100
        if (property_exists($jsonObject, self::RELATION_CONDITIONS)) {
101
            $relationConditions =
102
                JsonQueryFilteringPropertyConverter::arrayConverter()->toObject(JsonUtil::getArray($jsonObject, self::RELATION_CONDITIONS));
103
            $property->setRelationConditions($relationConditions);
104
        }
105
106
        return $property;
107
    }
108
}
109