SerialiserUtilities::checkElementsInput()   B
last analyzed

Complexity

Conditions 7
Paths 5

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 11
rs 8.8333
cc 7
nc 5
nop 1
1
<?php
2
3
declare(strict_types=1);
4
/**
5
 * Created by PhpStorm.
6
 * User: alex
7
 * Date: 16/02/20
8
 * Time: 1:19 PM.
9
 */
10
namespace AlgoWeb\PODataLaravel\Serialisers;
11
12
use Illuminate\Database\Eloquent\Model;
13
use Illuminate\Support\Collection;
14
use POData\Common\InvalidOperationException;
15
use POData\Common\Messages;
16
use POData\Common\ODataException;
17
use POData\Providers\Metadata\IMetadataProvider;
18
use POData\Providers\Metadata\ResourceEntityType;
19
use POData\Providers\Metadata\ResourceType;
20
use POData\Providers\Metadata\Type\IType;
21
use POData\Providers\Query\QueryResult;
22
23
abstract class SerialiserUtilities
24
{
25
    /**
26
     * @param  int  $resourceKind
27
     * @return bool
28
     */
29
    public static function isMatchPrimitive($resourceKind)
30
    {
31
        if (16 > $resourceKind) {
32
            return false;
33
        }
34
        if (28 < $resourceKind) {
35
            return false;
36
        }
37
        return 0 == ($resourceKind % 4);
38
    }
39
40
    /**
41
     * @param  QueryResult               $entryObjects
42
     * @throws InvalidOperationException
43
     */
44
    public static function checkElementsInput(QueryResult &$entryObjects): void
45
    {
46
        $res = $entryObjects->results;
47
        if (!(is_array($res) || $res instanceof Collection)) {
48
            throw new InvalidOperationException('!is_array($entryObjects->results)');
49
        }
50
        if (is_array($res) && 0 == count($res)) {
51
            $entryObjects->hasMore = false;
52
        }
53
        if ($res instanceof Collection && 0 == $res->count()) {
54
            $entryObjects->hasMore = false;
55
        }
56
    }
57
58
    /**
59
     * @param  QueryResult               $entryObject
60
     * @throws InvalidOperationException
61
     */
62
    public static function checkSingleElementInput(QueryResult $entryObject): void
63
    {
64
        if (!$entryObject->results instanceof Model) {
65
            $res = $entryObject->results;
66
            $msg = is_array($res) ? 'Entry object must be single Model' : get_class($res);
67
            throw new InvalidOperationException($msg);
68
        }
69
    }
70
71
    /**
72
     * @param  Model                     $entityInstance
73
     * @param  ResourceType              $resourceType
74
     * @param  string                    $containerName
75
     * @throws InvalidOperationException
76
     * @throws ODataException
77
     * @throws \ReflectionException
78
     * @return string
79
     */
80
    public static function getEntryInstanceKey($entityInstance, ResourceType $resourceType, $containerName)
81
    {
82
        $typeName      = $resourceType->getName();
83
        $keyProperties = $resourceType->getKeyProperties();
84
        if (0 == count($keyProperties)) {
85
            throw new InvalidOperationException('count($keyProperties) == 0');
86
        }
87
        $keyString = $containerName . '(';
88
        $comma     = null;
89
        foreach ($keyProperties as $keyName => $resourceProperty) {
90
            $keyType = $resourceProperty->getInstanceType();
91
            if (!$keyType instanceof IType) {
92
                throw new InvalidOperationException('$keyType not instanceof IType');
93
            }
94
            $keyName  = $resourceProperty->getName();
95
            $keyValue = $entityInstance->{$keyName};
96
            if (!isset($keyValue)) {
97
                throw ODataException::createInternalServerError(
98
                    Messages::badQueryNullKeysAreNotSupported($typeName, $keyName)
99
                );
100
            }
101
102
            $keyValue = $keyType->convertToOData(strval($keyValue));
103
            $keyString .= $comma . $keyName . '=' . $keyValue;
104
            $comma = ',';
105
        }
106
107
        $keyString .= ')';
108
109
        return $keyString;
110
    }
111
112
    /**
113
     * @param  ResourceEntityType              $resourceType
114
     * @param  IMetadataProvider               $metadata
115
     * @param  string                          $payloadClass
116
     * @throws InvalidOperationException
117
     * @throws \ReflectionException
118
     * @return ResourceEntityType|ResourceType
119
     */
120
    public static function getConcreteTypeFromAbstractType(
121
        ResourceEntityType $resourceType,
122
        IMetadataProvider $metadata,
123
        $payloadClass
124
    ) {
125
        if ($resourceType->isAbstract()) {
126
            $derived = $metadata->getDerivedTypes($resourceType);
127
            if (0 == count($derived)) {
128
                throw new InvalidOperationException('Supplied abstract type must have at least one derived type');
129
            }
130
            $derived = array_filter(
131
                $derived,
132
                function (ResourceType $element) {
133
                    return !$element->isAbstract();
134
                }
135
            );
136
            foreach ($derived as $rawType) {
137
                $name = $rawType->getInstanceType()->getName();
138
                if ($payloadClass == $name) {
139
                    $resourceType = $rawType;
140
                    break;
141
                }
142
            }
143
        }
144
        // despite all set up, checking, etc, if we haven't picked a concrete resource type,
145
        // wheels have fallen off, so blow up
146
        if ($resourceType->isAbstract()) {
147
            throw new InvalidOperationException('Concrete resource type not selected for payload ' . $payloadClass);
148
        }
149
        return $resourceType;
150
    }
151
}
152