__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 7
Ratio 100 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 7
loc 7
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
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
29
/**
30
 * @package Limoncello\Flute
31
 */
32 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...
33
{
34
    /**
35
     * Property key.
36
     */
37
    const PROPERTY_TABLE_NAME = self::PROPERTY_LAST + 1;
38
39
    /**
40
     * Property key.
41
     */
42
    const PROPERTY_PRIMARY_NAME = self::PROPERTY_TABLE_NAME + 1;
43
44
    /**
45
     * @param string $tableName
46
     * @param string $primaryName
47
     */
48 32
    public function __construct(string $tableName, string $primaryName)
49
    {
50 32
        parent::__construct([
51 32
            static::PROPERTY_TABLE_NAME   => $tableName,
52 32
            static::PROPERTY_PRIMARY_NAME => $primaryName,
53
        ]);
54
    }
55
56
    /**
57
     * @param mixed            $value
58
     * @param ContextInterface $context
59
     *
60
     * @return array
61
     *
62
     * @SuppressWarnings(PHPMD.StaticAccess)
63
     *
64
     * @throws ContainerExceptionInterface
65
     * @throws NotFoundExceptionInterface
66
     */
67 4
    public static function execute($value, ContextInterface $context): array
68
    {
69 4
        $count = 0;
70
71 4
        if (is_scalar($value) === true) {
72 4
            $tableName   = $context->getProperties()->getProperty(static::PROPERTY_TABLE_NAME);
73 4
            $primaryName = $context->getProperties()->getProperty(static::PROPERTY_PRIMARY_NAME);
74
75
            /** @var Connection $connection */
76 4
            $connection = $context->getContainer()->get(Connection::class);
77 4
            $builder    = $connection->createQueryBuilder();
78
            $statement  = $builder
79 4
                ->select('count(*)')
80 4
                ->from($tableName)
81 4
                ->where($builder->expr()->eq($primaryName, $builder->createPositionalParameter($value)))
82 4
                ->execute();
83
84 4
            $count = $statement->fetchColumn();
85
        }
86
87 4
        $reply = $count > 0 ?
88 3
            static::createSuccessReply($value) :
89 1
            static::createErrorReply(
90 1
                $context,
91 1
                $value,
92 1
                ErrorCodes::EXIST_IN_DATABASE_SINGLE,
93 1
                Messages::EXIST_IN_DATABASE_SINGLE,
94 4
                []
95
            );
96
97 4
        return $reply;
98
    }
99
}
100