Completed
Push — master ( 0ca994...bcdd07 )
by Neomerx
05:23
created

ExistInDbTableSingleWithDoctrine::getTableName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 4
Ratio 100 %

Importance

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