| Total Complexity | 41 |
| Total Lines | 193 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like VatNumberValidator often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use VatNumberValidator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class VatNumberValidator extends ConstraintValidator |
||
| 20 | { |
||
| 21 | private ?VatNumberValidatorInterface $validator = null; |
||
| 22 | |||
| 23 | public function __construct( |
||
| 24 | private VatNumberValidatorProviderInterface $validatorProvider, |
||
| 25 | private bool $isActive = true, |
||
| 26 | private bool $validateCountry = true, |
||
| 27 | private bool $validateExistence = true, |
||
| 28 | private bool $isCompanyVatRequired = true, |
||
| 29 | private array $requiredCountries = [], |
||
| 30 | ) { |
||
| 31 | } |
||
| 32 | |||
| 33 | public function validate(mixed $value, Constraint $constraint): void |
||
| 34 | { |
||
| 35 | if (!$constraint instanceof VatNumber) { |
||
| 36 | throw new UnexpectedTypeException($constraint, VatNumber::class); |
||
| 37 | } |
||
| 38 | |||
| 39 | if (!$value instanceof VatNumberAddressInterface) { |
||
| 40 | throw new UnexpectedValueException($value, VatNumberAddressInterface::class); |
||
| 41 | } |
||
| 42 | |||
| 43 | if (!$this->hasVatNumberForCompany($value, $constraint)) { |
||
| 44 | return; |
||
| 45 | } |
||
| 46 | |||
| 47 | if (!$this->hasVatNumberForCountry($value, $constraint)) { |
||
| 48 | return; |
||
| 49 | } |
||
| 50 | |||
| 51 | if (!$value->hasVatNumber()) { |
||
| 52 | return; |
||
| 53 | } |
||
| 54 | |||
| 55 | $this->validateVatNumberAddress($value, $constraint); |
||
| 56 | } |
||
| 57 | |||
| 58 | private function hasVatNumberForCompany(VatNumberAddressInterface $address, VatNumber $constraint): bool |
||
| 73 | } |
||
| 74 | |||
| 75 | private function hasVatNumberForCountry(VatNumberAddressInterface $address, VatNumber $constraint): bool |
||
| 76 | { |
||
| 77 | if (count($this->requiredCountries) > 0 && |
||
| 78 | in_array($address->getCountryCode(), $this->requiredCountries, true) && |
||
| 79 | !$address->hasVatNumber()) { |
||
| 80 | $this->context->buildViolation($constraint->messageRequired) |
||
| 81 | ->atPath($constraint->vatNumberPath) |
||
| 82 | ->addViolation() |
||
| 83 | ; |
||
| 84 | |||
| 85 | return false; |
||
| 86 | } |
||
| 87 | |||
| 88 | return true; |
||
| 89 | } |
||
| 90 | |||
| 91 | private function setValidator(VatNumberAddressInterface $address): bool |
||
| 92 | { |
||
| 93 | $countryCode = $address->getCountryCode(); |
||
| 94 | if (null === $countryCode || '' === $countryCode) { |
||
| 95 | return false; |
||
| 96 | } |
||
| 97 | |||
| 98 | $this->validator = $this->validatorProvider->getValidator($countryCode); |
||
| 99 | if ($this->validator instanceof VatNumberValidatorInterface) { |
||
| 100 | return true; |
||
| 101 | } |
||
| 102 | |||
| 103 | return false; |
||
| 104 | } |
||
| 105 | |||
| 106 | private function validateVatNumberAddress(VatNumberAddressInterface $address, VatNumber $constraint): bool |
||
| 107 | { |
||
| 108 | if (!$this->isActive) { |
||
| 109 | return false; |
||
| 110 | } |
||
| 111 | |||
| 112 | if (!$this->setValidator($address)) { |
||
| 113 | return false; |
||
| 114 | } |
||
| 115 | |||
| 116 | if (!$this->validateFormat($address, $constraint)) { |
||
| 117 | return false; |
||
| 118 | } |
||
| 119 | |||
| 120 | if ($this->validateCountry && !$this->validateCountry($address, $constraint)) { |
||
| 121 | return false; |
||
| 122 | } |
||
| 123 | |||
| 124 | if ($this->validateExistence) { |
||
| 125 | return $this->validateExistence($address, $constraint); |
||
| 126 | } |
||
| 127 | |||
| 128 | return false; |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * check vat number format |
||
| 133 | */ |
||
| 134 | private function validateFormat(VatNumberAddressInterface $address, VatNumber $constraint): bool |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * check vat number country is same as address country |
||
| 156 | */ |
||
| 157 | private function validateCountry(VatNumberAddressInterface $address, VatNumber $constraint): bool |
||
| 158 | { |
||
| 159 | if ($this->validator === null) { |
||
| 160 | return false; |
||
| 161 | } |
||
| 162 | |||
| 163 | $vatNumber = $address->getVatNumber(); |
||
| 164 | $countryCode = $address->getCountryCode(); |
||
| 165 | |||
| 166 | if (null === $vatNumber || |
||
| 167 | null === $countryCode || |
||
| 168 | !$this->validator->validateCountry($vatNumber, $countryCode)) { |
||
| 169 | $this->context->buildViolation($constraint->messageCountry) |
||
| 170 | ->atPath($constraint->vatNumberPath) |
||
| 171 | ->addViolation() |
||
| 172 | ; |
||
| 173 | |||
| 174 | return false; |
||
| 175 | } |
||
| 176 | |||
| 177 | return true; |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * check vat number existence |
||
| 182 | */ |
||
| 183 | private function validateExistence(VatNumberAddressInterface $address, VatNumber $constraint): bool |
||
| 212 | } |
||
| 213 | } |
||
| 214 |