Passed
Pull Request — master (#1750)
by Gabriel
66:53 queued 01:47
created

AddSubscriptionJsonPresenter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\Fundraising\Frontend\Presentation\Presenters;
6
7
use WMDE\FunValidators\ValidationResponse;
8
use WMDE\FunValidators\ConstraintViolation;
9
10
/**
11
 * @licence GNU GPL v2+
12
 * @author Gabriel Birke < [email protected] >
13
 * @author Kai Nissen < [email protected] >
14
 */
15
class AddSubscriptionJsonPresenter {
16
17
	public function present( ValidationResponse $subscriptionResponse ): array {
18
		if ( $subscriptionResponse->isSuccessful() ) {
19
			return $this->newSuccessResponse();
20 3
		}
21 3
22 3
		return $this->newErrorResponse( $subscriptionResponse );
23
	}
24 3
25 3
	private function newSuccessResponse(): array {
26 2
		return [ 'status' => 'OK' ];
27
	}
28
29 1
	private function newErrorResponse( ValidationResponse $response ): array {
30
		$errors = [];
31
		/** @var ConstraintViolation $constraintViolation */
32 2
		foreach ( $response->getValidationErrors() as $constraintViolation ) {
33 2
			$errors[$constraintViolation->getSource()] = $constraintViolation->getMessageIdentifier();
34
		}
35
		return [ 'status' => 'ERR', 'errors' => $errors ];
36 1
	}
37
}
38