Completed
Push — develop ( e8233d...5c8fb2 )
by Neomerx
16:15 queued 11:26
created

IsReadableViaApiRule::__construct()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 11
loc 11
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 1
nop 1
crap 2
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\Api\CrudInterface;
20
use Limoncello\Flute\Contracts\FactoryInterface;
21
use Limoncello\Flute\Contracts\Validation\ErrorCodes;
22
use Limoncello\Validation\Contracts\Execution\ContextInterface;
23
use Limoncello\Validation\Rules\ExecuteRule;
24
use Psr\Container\ContainerExceptionInterface;
25
use Psr\Container\NotFoundExceptionInterface;
26
27
/**
28
 * @package Limoncello\Flute
29
 */
30 View Code Duplication
final class IsReadableViaApiRule extends ExecuteRule
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
31
{
32
    /**
33
     * Property key.
34
     */
35
    const PROPERTY_API_CLASS = self::PROPERTY_LAST + 1;
36
37
    /**
38
     * @param string $apiClass
39
     */
40 29
    public function __construct(string $apiClass)
41
    {
42 29
        assert(
43 29
            class_exists($apiClass) === true &&
44 29
            array_key_exists(CrudInterface::class, class_implements($apiClass)) === true
45
        );
46
47 29
        parent::__construct([
48 29
            static::PROPERTY_API_CLASS => $apiClass,
49
        ]);
50
    }
51
52
    /**
53
     * @param mixed            $value
54
     * @param ContextInterface $context
55
     *
56
     * @return array
57
     *
58
     * @SuppressWarnings(PHPMD.StaticAccess)
59
     *
60
     * @throws ContainerExceptionInterface
61
     * @throws NotFoundExceptionInterface
62
     */
63 1
    public static function execute($value, ContextInterface $context): array
64
    {
65 1
        assert(is_int($value) || is_string($value));
66
67 1
        $apiClass = $context->getProperties()->getProperty(static::PROPERTY_API_CLASS);
68
69
        /** @var FactoryInterface $apiFactory */
70 1
        $apiFactory = $context->getContainer()->get(FactoryInterface::class);
71
72
        /** @var CrudInterface $api */
73 1
        $api = $apiFactory->createApi($apiClass);
74
75 1
        $data   = $api->withIndexFilter($value)->indexIdentities();
76 1
        $result = !empty($data);
77
78 1
        return $result === true ?
79 1
            static::createSuccessReply($value) :
80 1
            static::createErrorReply($context, $value, ErrorCodes::EXIST_IN_DATABASE_SINGLE);
81
    }
82
}
83