Passed
Push — master ( e5acb2...2b13b1 )
by Evgeny
07:45
created

FileRenderer   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 57
ccs 0
cts 24
cp 0
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A response() 0 10 1
A error() 0 17 3
A _encode() 0 6 2
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 CakeDC\Api\Service\Renderer\BaseRenderer;
16
use Cake\Core\Configure;
17
use Exception;
18
19
/**
20
 * Class FileRenderer
21
 *
22
 * @package CakeDC\Api\Service\Renderer
23
 */
24
class FileRenderer 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
    public function response(Result $result = null)
34
    {
35
        $response = $this->_service
36
            ->getResponse()
37
            ->withFile($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...
38
            ->withStatus($result->getCode());
39
        $this->_service->setResponse($response);
40
41
        return true;
42
    }
43
44
    /**
45
     * Processes an exception thrown while processing the request.
46
     *
47
     * @param Exception $exception The exception object.
48
     * @return void
49
     */
50
    public function error(Exception $exception)
51
    {
52
        $response = $this->_service->getResponse();
53
        $data = [
54
            'error' => [
55
                'code' => $exception->getCode(),
56
                'message' => $this->_buildMessage($exception)
57
            ]
58
        ];
59
        if (Configure::read('debug') > 0) {
60
            $data['error']['trace'] = $this->_stackTrace($exception);
61
        }
62
        if ($exception instanceof ValidationException) {
0 ignored issues
show
Bug introduced by
The class CakeDC\Api\Service\Renderer\ValidationException does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
63
            $data['error']['validation'] = $exception->getValidationErrors();
64
        }
65
        $this->_service->setResponse($response->withStringBody($this->_encode($data))->withType('application/json'));
66
    }
67
68
    /**
69
     * Encoded object as json. In debug mode used pretty printed objects.
70
     *
71
     * @param mixed $data Encoded data.
72
     * @return string
73
     */
74
    protected function _encode($data)
75
    {
76
        $format = (Configure::read('debug') > 0) ? JSON_PRETTY_PRINT : 0;
77
78
        return json_encode($data, $format);
79
    }
80
}
81