|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare( strict_types = 1 ); |
|
4
|
|
|
|
|
5
|
|
|
namespace WMDE\Fundraising\Frontend\App\RouteHandlers; |
|
6
|
|
|
|
|
7
|
|
|
use Silex\Application; |
|
8
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
9
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
10
|
|
|
use WMDE\Fundraising\Frontend\DonationContext\Domain\Model\Donor; |
|
11
|
|
|
use WMDE\Fundraising\Frontend\DonationContext\Domain\Model\DonorAddress; |
|
12
|
|
|
use WMDE\Fundraising\Frontend\DonationContext\Domain\Model\DonorName; |
|
13
|
|
|
use WMDE\Fundraising\Frontend\Factories\FunFunFactory; |
|
14
|
|
|
use WMDE\FunValidators\ConstraintViolation; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Validates donor information. The route is named badly. |
|
18
|
|
|
* |
|
19
|
|
|
* @license GNU GPL v2+ |
|
20
|
|
|
* @author Jeroen De Dauw < [email protected] > |
|
21
|
|
|
*/ |
|
22
|
|
|
class ValidateAddressHandler { |
|
23
|
|
|
|
|
24
|
|
|
private $ffFactory; |
|
25
|
|
|
private $app; |
|
26
|
|
|
|
|
27
|
|
|
public function __construct( FunFunFactory $ffFactory, Application $app ) { |
|
28
|
|
|
$this->ffFactory = $ffFactory; |
|
29
|
|
|
$this->app = $app; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function handle( Request $request ): Response { |
|
33
|
|
|
if ( $request->get( 'adressType', '' ) === 'anonym' ) { |
|
34
|
|
|
return $this->newSuccessResponse(); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
$validationResult = |
|
38
|
|
|
$this->ffFactory->newDonorValidator() |
|
39
|
|
|
->validate( $this->getDonorFromRequest( $request ) ); |
|
40
|
|
|
|
|
41
|
|
|
if ( $validationResult->isSuccessful() ) { |
|
42
|
|
|
return $this->newSuccessResponse(); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
return $this->newErrorResponse( ...$validationResult->getViolations() ); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
private function newSuccessResponse(): Response { |
|
49
|
|
|
return $this->app->json( [ 'status' => 'OK' ] ); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
private function newErrorResponse( ConstraintViolation ...$violations ) { |
|
53
|
|
|
$errors = []; |
|
54
|
|
|
|
|
55
|
|
|
foreach( $violations as $violation ) { |
|
56
|
|
|
$errors[$violation->getSource()] = $this->ffFactory->getTranslator()->trans( $violation->getMessageIdentifier() ); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return $this->app->json( [ 'status' => 'ERR', 'messages' => $errors ] ); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
private function getDonorFromRequest( Request $request ): Donor { |
|
63
|
|
|
return new Donor( |
|
64
|
|
|
$this->getNameFromRequest( $request ), |
|
65
|
|
|
$this->getPhysicalAddressFromRequest( $request ), |
|
66
|
|
|
$request->get( 'email', '' ) |
|
67
|
|
|
); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
private function getPhysicalAddressFromRequest( Request $request ): DonorAddress { |
|
71
|
|
|
$address = new DonorAddress(); |
|
72
|
|
|
|
|
73
|
|
|
$address->setStreetAddress( $request->get( 'street', '' ) ); |
|
74
|
|
|
$address->setPostalCode( $request->get( 'postcode', '' ) ); |
|
75
|
|
|
$address->setCity( $request->get( 'city', '' ) ); |
|
76
|
|
|
$address->setCountryCode( $request->get( 'country', '' ) ); |
|
77
|
|
|
|
|
78
|
|
|
return $address->freeze()->assertNoNullFields(); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
private function getNameFromRequest( Request $request ): DonorName { |
|
82
|
|
|
$name = $request->get( 'addressType', '' ) === 'firma' |
|
83
|
|
|
? DonorName::newCompanyName() : DonorName::newPrivatePersonName(); |
|
84
|
|
|
|
|
85
|
|
|
$name->setSalutation( $request->get( 'salutation', '' ) ); |
|
86
|
|
|
$name->setTitle( $request->get( 'title', '' ) ); |
|
87
|
|
|
$name->setCompanyName( $request->get( 'companyName', '' ) ); |
|
88
|
|
|
$name->setFirstName( $request->get( 'firstName', '' ) ); |
|
89
|
|
|
$name->setLastName( $request->get( 'lastName', '' ) ); |
|
90
|
|
|
|
|
91
|
|
|
return $name->freeze()->assertNoNullFields(); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
} |