UniqueInDbTableSingleWithDoctrineRule   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 67
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 7 7 1
A execute() 31 31 3

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 declare (strict_types = 1);
2
3
namespace Limoncello\Flute\Validation\JsonApi\Rules;
4
5
/**
6
 * Copyright 2015-2019 [email protected]
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 * http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
use Doctrine\DBAL\Connection;
22
use Limoncello\Flute\Contracts\Validation\ErrorCodes;
23
use Limoncello\Flute\L10n\Messages;
24
use Limoncello\Validation\Contracts\Execution\ContextInterface;
25
use Limoncello\Validation\Rules\ExecuteRule;
26
use Psr\Container\ContainerExceptionInterface;
27
use Psr\Container\NotFoundExceptionInterface;
28
use function is_scalar;
29
30
/**
31
 * @package Limoncello\Flute
32
 */
33 View Code Duplication
final class UniqueInDbTableSingleWithDoctrineRule 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...
34
{
35
    /**
36
     * Property key.
37
     */
38
    const PROPERTY_TABLE_NAME = self::PROPERTY_LAST + 1;
39
40
    /**
41
     * Property key.
42
     */
43
    const PROPERTY_PRIMARY_NAME = self::PROPERTY_TABLE_NAME + 1;
44
45
    /**
46
     * @param string $tableName
47
     * @param string $primaryName
48 2
     */
49
    public function __construct(string $tableName, string $primaryName)
50 2
    {
51 2
        parent::__construct([
52 2
            static::PROPERTY_TABLE_NAME   => $tableName,
53
            static::PROPERTY_PRIMARY_NAME => $primaryName,
54
        ]);
55
    }
56
57
    /**
58
     * @param mixed            $value
59
     * @param ContextInterface $context
60
     *
61
     * @return array
62
     *
63
     * @SuppressWarnings(PHPMD.StaticAccess)
64
     *
65
     * @throws ContainerExceptionInterface
66
     * @throws NotFoundExceptionInterface
67 1
     */
68
    public static function execute($value, ContextInterface $context): array
69 1
    {
70
        $count = 0;
71 1
72
        if (is_scalar($value) === true) {
73 1
            /** @var Connection $connection */
74 1
            $connection  = $context->getContainer()->get(Connection::class);
75 1
            $builder     = $connection->createQueryBuilder();
76 1
            $tableName   = $context->getProperties()->getProperty(static::PROPERTY_TABLE_NAME);
77
            $primaryName = $context->getProperties()->getProperty(static::PROPERTY_PRIMARY_NAME);
78 1
            $statement   = $builder
79 1
                ->select('count(*)')
80 1
                ->from($tableName)
81 1
                ->where($builder->expr()->eq($primaryName, $builder->createPositionalParameter($value)))
82
                ->execute();
83 1
84
            $count = $statement->fetchColumn();
85
        }
86 1
87 1
        $reply = $count <= 0 ?
88 1
            static::createSuccessReply($value) :
89 1
            static::createErrorReply(
90 1
                $context,
91 1
                $value,
92 1
                ErrorCodes::UNIQUE_IN_DATABASE_SINGLE,
93 1
                Messages::UNIQUE_IN_DATABASE_SINGLE,
94
                []
95
            );
96 1
97
        return $reply;
98
    }
99
}
100