Completed
Push — master ( 6d12c1...fefd2b )
by Vincenzo
03:27
created

ApiAction::formatResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
4
namespace App\Lib\Slime\RestAction;
5
6
7
use App\Lib\Helpers\Responder;
8
use App\Lib\Slime\Exceptions\SlimeException;
9
use App\Lib\Slime\RestAction\UseCase\Action;
10
11
abstract class ApiAction extends Action
12
{
13
    protected $code;
14
    protected $message;
15
    protected $payload;
16
    protected $pagination = null;
17
18
    protected function init()
19
    {
20
        $this->code = 200;
21
        $this->message = "OK";
22
        $this->payload = null;
23
    }
24
25
    protected function performChecks()
26
    {
27
28
    }
29
30
    protected function performCallBack()
31
    {
32
33
    }
34
35
    protected function formatResponse()
36
    {
37
        $bodyContent = $this->buildBody();
38
39
        $this->response = Responder::getJsonResponse(
40
            $bodyContent,
41
            $this->response
42
        );
43
    }
44
45
    protected function manageSlimeException(SlimeException $slimeException)
46
    {
47
        $this->manageBaseException($slimeException);
48
    }
49
50
    protected function manageBaseException(\Exception $baseException)
51
    {
52
        $this->code = $baseException->getCode();
53
        $this->message = $baseException->getMessage();
54
    }
55
56
    private function buildBody()
57
    {
58
        $body = [
59
            'code' => $this->code,
60
            'message' => $this->message,
61
            'payload' => $this->payload
62
        ];
63
64
        if (!empty($this->pagination)) {
65
            $body['pagination'] = $this->pagination;
66
        }
67
        return json_encode($body);
68
    }
69
}