|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare( strict_types = 1 ); |
|
4
|
|
|
|
|
5
|
|
|
namespace WMDE\Fundraising\Frontend\App\Controllers\Validation; |
|
6
|
|
|
|
|
7
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
8
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
9
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
10
|
|
|
use WMDE\Fundraising\Frontend\Factories\FunFunFactory; |
|
11
|
|
|
use WMDE\Fundraising\Frontend\Infrastructure\AddressType; |
|
12
|
|
|
use WMDE\FunValidators\ConstraintViolation; |
|
13
|
|
|
use WMDE\FunValidators\Validators\AddressValidator; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @license GPL-2.0-or-later |
|
17
|
|
|
*/ |
|
18
|
|
|
class ValidateAddressController { |
|
19
|
|
|
|
|
20
|
|
|
private const VIOLATION_UNKNOWN_ADDRESS_TYPE = 'address_form_error'; |
|
21
|
|
|
|
|
22
|
|
|
private AddressValidator $addressValidator; |
|
23
|
|
|
|
|
24
|
|
|
public function index( Request $request, FunFunFactory $ffFactory ): Response { |
|
25
|
|
|
$this->addressValidator = $ffFactory->newAddressValidator(); |
|
26
|
|
|
|
|
27
|
|
|
$addressType = $this->getAddressType( $request ); |
|
28
|
|
|
if ( $addressType === AddressType::PERSON ) { |
|
29
|
|
|
$nameViolations = $this->getPersonViolations( $request ); |
|
30
|
|
|
} elseif ( $addressType === AddressType::COMPANY ) { |
|
31
|
|
|
$nameViolations = $this->getCompanyViolations( $request ); |
|
32
|
|
|
} elseif ( $addressType === AddressType::EMAIL ) { |
|
33
|
|
|
$nameViolations = $this->getPersonViolations( $request ); |
|
34
|
|
|
} elseif ( $addressType === AddressType::ANONYMOUS ) { |
|
35
|
|
|
return $this->newSuccessResponse(); |
|
36
|
|
|
} else { |
|
37
|
|
|
return $this->newErrorResponse( |
|
38
|
|
|
new ConstraintViolation( |
|
39
|
|
|
$addressType, |
|
40
|
|
|
self::VIOLATION_UNKNOWN_ADDRESS_TYPE, |
|
41
|
|
|
'addressType' |
|
42
|
|
|
) |
|
43
|
|
|
); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$violations = array_merge( |
|
47
|
|
|
$nameViolations, |
|
48
|
|
|
$this->getAddressViolations( $request ) |
|
49
|
|
|
); |
|
50
|
|
|
|
|
51
|
|
|
if ( empty( $violations ) ) { |
|
52
|
|
|
return $this->newSuccessResponse(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return $this->newErrorResponse( ...$violations ); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
private function getPersonViolations( Request $request ): array { |
|
59
|
|
|
return $this->addressValidator->validatePersonName( |
|
60
|
|
|
$request->get( 'salutation', '' ), |
|
61
|
|
|
$request->get( 'title', '' ), |
|
62
|
|
|
$request->get( 'firstName', '' ), |
|
63
|
|
|
$request->get( 'lastName', '' ) |
|
64
|
|
|
)->getViolations(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
private function getCompanyViolations( Request $request ): array { |
|
68
|
|
|
return $this->addressValidator->validateCompanyName( |
|
69
|
|
|
$request->get( 'companyName', '' ) |
|
70
|
|
|
)->getViolations(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
private function getAddressViolations( Request $request ): array { |
|
74
|
|
|
return $this->addressValidator->validatePostalAddress( |
|
75
|
|
|
$request->get( 'street', '' ), |
|
76
|
|
|
$request->get( 'postcode', '' ), |
|
77
|
|
|
$request->get( 'city', '' ), |
|
78
|
|
|
$request->get( 'country', '' ) |
|
79
|
|
|
)->getViolations(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
private function getAddressType( Request $request ): string { |
|
83
|
|
|
$addressType = $request->get( 'addressType', '' ); |
|
84
|
|
|
try { |
|
85
|
|
|
return AddressType::presentationAddressTypeToDomainAddressType( $addressType ); |
|
86
|
|
|
} catch ( \UnexpectedValueException $ex ) { |
|
87
|
|
|
return $addressType; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
private function newSuccessResponse(): Response { |
|
92
|
|
|
return new JsonResponse( [ 'status' => 'OK' ] ); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
private function newErrorResponse( ConstraintViolation ...$violations ): Response { |
|
96
|
|
|
$errors = []; |
|
97
|
|
|
|
|
98
|
|
|
foreach ( $violations as $violation ) { |
|
99
|
|
|
$errors[$violation->getSource()] = $violation->getMessageIdentifier(); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return new JsonResponse( [ 'status' => 'ERR', 'messages' => $errors ] ); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
|