Completed
Push — master ( fb30fb...b9dc46 )
by Neomerx
13:06
created

ToManyRelationshipTypeChecker::execute()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 27
c 0
b 0
f 0
ccs 17
cts 17
cp 1
rs 8.439
cc 6
eloc 18
nc 6
nop 2
crap 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\Blocks\ProcedureBlock;
21
use Limoncello\Validation\Contracts\Blocks\ExecutionBlockInterface;
22
use Limoncello\Validation\Contracts\Execution\ContextInterface;
23
use Limoncello\Validation\Execution\BlockReplies;
24
use Limoncello\Validation\Rules\BaseRule;
25
26
/**
27
 * @package Limoncello\Flute
28
 */
29
final class ToManyRelationshipTypeChecker extends BaseRule
30
{
31
    /**
32
     * Property key.
33
     */
34
    const PROPERTY_RESOURCE_TYPE = self::PROPERTY_LAST + 1;
35
36
    /**
37
     * @var string
38
     */
39
    private $type;
40
41
    /**
42
     * @param string $type
43
     */
44 36
    public function __construct(string $type)
45
    {
46 36
        $this->type = $type;
47
    }
48
49
    /**
50
     * @inheritdoc
51
     */
52 36
    public function toBlock(): ExecutionBlockInterface
53
    {
54 36
        return (new ProcedureBlock([self::class, 'execute']))
55 36
            ->setProperties($this->getStandardProperties() + [self::PROPERTY_RESOURCE_TYPE => $this->getType()]);
56
    }
57
58
    /**
59
     * @param mixed            $value
60
     * @param ContextInterface $context
61
     *
62
     * @return array
63
     *
64
     * @SuppressWarnings(PHPMD.StaticAccess)
65
     * @SuppressWarnings(PHPMD.ElseExpression)
66
     */
67 5
    public static function execute($value, ContextInterface $context): array
68
    {
69
        // parser guarantees that input will be an array of [$type => $id] where type and id are scalars
70
71
        // we will check the type of every pair and send further identities only
72 5
        $indexes          = [];
73 5
        $foundInvalidType = null;
74 5
        $expectedType     = $context->getProperties()->getProperty(self::PROPERTY_RESOURCE_TYPE);
75 5
        foreach ($value as $typeAndId) {
76 4
            assert(is_array($typeAndId) === true && count($typeAndId) === 1);
77 4
            $index = reset($typeAndId);
78 4
            $type  = key($typeAndId);
79 4
            assert(is_scalar($index) === true && is_scalar($type) === true);
80 4
            if ($type === $expectedType) {
81 3
                $indexes[] = $index;
82
            } else {
83 1
                $foundInvalidType = $type;
84 4
                break;
85
            }
86
        }
87
88 5
        $reply = $foundInvalidType === null ?
89 4
            BlockReplies::createSuccessReply($indexes) :
90 5
            BlockReplies::createErrorReply($context, $foundInvalidType, ErrorCodes::INVALID_RELATIONSHIP_TYPE);
91
92 5
        return $reply;
93
    }
94
95
    /**
96
     * @return string
97
     */
98 36
    public function getType(): string
99
    {
100 36
        return $this->type;
101
    }
102
}
103