@@ 29-90 (lines=62) @@ | ||
26 | /** |
|
27 | * @package Limoncello\Flute |
|
28 | */ |
|
29 | final class ExistInDbTableSingleWithDoctrineRule 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 $value |
|
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($value, ContextInterface $context): array |
|
65 | { |
|
66 | $count = 0; |
|
67 | ||
68 | if (is_scalar($value) === true) { |
|
69 | $tableName = $context->getProperties()->getProperty(static::PROPERTY_TABLE_NAME); |
|
70 | $primaryName = $context->getProperties()->getProperty(static::PROPERTY_PRIMARY_NAME); |
|
71 | ||
72 | /** @var Connection $connection */ |
|
73 | $connection = $context->getContainer()->get(Connection::class); |
|
74 | $builder = $connection->createQueryBuilder(); |
|
75 | $statement = $builder |
|
76 | ->select('count(*)') |
|
77 | ->from($tableName) |
|
78 | ->where($builder->expr()->eq($primaryName, $builder->createPositionalParameter($value))) |
|
79 | ->execute(); |
|
80 | ||
81 | $count = $statement->fetchColumn(); |
|
82 | } |
|
83 | ||
84 | $reply = $count > 0 ? |
|
85 | static::createSuccessReply($value) : |
|
86 | static::createErrorReply($context, $value, ErrorCodes::EXIST_IN_DATABASE_SINGLE); |
|
87 | ||
88 | return $reply; |
|
89 | } |
|
90 | } |
|
91 |
@@ 29-89 (lines=61) @@ | ||
26 | /** |
|
27 | * @package Limoncello\Flute |
|
28 | */ |
|
29 | final class UniqueInDbTableSingleWithDoctrineRule 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 $value |
|
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($value, ContextInterface $context): array |
|
65 | { |
|
66 | $count = 0; |
|
67 | ||
68 | if (is_scalar($value) === true) { |
|
69 | /** @var Connection $connection */ |
|
70 | $connection = $context->getContainer()->get(Connection::class); |
|
71 | $builder = $connection->createQueryBuilder(); |
|
72 | $tableName = $context->getProperties()->getProperty(static::PROPERTY_TABLE_NAME); |
|
73 | $primaryName = $context->getProperties()->getProperty(static::PROPERTY_PRIMARY_NAME); |
|
74 | $statement = $builder |
|
75 | ->select('count(*)') |
|
76 | ->from($tableName) |
|
77 | ->where($builder->expr()->eq($primaryName, $builder->createPositionalParameter($value))) |
|
78 | ->execute(); |
|
79 | ||
80 | $count = $statement->fetchColumn(); |
|
81 | } |
|
82 | ||
83 | $reply = $count <= 0 ? |
|
84 | static::createSuccessReply($value) : |
|
85 | static::createErrorReply($context, $value, ErrorCodes::UNIQUE_IN_DATABASE_SINGLE); |
|
86 | ||
87 | return $reply; |
|
88 | } |
|
89 | } |
|
90 |