Code Duplication    Length = 67-68 lines in 2 locations

src/Validation/JsonApi/Rules/ExistInDbTableSingleWithDoctrineRule.php 1 location

@@ 32-99 (lines=68) @@
29
/**
30
 * @package Limoncello\Flute
31
 */
32
final class ExistInDbTableSingleWithDoctrineRule extends ExecuteRule
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
    public function __construct(string $tableName, string $primaryName)
49
    {
50
        parent::__construct([
51
            static::PROPERTY_TABLE_NAME   => $tableName,
52
            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
    public static function execute($value, ContextInterface $context): array
68
    {
69
        $count = 0;
70
71
        if (is_scalar($value) === true) {
72
            $tableName   = $context->getProperties()->getProperty(static::PROPERTY_TABLE_NAME);
73
            $primaryName = $context->getProperties()->getProperty(static::PROPERTY_PRIMARY_NAME);
74
75
            /** @var Connection $connection */
76
            $connection = $context->getContainer()->get(Connection::class);
77
            $builder    = $connection->createQueryBuilder();
78
            $statement  = $builder
79
                ->select('count(*)')
80
                ->from($tableName)
81
                ->where($builder->expr()->eq($primaryName, $builder->createPositionalParameter($value)))
82
                ->execute();
83
84
            $count = $statement->fetchColumn();
85
        }
86
87
        $reply = $count > 0 ?
88
            static::createSuccessReply($value) :
89
            static::createErrorReply(
90
                $context,
91
                $value,
92
                ErrorCodes::EXIST_IN_DATABASE_SINGLE,
93
                Messages::EXIST_IN_DATABASE_SINGLE,
94
                []
95
            );
96
97
        return $reply;
98
    }
99
}
100

src/Validation/JsonApi/Rules/UniqueInDbTableSingleWithDoctrineRule.php 1 location

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