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

ExistInDbTableMultipleWithDoctrineRule   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 5
dl 0
loc 68
ccs 0
cts 32
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A execute() 0 32 5
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
final class ExistInDbTableMultipleWithDoctrineRule extends ExecuteRule
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            $values
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($values, ContextInterface $context): array
65
    {
66
        // let's consider an empty index list as `exists`
67
        $result = is_array($values);
68
69
        if ($result === true && empty($values) === false) {
70
            $tableName   = $context->getProperties()->getProperty(static::PROPERTY_TABLE_NAME);
71
            $primaryName = $context->getProperties()->getProperty(static::PROPERTY_PRIMARY_NAME);
72
73
            /** @var Connection $connection */
74
            $connection   = $context->getContainer()->get(Connection::class);
75
            $builder      = $connection->createQueryBuilder();
76
            $placeholders = [];
77
            foreach ($values as $value) {
78
                $placeholders[] = $builder->createPositionalParameter($value);
79
            }
80
            $statement = $builder
81
                ->select('count(*)')
82
                ->from($tableName)
83
                ->where($builder->expr()->in($primaryName, $placeholders))
84
                ->execute();
85
86
            $count  = (int)$statement->fetchColumn();
87
            $result = $count === count($values);
88
        }
89
90
        $reply = $result === true ?
91
            static::createSuccessReply($values) :
92
            static::createErrorReply($context, $values, ErrorCodes::EXIST_IN_DATABASE_MULTIPLE);
93
94
        return $reply;
95
    }
96
}
97