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

ExistInDbTableSingleWithDoctrine::execute()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 26
Code Lines 17

Duplication

Lines 26
Ratio 100 %

Code Coverage

Tests 16
CRAP Score 3

Importance

Changes 0
Metric Value
dl 26
loc 26
ccs 16
cts 16
cp 1
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 17
nc 4
nop 2
crap 3
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 ExistInDbTableSingleWithDoctrine 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 27
    public function __construct(string $tableName, string $primaryName)
57
    {
58 27
        $this->tableName   = $tableName;
59 27
        $this->primaryName = $primaryName;
60
    }
61
62
    /**
63
     * @inheritdoc
64
     */
65 27
    public function toBlock(): ExecutionBlockInterface
66
    {
67
        $customProperties = [
68 27
            self::PROPERTY_TABLE_NAME   => $this->getTableName(),
69 27
            self::PROPERTY_PRIMARY_NAME => $this->getPrimaryName(),
70
        ];
71
72 27
        return (new ProcedureBlock([self::class, 'execute']))
73 27
            ->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 5
    public static function execute($value, ContextInterface $context): array
85
    {
86 5
        $count = 0;
87
88 5
        if (is_scalar($value) === true) {
89 5
            $tableName   = $context->getProperties()->getProperty(self::PROPERTY_TABLE_NAME);
90 5
            $primaryName = $context->getProperties()->getProperty(self::PROPERTY_PRIMARY_NAME);
91
92
            /** @var Connection $connection */
93 5
            $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...
94 5
            $builder     = $connection->createQueryBuilder();
95
            $statement   = $builder
96 5
                ->select('count(*)')
97 5
                ->from($tableName)
98 5
                ->where($builder->expr()->eq($primaryName, $builder->createPositionalParameter($value)))
99 5
                ->execute();
100
101 5
            $count = $statement->fetchColumn();
102
        }
103
104 5
        $reply = $count > 0 ?
105 4
            BlockReplies::createSuccessReply($value) :
106 5
            BlockReplies::createErrorReply($context, $value, ErrorCodes::EXIST_IN_DATABASE_SINGLE);
107
108 5
        return $reply;
109
    }
110
111
    /**
112
     * @return string
113
     */
114 27
    public function getTableName(): string
115
    {
116 27
        return $this->tableName;
117
    }
118
119
    /**
120
     * @return string
121
     */
122 27
    public function getPrimaryName(): string
123
    {
124 27
        return $this->primaryName;
125
    }
126
}
127