Passed
Push — master ( 8d4c80...4de01f )
by Tim
02:00 queued 11s
created

ValidateAddressController::getAddressViolations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 7
ccs 7
cts 7
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\Fundraising\Frontend\App\Controllers;
6
7
use Symfony\Component\HttpFoundation\JsonResponse;
8
use Symfony\Component\HttpFoundation\Request;
9
use Symfony\Component\HttpFoundation\Response;
10
use Symfony\Component\Translation\TranslatorInterface;
11
use WMDE\Fundraising\DonationContext\Domain\Model\DonorName;
12
use WMDE\Fundraising\Frontend\Factories\FunFunFactory;
13
use WMDE\FunValidators\ConstraintViolation;
14
use WMDE\FunValidators\Validators\AddressValidator;
15
16
/**
17
 * @license GNU GPL v2+
18
 */
19
class ValidateAddressController {
20
21
	private const VIOLATION_UNKNOWN_ADDRESS_TYPE = 'address_form_error';
22
23
	/**
24
	 * @var AddressValidator
25
	 */
26
	private $addressValidator;
27
28 3
	public function validate( Request $request, FunFunFactory $ffFactory ): Response {
29 3
		$this->addressValidator = $ffFactory->newAddressValidator();
30
31 3
		if ( $this->getAddressType( $request ) === DonorName::PERSON_PRIVATE ) {
32 1
			$nameViolations = $this->getPersonViolations( $request );
33 2
		} elseif ( $this->getAddressType( $request ) === DonorName::PERSON_COMPANY ) {
34 1
			$nameViolations = $this->getCompanyViolations( $request );
35 1
		} elseif ( $this->getAddressType( $request ) === DonorName::PERSON_ANONYMOUS ) {
36
			return $this->newSuccessResponse();
37
		} else {
38 1
			return $this->newErrorResponse(
39 1
				$ffFactory->getTranslator(),
40 1
				new ConstraintViolation(
41 1
					$this->getAddressType( $request ),
42 1
					self::VIOLATION_UNKNOWN_ADDRESS_TYPE,
43 1
					'addressType'
44
				)
45
			);
46
		}
47
48 2
		$violations = array_merge(
49 2
			$nameViolations,
50 2
			$this->getAddressViolations( $request )
51
		);
52
53 2
		if ( empty( $violations ) ) {
54 1
			return $this->newSuccessResponse();
55
		}
56
57 1
		return $this->newErrorResponse( $ffFactory->getTranslator(), ...$violations );
58
	}
59
60 1
	private function getPersonViolations( Request $request ): array {
61 1
		return $this->addressValidator->validatePersonName(
62 1
			$request->get( 'salutation', '' ),
63 1
			$request->get( 'title', '' ),
64 1
			$request->get( 'firstName', '' ),
65 1
			$request->get( 'lastName', '' )
66 1
		)->getViolations();
67
	}
68
69 1
	private function getCompanyViolations( Request $request ): array {
70 1
		return $this->addressValidator->validateCompanyName(
71 1
			$request->get( 'companyName', '' )
72 1
		)->getViolations();
73
	}
74
75 2
	private function getAddressViolations( Request $request ): array {
76 2
		return $this->addressValidator->validatePostalAddress(
77 2
			$request->get( 'street', '' ),
78 2
			$request->get( 'postcode', '' ),
79 2
			$request->get( 'city', '' ),
80 2
			$request->get( 'country', '' )
81 2
		)->getViolations();
82
	}
83
84 3
	private function getAddressType( Request $request ): string {
85 3
		return $request->get( 'addressType', '' );
86
	}
87
88 1
	private function newSuccessResponse(): Response {
89 1
		return new JsonResponse( [ 'status' => 'OK' ] );
90
	}
91
92 2
	private function newErrorResponse( TranslatorInterface $translator, ConstraintViolation ...$violations ): Response {
93 2
		$errors = [];
94
95 2
		foreach ( $violations as $violation ) {
96 2
			$errors[$violation->getSource()] = $translator->trans( $violation->getMessageIdentifier() );
97
		}
98
99 2
		return new JsonResponse( [ 'status' => 'ERR', 'messages' => $errors ] );
100
	}
101
102
}