ApiResponse::fetchErrorMessage()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
namespace Kineo\Component;
3
4
use Silex\Application;
5
use Symfony\Component\HttpFoundation\Response;
6
7
class ApiResponse extends Response
8
{
9
	public static function error($code, $customMessage = null)
10
	{		
11
		return self::responseFactory($code, 'error', $customMessage);
12
	}
13
14
	public static function success($code, $customMessage = null)
15
	{
16
		return self::responseFactory($code, 'success', $customMessage);
17
	}
18
19
	private static function responseFactory($code, $status, $customMessage = null)
20
	{
21
		$response = self::create();
22
23
		$content = self::generateResponseBody($code, $status, $customMessage);
24
		$response->setContent($content);
25
26
		self::setResponseStatusCode($response, $code);
27
28
		return $response;
29
	}
30
31
	private static function generateResponseBody($code, $status, $customMessage = null)
32
	{
33
		$message = (isset($customMessage)) ? $customMessage : self::fetchErrorMessage($code);
34
35
		return self::encodeResponse(array('code' => $code, 'status' => $status, 'message' => $message));
36
	}
37
38
	private static function fetchErrorMessage($code)
39
	{
40
		return constant("\Kineo\Component\ApiResponseMessageDefinition::$code");
41
	}
42
43
	private static function encodeResponse(array $response)
44
	{
45
		return json_encode($response);
46
	}
47
48
	private static function setResponseStatusCode($response, $code)
49
	{
50
		switch($code) {
51
			case 'SESSION_CHECK':
52
			case 'DEFAULT_RESPONSE_SUCCESS':
53
				// 200 - Success
54
				$response->setStatusCode(Response::HTTP_OK);
55
				break;
56
57
			case 'USER_EXISTS':
58
			case 'USER_SAVE_FAIL':
59
			case 'NO_CANDIDATES_FOUND':
60
			case 'NO_CONSTITUENCIES_FOUND';
0 ignored issues
show
Coding Style introduced by
CASE statements must be defined using a colon

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
61
				// 400 - Bad Request
62
				$response->setStatusCode(Response::HTTP_BAD_REQUEST);
63
				break;
64
65
			case 'SESSION_EXPIRED':
66
			case 'PASSWORD_INVALID':
67
			case 'USER_UNVERIFIED':
68
			case 'INVALID_VERIFICATION_CODE':
69
				// 403 - Unauthorized
70
				$response->setStatusCode(Response::HTTP_FORBIDDEN);
71
				break;
72
73
			default:
74
				// 200 - Success
75
				$response->setStatusCode(Response::HTTP_OK);
76
				break;
77
		}
78
	}
79
}
80
81
class ApiResponseMessageDefinition
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
82
{
83
	const DEFAULT_RESPONSE_SUCCESS = 'The API action was succesfully completed.';
84
85
	const SESSION_EXPIRED = 'Authenticated session required.';
86
87
	const PASSWORD_INVALID = 'The password provided is incorrect.';
88
	const USER_UNVERIFIED = 'This user account has not yet been verified.';
89
	const INVALID_VERIFICATION_CODE = 'The supplied verification code is invalid.';
90
	
91
	const NO_CONSTITUENCIES_FOUND = 'Sorry, we couldn\'t find any constituencies.';
92
	const NO_CANDIDATES_FOUND = 'Sorry, we couldn\'t find any candidates for this constituency.';
93
	const USER_SAVE_FAIL = 'Sorry, there was a problem saving your details to the system.';
94
	const USER_EXISTS = 'This email address has already been used to cast a vote.';
95
}