edmondscommerce /
doctrine-static-meta
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Action; |
||
| 6 | |||
| 7 | // phpcs:disable |
||
| 8 | use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Validation\Constraints\EntityIsValidConstraintCreator; |
||
| 9 | use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Validation\Constraints\EntityIsValidConstraintValidatorCreator; |
||
| 10 | use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Validation\Constraints\PropertyConstraintCreator; |
||
| 11 | use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Validation\Constraints\PropertyConstraintValidatorCreator; |
||
| 12 | use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper; |
||
| 13 | use EdmondsCommerce\DoctrineStaticMeta\Config; |
||
| 14 | use InvalidArgumentException; |
||
| 15 | use LogicException; |
||
| 16 | use RuntimeException; |
||
| 17 | use function in_array; |
||
| 18 | |||
| 19 | // phpcs:enable |
||
| 20 | class CreateConstraintAction implements ActionInterface |
||
| 21 | { |
||
| 22 | private const SUFFIX_CONSTRAINT = 'Constraint'; |
||
| 23 | private const SUFFIX_CONSTRAINT_VALIDATOR = 'ConstraintValidator'; |
||
| 24 | |||
| 25 | public const OPTION_PROPERTY = 'property'; |
||
| 26 | public const OPTION_ENTITY = 'entity'; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var PropertyConstraintCreator |
||
| 30 | */ |
||
| 31 | protected $propertyConstraintCreator; |
||
| 32 | /** |
||
| 33 | * @var PropertyConstraintValidatorCreator |
||
| 34 | */ |
||
| 35 | protected $propertyConstraintValidatorCreator; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | private $constraintsRootNamespace; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | private $constraintShortName; |
||
| 46 | /** |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | private $propertyOrEntity = self::OPTION_PROPERTY; |
||
| 50 | /** |
||
| 51 | * @var EntityIsValidConstraintCreator |
||
| 52 | */ |
||
| 53 | private $entityIsValidConstraintCreator; |
||
| 54 | /** |
||
| 55 | * @var EntityIsValidConstraintValidatorCreator |
||
| 56 | */ |
||
| 57 | private $entityIsValidConstraintValidatorCreator; |
||
| 58 | |||
| 59 | 3 | public function __construct( |
|
| 60 | PropertyConstraintCreator $constraintCreator, |
||
| 61 | PropertyConstraintValidatorCreator $constraintValidatorCreator, |
||
| 62 | EntityIsValidConstraintCreator $entityIsValidConstraintCreator, |
||
| 63 | EntityIsValidConstraintValidatorCreator $entityIsValidConstraintValidatorCreator, |
||
| 64 | NamespaceHelper $namespaceHelper, |
||
| 65 | Config $config |
||
| 66 | ) { |
||
| 67 | 3 | $this->propertyConstraintCreator = $constraintCreator; |
|
| 68 | 3 | $this->propertyConstraintValidatorCreator = $constraintValidatorCreator; |
|
| 69 | 3 | $this->entityIsValidConstraintCreator = $entityIsValidConstraintCreator; |
|
| 70 | 3 | $this->entityIsValidConstraintValidatorCreator = $entityIsValidConstraintValidatorCreator; |
|
| 71 | 3 | $this->setProjectRootNamespace($namespaceHelper->getProjectRootNamespaceFromComposerJson()); |
|
| 72 | 3 | $this->setProjectRootDirectory($config::getProjectRootDirectory()); |
|
| 73 | 3 | } |
|
| 74 | |||
| 75 | 3 | public function setProjectRootNamespace(string $projectRootNamespace): self |
|
| 76 | { |
||
| 77 | 3 | $this->propertyConstraintCreator->setProjectRootNamespace($projectRootNamespace); |
|
| 78 | 3 | $this->propertyConstraintValidatorCreator->setProjectRootNamespace($projectRootNamespace); |
|
| 79 | 3 | $this->constraintsRootNamespace = $projectRootNamespace . '\\Validation\\Constraints'; |
|
| 80 | |||
| 81 | 3 | return $this; |
|
| 82 | } |
||
| 83 | |||
| 84 | 3 | public function setProjectRootDirectory(string $projectRootDirectory): self |
|
| 85 | { |
||
| 86 | 3 | $this->propertyConstraintCreator->setProjectRootDirectory($projectRootDirectory); |
|
| 87 | 3 | $this->propertyConstraintValidatorCreator->setProjectRootDirectory($projectRootDirectory); |
|
| 88 | |||
| 89 | 3 | return $this; |
|
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @param string $constraintShortName |
||
| 94 | * |
||
| 95 | * @return CreateConstraintAction |
||
| 96 | */ |
||
| 97 | 3 | public function setConstraintShortName(string $constraintShortName): self |
|
| 98 | { |
||
| 99 | 3 | $this->constraintShortName = $constraintShortName; |
|
| 100 | |||
| 101 | 3 | return $this; |
|
| 102 | } |
||
| 103 | |||
| 104 | 1 | public function setPropertyOrEntity(string $propertyOrEntity): self |
|
| 105 | { |
||
| 106 | 1 | if (false === in_array($propertyOrEntity, [self::OPTION_PROPERTY, self::OPTION_ENTITY], true)) { |
|
| 107 | throw new InvalidArgumentException( |
||
| 108 | '$propertyOrEntity must be one of self::OPTION_PROPERTY,self::OPTION_ENTITY' |
||
| 109 | ); |
||
| 110 | } |
||
| 111 | 1 | $this->propertyOrEntity = $propertyOrEntity; |
|
| 112 | |||
| 113 | 1 | return $this; |
|
| 114 | } |
||
| 115 | |||
| 116 | 3 | public function run(): void |
|
| 117 | { |
||
| 118 | 3 | if (null === $this->constraintShortName) { |
|
| 119 | throw new RuntimeException('You must call setContraintShortname before calling run'); |
||
| 120 | } |
||
| 121 | 3 | if (self::OPTION_PROPERTY === $this->propertyOrEntity) { |
|
| 122 | 2 | $this->createPropertyConstraint($this->constraintShortName); |
|
| 123 | 2 | $this->createPropertyConstraintValidator($this->constraintShortName); |
|
| 124 | |||
| 125 | 2 | return; |
|
| 126 | } |
||
| 127 | 1 | if (self::OPTION_ENTITY === $this->propertyOrEntity) { |
|
| 128 | 1 | $this->createEntityConstraint($this->constraintShortName); |
|
| 129 | 1 | $this->createEntityConstraintValidator($this->constraintShortName); |
|
| 130 | |||
| 131 | 1 | return; |
|
| 132 | } |
||
| 133 | |||
| 134 | throw new LogicException('Invalid propertyOrEntity ' . $this->propertyOrEntity); |
||
| 135 | } |
||
| 136 | |||
| 137 | 2 | private function createPropertyConstraint(string $constraintShortName): void |
|
| 138 | { |
||
| 139 | 2 | $constraintFqn = $this->constraintsRootNamespace . '\\' . $this->stripSuffix($constraintShortName) |
|
| 140 | 2 | . self::SUFFIX_CONSTRAINT; |
|
| 141 | 2 | $this->propertyConstraintCreator->createTargetFileObject($constraintFqn)->write(); |
|
| 142 | 2 | } |
|
| 143 | |||
| 144 | 3 | private function stripSuffix(string $constraintShortName): string |
|
| 145 | { |
||
| 146 | 3 | if (false === \ts\stringContains($constraintShortName, self::SUFFIX_CONSTRAINT)) { |
|
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 147 | 2 | return $constraintShortName; |
|
| 148 | } |
||
| 149 | |||
| 150 | 1 | $pos = \ts\strpos($constraintShortName, self::SUFFIX_CONSTRAINT); |
|
| 151 | |||
| 152 | 1 | return substr($constraintShortName, 0, $pos); |
|
| 153 | } |
||
| 154 | |||
| 155 | 2 | private function createPropertyConstraintValidator(string $constraintShortName): void |
|
| 156 | { |
||
| 157 | 2 | $constraintValidatorFqn = $this->constraintsRootNamespace . '\\' . $this->stripSuffix($constraintShortName) . |
|
| 158 | 2 | self::SUFFIX_CONSTRAINT_VALIDATOR; |
|
| 159 | 2 | $this->propertyConstraintValidatorCreator->createTargetFileObject($constraintValidatorFqn)->write(); |
|
| 160 | 2 | } |
|
| 161 | |||
| 162 | 1 | private function createEntityConstraint(string $constraintShortName): void |
|
| 163 | { |
||
| 164 | 1 | $constraintFqn = $this->constraintsRootNamespace . '\\' . $this->stripSuffix($constraintShortName) |
|
| 165 | 1 | . self::SUFFIX_CONSTRAINT; |
|
| 166 | 1 | $this->entityIsValidConstraintCreator->createTargetFileObject($constraintFqn)->write(); |
|
| 167 | 1 | } |
|
| 168 | |||
| 169 | 1 | private function createEntityConstraintValidator(string $constraintShortName): void |
|
| 170 | { |
||
| 171 | 1 | $constraintValidatorFqn = $this->constraintsRootNamespace . '\\' . $this->stripSuffix($constraintShortName) . |
|
| 172 | 1 | self::SUFFIX_CONSTRAINT_VALIDATOR; |
|
| 173 | 1 | $this->entityIsValidConstraintValidatorCreator->createTargetFileObject($constraintValidatorFqn)->write(); |
|
| 174 | 1 | } |
|
| 175 | } |
||
| 176 |