Completed
Push — master ( dfab50...8f8d73 )
by Neomerx
05:14
created

ToManyRelationshipTypeCheckerRule   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 3
dl 0
loc 54
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B execute() 0 27 6
1
<?php namespace Limoncello\Flute\Validation\JsonApi\Rules;
2
3
/**
4
 * Copyright 2015-2017 [email protected]
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
use Limoncello\Flute\Contracts\Validation\ErrorCodes;
20
use Limoncello\Validation\Contracts\Execution\ContextInterface;
21
use Limoncello\Validation\Rules\ExecuteRule;
22
23
/**
24
 * @package Limoncello\Flute
25
 */
26
final class ToManyRelationshipTypeCheckerRule extends ExecuteRule
27
{
28
    /**
29
     * Property key.
30
     */
31
    const PROPERTY_RESOURCE_TYPE = self::PROPERTY_LAST + 1;
32
33
    /**
34
     * @param string $type
35
     */
36 36
    public function __construct(string $type)
37
    {
38 36
        parent::__construct([
39 36
            static::PROPERTY_RESOURCE_TYPE => $type,
40
        ]);
41
    }
42
43
    /**
44
     * @param mixed            $value
45
     * @param ContextInterface $context
46
     *
47
     * @return array
48
     *
49
     * @SuppressWarnings(PHPMD.StaticAccess)
50
     * @SuppressWarnings(PHPMD.ElseExpression)
51
     */
52 5
    public static function execute($value, ContextInterface $context): array
53
    {
54
        // parser guarantees that input will be an array of [$type => $id] where type and id are scalars
55
56
        // we will check the type of every pair and send further identities only
57 5
        $indexes          = [];
58 5
        $foundInvalidType = null;
59 5
        $expectedType     = $context->getProperties()->getProperty(static::PROPERTY_RESOURCE_TYPE);
60 5
        foreach ($value as $typeAndId) {
61 4
            assert(is_array($typeAndId) === true && count($typeAndId) === 1);
62 4
            $index = reset($typeAndId);
63 4
            $type  = key($typeAndId);
64 4
            assert(is_scalar($index) === true && is_scalar($type) === true);
65 4
            if ($type === $expectedType) {
66 3
                $indexes[] = $index;
67
            } else {
68 1
                $foundInvalidType = $type;
69 4
                break;
70
            }
71
        }
72
73 5
        $reply = $foundInvalidType === null ?
74 4
            static::createSuccessReply($indexes) :
75 5
            static::createErrorReply($context, $foundInvalidType, ErrorCodes::INVALID_RELATIONSHIP_TYPE);
76
77 5
        return $reply;
78
    }
79
}
80