| 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 |  |  | use function count; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | use function is_array; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  * @package Limoncello\Flute | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | final class ExistInDbTableMultipleWithDoctrineRule extends ExecuteRule | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * Property key. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     const PROPERTY_TABLE_NAME = self::PROPERTY_LAST + 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * Property key. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     const PROPERTY_PRIMARY_NAME = self::PROPERTY_TABLE_NAME + 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @param string $tableName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 32 |  |      * @param string $primaryName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 32 |  |     public function __construct(string $tableName, string $primaryName) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 32 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 32 |  |         parent::__construct([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             static::PROPERTY_TABLE_NAME   => $tableName, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             static::PROPERTY_PRIMARY_NAME => $primaryName, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * @param mixed            $values | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * @param ContextInterface $context | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * @SuppressWarnings(PHPMD.StaticAccess) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * @throws ContainerExceptionInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 5 |  |      * @throws NotFoundExceptionInterface | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 68 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 69 |  |  |     public static function execute($values, ContextInterface $context): array | 
            
                                                        
            
                                    
            
            
                | 70 | 5 |  |     { | 
            
                                                        
            
                                    
            
            
                | 71 |  |  |         // let's consider an empty index list as `exists` | 
            
                                                        
            
                                    
            
            
                | 72 | 5 |  |         $result = is_array($values); | 
            
                                                        
            
                                    
            
            
                | 73 | 5 |  |  | 
            
                                                        
            
                                    
            
            
                | 74 | 5 |  |         if ($result === true && empty($values) === false) { | 
            
                                                        
            
                                    
            
            
                | 75 |  |  |             $tableName   = $context->getProperties()->getProperty(static::PROPERTY_TABLE_NAME); | 
            
                                                        
            
                                    
            
            
                | 76 |  |  |             $primaryName = $context->getProperties()->getProperty(static::PROPERTY_PRIMARY_NAME); | 
            
                                                        
            
                                    
            
            
                | 77 | 5 |  |  | 
            
                                                        
            
                                    
            
            
                | 78 | 5 |  |             /** @var Connection $connection */ | 
            
                                                        
            
                                    
            
            
                | 79 | 5 |  |             $connection   = $context->getContainer()->get(Connection::class); | 
            
                                                        
            
                                    
            
            
                | 80 | 5 |  |             $builder      = $connection->createQueryBuilder(); | 
            
                                                        
            
                                    
            
            
                | 81 | 5 |  |             $placeholders = []; | 
            
                                                        
            
                                    
            
            
                | 82 |  |  |             foreach ($values as $value) { | 
            
                                                        
            
                                    
            
            
                | 83 |  |  |                 $placeholders[] = $builder->createPositionalParameter($value); | 
            
                                                        
            
                                    
            
            
                | 84 | 5 |  |             } | 
            
                                                        
            
                                    
            
            
                | 85 | 5 |  |             $statement = $builder | 
            
                                                        
            
                                    
            
            
                | 86 | 5 |  |                 ->select('count(*)') | 
            
                                                        
            
                                    
            
            
                | 87 | 5 |  |                 ->from($tableName) | 
            
                                                        
            
                                    
            
            
                | 88 |  |  |                 ->where($builder->expr()->in($primaryName, $placeholders)) | 
            
                                                        
            
                                    
            
            
                | 89 | 5 |  |                 ->execute(); | 
            
                                                        
            
                                    
            
            
                | 90 | 5 |  |  | 
            
                                                        
            
                                    
            
            
                | 91 |  |  |             $count  = (int)$statement->fetchColumn(); | 
            
                                                        
            
                                    
            
            
                | 92 |  |  |             $result = $count === count($values); | 
            
                                                        
            
                                    
            
            
                | 93 | 5 |  |         } | 
            
                                                        
            
                                    
            
            
                | 94 | 4 |  |  | 
            
                                                        
            
                                    
            
            
                | 95 | 1 |  |         $reply = $result === true ? | 
            
                                                        
            
                                    
            
            
                | 96 | 1 |  |             static::createSuccessReply($values) : | 
            
                                                        
            
                                    
            
            
                | 97 | 1 |  |             static::createErrorReply( | 
            
                                                        
            
                                    
            
            
                | 98 | 1 |  |                 $context, | 
            
                                                        
            
                                    
            
            
                | 99 | 1 |  |                 $values, | 
            
                                                        
            
                                    
            
            
                | 100 | 5 |  |                 ErrorCodes::EXIST_IN_DATABASE_MULTIPLE, | 
            
                                                        
            
                                    
            
            
                | 101 |  |  |                 Messages::EXIST_IN_DATABASE_MULTIPLE, | 
            
                                                        
            
                                    
            
            
                | 102 |  |  |                 [] | 
            
                                                        
            
                                    
            
            
                | 103 | 5 |  |             ); | 
            
                                                        
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 105 |  |  |         return $reply; | 
            
                                                        
            
                                    
            
            
                | 106 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 107 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 108 |  |  |  |