Completed
Push — develop ( dcfc04...3d10a6 )
by Neomerx
04:33
created

ExistInDbTableSingleWithDoctrineRule   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 62
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 5
dl 62
loc 62
ccs 0
cts 27
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 7 7 1
A execute() 26 26 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 namespace Limoncello\Flute\Validation\JsonApi\Rules;
2
3
/**
4
 * Copyright 2015-2018 [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 Doctrine\DBAL\Connection;
20
use Limoncello\Flute\Contracts\Validation\ErrorCodes;
21
use Limoncello\Validation\Contracts\Execution\ContextInterface;
22
use Limoncello\Validation\Rules\ExecuteRule;
23
use Psr\Container\ContainerExceptionInterface;
24
use Psr\Container\NotFoundExceptionInterface;
25
26
/**
27
 * @package Limoncello\Flute
28
 */
29 View Code Duplication
final class ExistInDbTableSingleWithDoctrineRule 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...
30
{
31
    /**
32
     * Property key.
33
     */
34
    const PROPERTY_TABLE_NAME = self::PROPERTY_LAST + 1;
35
36
    /**
37
     * Property key.
38
     */
39
    const PROPERTY_PRIMARY_NAME = self::PROPERTY_TABLE_NAME + 1;
40
41
    /**
42
     * @param string $tableName
43
     * @param string $primaryName
44
     */
45
    public function __construct(string $tableName, string $primaryName)
46
    {
47
        parent::__construct([
48
            static::PROPERTY_TABLE_NAME   => $tableName,
49
            static::PROPERTY_PRIMARY_NAME => $primaryName,
50
        ]);
51
    }
52
53
    /**
54
     * @param mixed            $value
55
     * @param ContextInterface $context
56
     *
57
     * @return array
58
     *
59
     * @SuppressWarnings(PHPMD.StaticAccess)
60
     *
61
     * @throws ContainerExceptionInterface
62
     * @throws NotFoundExceptionInterface
63
     */
64
    public static function execute($value, ContextInterface $context): array
65
    {
66
        $count = 0;
67
68
        if (is_scalar($value) === true) {
69
            $tableName   = $context->getProperties()->getProperty(static::PROPERTY_TABLE_NAME);
70
            $primaryName = $context->getProperties()->getProperty(static::PROPERTY_PRIMARY_NAME);
71
72
            /** @var Connection $connection */
73
            $connection = $context->getContainer()->get(Connection::class);
74
            $builder    = $connection->createQueryBuilder();
75
            $statement  = $builder
76
                ->select('count(*)')
77
                ->from($tableName)
78
                ->where($builder->expr()->eq($primaryName, $builder->createPositionalParameter($value)))
79
                ->execute();
80
81
            $count = $statement->fetchColumn();
82
        }
83
84
        $reply = $count > 0 ?
85
            static::createSuccessReply($value) :
86
            static::createErrorReply($context, $value, ErrorCodes::EXIST_IN_DATABASE_SINGLE);
87
88
        return $reply;
89
    }
90
}
91