Passed
Push — master ( 7bf80a...213b2b )
by Gabriel
30:28 queued 10s
created

ValidateAddressController::getCompanyViolations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 4
ccs 4
cts 4
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
15
/**
16
 * @license GNU GPL v2+
17
 */
18
class ValidateAddressController {
19
20
	private const VIOLATION_UNKNOWN_ADDRESS_TYPE = 'address_form_error';
21
22
	private $addressValidator;
23
	private $emailValidator;
24
25 3
	public function validate( Request $request, FunFunFactory $ffFactory ): Response {
26 3
		$this->addressValidator = $ffFactory->newAddressValidator();
27 3
		$this->emailValidator = $ffFactory->getEmailValidator();
28
29 3
		if ( $this->getAddressType( $request ) === DonorName::PERSON_PRIVATE ) {
30 1
			$nameViolations = $this->getPersonViolations( $request );
31 2
		} elseif ( $this->getAddressType( $request ) === DonorName::PERSON_COMPANY ) {
32 1
			$nameViolations = $this->getCompanyViolations( $request );
33 1
		} elseif ( $this->getAddressType( $request ) === DonorName::PERSON_ANONYMOUS ) {
34
			return $this->newSuccessResponse();
35
		} else {
36 1
			return $this->newErrorResponse(
37 1
				$ffFactory->getTranslator(),
38 1
				new ConstraintViolation(
39 1
					$this->getAddressType( $request ),
40 1
					self::VIOLATION_UNKNOWN_ADDRESS_TYPE,
41 1
					'addressType'
42
				)
43
			);
44
		}
45
46 2
		$violations = array_merge(
47 2
			$nameViolations,
48 2
			$this->getAddressViolations( $request ),
49 2
			$this->getEmailViolations( $request )
50
		);
51
52 2
		if ( empty( $violations ) ) {
53 1
			return $this->newSuccessResponse();
54
		}
55
56 1
		return $this->newErrorResponse( $ffFactory->getTranslator(), ...$violations );
57
	}
58
59 1
	private function getPersonViolations( Request $request ): array {
60 1
		return $this->addressValidator->validatePersonName(
61 1
			$request->get( 'salutation', '' ),
62 1
			$request->get( 'title', '' ),
63 1
			$request->get( 'firstName', '' ),
64 1
			$request->get( 'lastName', '' )
65 1
		)->getViolations();
66
	}
67
68 1
	private function getCompanyViolations( Request $request ): array {
69 1
		return $this->addressValidator->validateCompanyName(
70 1
			$request->get( 'companyName', '' )
71 1
		)->getViolations();
72
	}
73
74 2
	private function getAddressViolations( Request $request ): array {
75 2
		return $this->addressValidator->validatePostalAddress(
76 2
			$request->get( 'street', '' ),
77 2
			$request->get( 'postcode', '' ),
78 2
			$request->get( 'city', '' ),
79 2
			$request->get( 'country', '' )
80 2
		)->getViolations();
81
	}
82
83 2
	private function getEmailViolations( Request $request ): array {
84 2
		return $this->emailValidator->validate( $request->get( 'email', '' ) )->getViolations();
85
	}
86
87 3
	private function getAddressType( Request $request ): string {
88 3
		return $request->get( 'addressType', '' );
89
	}
90
91 1
	private function newSuccessResponse(): Response {
92 1
		return new JsonResponse( [ 'status' => 'OK' ] );
93
	}
94
95 2
	private function newErrorResponse( TranslatorInterface $translator, ConstraintViolation ...$violations ): Response {
96 2
		$errors = [];
97
98 2
		foreach ( $violations as $violation ) {
99 2
			$errors[$violation->getSource()] = $translator->trans( $violation->getMessageIdentifier() );
100
		}
101
102 2
		return new JsonResponse( [ 'status' => 'ERR', 'messages' => $errors ] );
103
	}
104
105
}