Completed
Push — develop ( a2b4be...d107a0 )
by Neomerx
03:39 queued 01:56
created

UniqueInDbTableSingleWithDoctrine   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 96
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 8
dl 96
loc 96
ccs 28
cts 28
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 5 5 1
A toBlock() 10 10 1
B execute() 25 25 3
A getTableName() 4 4 1
A getPrimaryName() 4 4 1

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\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 Doctrine\DBAL\Connection;
20
use Limoncello\Flute\Contracts\Validation\ErrorCodes;
21
use Limoncello\Validation\Blocks\ProcedureBlock;
22
use Limoncello\Validation\Contracts\Blocks\ExecutionBlockInterface;
23
use Limoncello\Validation\Contracts\Execution\ContextInterface;
24
use Limoncello\Validation\Execution\BlockReplies;
25
use Limoncello\Validation\Rules\BaseRule;
26
27
/**
28
 * @package Limoncello\Flute
29
 */
30 View Code Duplication
final class UniqueInDbTableSingleWithDoctrine extends BaseRule
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_TABLE_NAME = self::PROPERTY_LAST + 1;
36
37
    /**
38
     * Property key.
39
     */
40
    const PROPERTY_PRIMARY_NAME = self::PROPERTY_TABLE_NAME + 1;
41
42
    /**
43
     * @var string
44
     */
45
    private $tableName;
46
47
    /**
48
     * @var string
49
     */
50
    private $primaryName;
51
52
    /**
53
     * @param string $tableName
54
     * @param string $primaryName
55
     */
56 2
    public function __construct(string $tableName, string $primaryName)
57
    {
58 2
        $this->tableName   = $tableName;
59 2
        $this->primaryName = $primaryName;
60
    }
61
62
    /**
63
     * @inheritdoc
64
     */
65 2
    public function toBlock(): ExecutionBlockInterface
66
    {
67
        $customProperties = [
68 2
            self::PROPERTY_TABLE_NAME   => $this->getTableName(),
69 2
            self::PROPERTY_PRIMARY_NAME => $this->getPrimaryName(),
70
        ];
71
72 2
        return (new ProcedureBlock([self::class, 'execute']))
73 2
            ->setProperties($this->getStandardProperties() + $customProperties);
74
    }
75
76
    /**
77
     * @param mixed            $value
78
     * @param ContextInterface $context
79
     *
80
     * @return array
81
     *
82
     * @SuppressWarnings(PHPMD.StaticAccess)
83
     */
84 1
    public static function execute($value, ContextInterface $context): array
85
    {
86 1
        $count = 0;
87
88 1
        if (is_scalar($value) === true) {
89
            /** @var Connection $connection */
90 1
            $connection  = $context->getContainer()->get(Connection::class);
0 ignored issues
show
Bug introduced by
The method getContainer() does not seem to exist on object<Limoncello\Valida...ution\ContextInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
91 1
            $builder     = $connection->createQueryBuilder();
92 1
            $tableName   = $context->getProperties()->getProperty(self::PROPERTY_TABLE_NAME);
93 1
            $primaryName = $context->getProperties()->getProperty(self::PROPERTY_PRIMARY_NAME);
94
            $statement   = $builder
95 1
                ->select('count(*)')
96 1
                ->from($tableName)
97 1
                ->where($builder->expr()->eq($primaryName, $builder->createPositionalParameter($value)))
98 1
                ->execute();
99
100 1
            $count = $statement->fetchColumn();
101
        }
102
103 1
        $reply = $count <= 0 ?
104 1
            BlockReplies::createSuccessReply($value) :
105 1
            BlockReplies::createErrorReply($context, $value, ErrorCodes::UNIQUE_IN_DATABASE_SINGLE);
106
107 1
        return $reply;
108
    }
109
110
    /**
111
     * @return string
112
     */
113 2
    public function getTableName(): string
114
    {
115 2
        return $this->tableName;
116
    }
117
118
    /**
119
     * @return string
120
     */
121 2
    public function getPrimaryName(): string
122
    {
123 2
        return $this->primaryName;
124
    }
125
}
126