RawRenderer::error()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
ccs 7
cts 7
cp 1
crap 3
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\Service\Action\Result;
15
use Cake\Core\Configure;
16
use Exception;
17
18
/**
19
 * Class RawRenderer
20
 * Raw unformatted content negotiation Renderer.
21
 *
22
 * @package CakeDC\Api\Service\Renderer
23
 */
24
class RawRenderer extends BaseRenderer
25
{
26
27
    /**
28
     * Builds the HTTP response.
29
     *
30
     * @param Result $result The result object returned by the Service.
31
     * @return bool
32
     */
33 1
    public function response(Result $result = null)
34
    {
35 1
        $response = $this->_service->getResponse();
36 1
        $this->_service->setResponse($response->withStringBody((string)$result->getData())->withStatus($result->getCode())
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...
37 1
            ->withType('text/plain'));
38
39 1
        return true;
40
    }
41
42
    /**
43
     * Processes an exception thrown while processing the request.
44
     *
45
     * @param Exception $exception The exception object.
46
     * @return void
47
     */
48 1
    public function error(Exception $exception)
49
    {
50 1
        $response = $this->_service->getResponse();
51 1
        $message = (Configure::read('debug') > 0) ? $exception->getMessage() . ' on line ' . $exception->getLine() . ' in ' . $exception->getFile() : $exception->getMessage();
52 1
        $trace = $exception->getTrace();
53 1
        $debug = (Configure::read('debug') > 0) ? "\n" . print_r($trace, true) : '';
54 1
        $this->_service->setResponse($response->withStringBody($message . $debug)->withType('text/plain'));
55 1
    }
56
}
57