BaseRenderer::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 3
cts 3
cp 1
crap 1
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\Service;
16
use Cake\Core\Configure;
17
use Exception;
18
19
/**
20
 * Base class for a Service content negotiation Renderer.
21
 */
22
abstract class BaseRenderer
23
{
24
25
    /**
26
     * Reference to the Service.
27
     *
28
     * @var \CakeDC\Api\Service\Service
29
     */
30
    protected $_service = null;
31
32
    /**
33
     * Constructor
34
     *
35
     * @param Service $service The Service instantiating the Renderer.
36
     */
37 140
    public function __construct(Service $service)
38
    {
39 140
        $this->_service = $service;
40 140
    }
41
42
    /**
43
     * Confirms if the specified content type is acceptable for the response.
44
     *
45
     * @return bool
46
     */
47
    public function accept()
48
    {
49
        return true;
50
    }
51
52
    /**
53
     * Builds the HTTP response.
54
     *
55
     * @param Result $result The result object returned by the Service.
56
     * @return bool
57
     */
58
    abstract public function response(Result $result = null);
59
60
    /**
61
     * Processes an exception thrown while processing the request.
62
     *
63
     * @param Exception $exception The exception object.
64
     * @return void
65
     */
66
    abstract public function error(Exception $exception);
67
68
    /**
69
     * Format error message.
70
     *
71
     * @param Exception $exception An Exception instance.
72
     * @return string
73
     */
74 11
    protected function _buildMessage(Exception $exception)
75
    {
76 11
        $message = $exception->getMessage();
77 11
        if (Configure::read('debug') > 0) {
78 8
            $message .= ' on line ' . $exception->getLine() . ' in ' . $exception->getFile();
79 8
        }
80
81 11
        return $message;
82
    }
83
84
    /**
85
     * Returns formatted stack trace
86
     *
87
     * @param Exception $exception An Exception instance.
88
     * @return array
89
     */
90 9
    protected function _stackTrace(Exception $exception)
91
    {
92 9
        if (Configure::read('debug') == 0) {
93 1
            return null;
94
        }
95 8
        $trace = $exception->getTrace();
96 8
        $count = count($trace);
97 8
        for ($i = 0; $i < $count; $i++) {
98 8
            foreach ($trace[$i] as $key => $value) {
99 8
                if ($key === 'object' || $key === 'type' || $key === 'args') {
100 8
                    unset($trace[$i][$key]);
101 8
                }
102 8
            }
103 8
        }
104
105 8
        return $trace;
106
    }
107
}
108