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

AreReadableViaApiRule   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 54
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 5
dl 54
loc 54
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 11 11 2
A execute() 20 20 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 AreReadableViaApiRule 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            $values
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($values, ContextInterface $context): array
64
    {
65 1
        assert(is_array($values));
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
        $readIndexes = $api->withIndexesFilter($values)->indexIdentities();
76
77 1
        $result = count($readIndexes) === count($values);
78
79 1
        return $result === true ?
80 1
            static::createSuccessReply($values) :
81 1
            static::createErrorReply($context, $values, ErrorCodes::EXIST_IN_DATABASE_MULTIPLE);
82
    }
83
}
84