| @@ 30-125 (lines=96) @@ | ||
| 27 | /** |
|
| 28 | * @package Limoncello\Flute |
|
| 29 | */ |
|
| 30 | final class ExistInDbTableSingleWithDoctrine extends BaseRule |
|
| 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 | ||
| @@ 30-125 (lines=96) @@ | ||
| 27 | /** |
|
| 28 | * @package Limoncello\Flute |
|
| 29 | */ |
|
| 30 | final class UniqueInDbTableSingleWithDoctrine extends BaseRule |
|
| 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::UNIQUE_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 | ||