ApiController::errorForbidden()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Arrilot\SlimApiController;
4
5
use Interop\Container\ContainerInterface;
6
use Psr\Http\Message\ResponseInterface;
7
use Psr\Http\Message\ServerRequestInterface;
8
use Psr\Log\LoggerInterface;
9
10
abstract class ApiController
11
{
12
    /**
13
     * @var ContainerInterface
14
     */
15
    protected $container;
16
17
    /**
18
     * @var ServerRequestInterface
19
     */
20
    protected $request;
21
22
    /**
23
     * @var ResponseInterface
24
     */
25
    protected $response;
26
    
27
    /**
28
     * @var LoggerInterface
29
     */
30
    protected $logger;
31
32
    /**
33
     * ApiController constructor.
34
     *
35
     * @param ContainerInterface $container
36
     */
37
    public function __construct(ContainerInterface $container)
38
    {
39
        $this->container = $container;
40
        $this->request = $container->get('request');
41
        $this->response = $container->get('response');
42
        $this->logger = $container->get('logger');
43
    }
44
45
    /**
46
     * Respond with error message and code.
47
     *
48
     * @param string $message
49
     * @param int $code
50
     * @return ResponseInterface
51
     */
52
    protected function respondWithError($message = 'No specific error message was specified', $code = 400)
53
    {
54
        $json = [
55
            'error' => [
56
                'http_code' => $code,
57
                'message'   => $message,
58
            ]
59
        ];
60
        
61
        return $this->response->withStatus($code)->withJson($json);
62
    }
63
64
    /**
65
     * 403 error.
66
     *
67
     * @param string $message
68
     * @return ResponseInterface
69
     */
70
    protected function errorForbidden($message = 'Forbidden')
71
    {
72
        return $this->respondWithError($message, 403);
73
    }
74
75
    /**
76
     * 500 error.
77
     *
78
     * @param string $message
79
     * @return ResponseInterface
80
     */
81
    protected function errorInternalError($message = 'Internal Error')
82
    {
83
        return $this->respondWithError($message, 500);
84
    }
85
86
    /**
87
     * 404 error
88
     *
89
     * @param string $message
90
     * @return ResponseInterface
91
     */
92
    protected function errorNotFound($message = 'Resource Not Found')
93
    {
94
        return $this->respondWithError($message, 404);
95
    }
96
97
    /**
98
     * 401 error.
99
     *
100
     * @param string $message
101
     * @return ResponseInterface
102
     */
103
    protected function errorUnauthorized($message = 'Unauthorized')
104
    {
105
        return $this->respondWithError($message, 401);
106
    }
107
108
    /**
109
     * 400 error.
110
     *
111
     * @param string$message
112
     * @return ResponseInterface
113
     */
114
    protected function errorWrongArgs($message = 'Wrong Arguments')
115
    {
116
        return $this->respondWithError($message, 400);
117
    }
118
119
    /**
120
     * 501 error.
121
     *
122
     * @param string $message
123
     * @return ResponseInterface
124
     */
125
    protected function errorNotImplemented($message = 'Not implemented')
126
    {
127
        return $this->respondWithError($message, 501);
128
    }
129
}
130