JsonRenderer::error()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3.2098

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 1
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
ccs 10
cts 14
cp 0.7143
crap 3.2098
1
<?php
2
/**
3
 * Copyright 2016 - 2018, Cake Development Corporation (http://cakedc.com)
4
 *
5
 * Licensed under The MIT License
6
 * Redistributions of files must retain the above copyright notice.
7
 *
8
 * @copyright Copyright 2016 - 2018, Cake Development Corporation (http://cakedc.com)
9
 * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
10
 */
11
12
namespace CakeDC\Api\Service\Renderer;
13
14
use CakeDC\Api\Exception\ValidationException;
15
use CakeDC\Api\Service\Action\Result;
16
use Cake\Core\Configure;
17
use Cake\Utility\Hash;
18
use Exception;
19
20
/**
21
 * Class JsonRenderer
22
 * JSON content negotiation Renderer.
23
 *
24
 * @package CakeDC\Api\Service\Renderer
25
 */
26
class JsonRenderer extends BaseRenderer
27
{
28
29
    /**
30
     * Builds the HTTP response.
31
     *
32
     * @param Result $result The result object returned by the Service.
33
     * @return bool
34
     */
35 1
    public function response(Result $result = null)
36
    {
37 1
        $response = $this->_service->getResponse();
38 1
        $data = $result->getData();
0 ignored issues
show
Bug introduced by
It seems like $result is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
39 1
        $payload = $result->getPayload();
40 1
        if (is_array($data) && is_array($payload)) {
41 1
            $data = Hash::merge($data, $payload);
42 1
        }
43 1
        $this->_service->setResponse($response->withStringBody($this->_encode($data))->withStatus($result->getCode())
44 1
            ->withType('application/json'));
45
46 1
        return true;
47
    }
48
49
    /**
50
     * Processes an exception thrown while processing the request.
51
     *
52
     * @param Exception $exception The exception object.
53
     * @return void
54
     */
55 1
    public function error(Exception $exception)
56
    {
57 1
        $response = $this->_service->getResponse();
58
        $data = [
59
            'error' => [
60 1
                'code' => $exception->getCode(),
61 1
                'message' => $this->_buildMessage($exception)
62 1
            ]
63 1
        ];
64 1
        if (Configure::read('debug') > 0) {
65
            $data['error']['trace'] = $this->_stackTrace($exception);
66
        }
67 1
        if ($exception instanceof ValidationException) {
68
            $data['error']['validation'] = $exception->getValidationErrors();
69
        }
70 1
        $this->_service->setResponse($response->withStringBody($this->_encode($data))->withType('application/json'));
71 1
    }
72
73
    /**
74
     * Encoded object as json. In debug mode used pretty printed objects.
75
     *
76
     * @param mixed $data Encoded data.
77
     * @return string
78
     */
79 2
    protected function _encode($data)
80
    {
81 2
        $format = (Configure::read('debug') > 0) ? JSON_PRETTY_PRINT : 0;
82
83 2
        return json_encode($data, $format);
84
    }
85
}
86