Completed
Pull Request — master (#9)
by Alberto
02:47
created

ExceptionRenderer::_outputMessageSafe()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 15
nc 3
nop 1
dl 0
loc 25
rs 9.7666
c 0
b 0
f 0
1
<?php
2
/**
3
 * BEdita, API-first content management framework
4
 * Copyright 2018 ChannelWeb Srl, Chialab Srl
5
 *
6
 * This file is part of BEdita: you can redistribute it and/or modify
7
 * it under the terms of the GNU Lesser General Public License as published
8
 * by the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details.
12
 */
13
14
namespace BEdita\WebTools\Error;
15
16
use Cake\Error\ExceptionRenderer as CakeExceptionRenderer;
17
use Cake\Log\LogTrait;
18
19
/**
20
 * Custom exception renderer class.
21
 * Handle with templates 500 and 400 (for status code < 500).
22
 */
23
class ExceptionRenderer extends CakeExceptionRenderer
24
{
25
    use LogTrait;
26
27
    /**
28
     * {@inheritDoc}
29
     */
30
    protected function _template(\Exception $exception, $method, $code) : string
31
    {
32
        $exception = $this->_unwrap($exception);
0 ignored issues
show
Unused Code introduced by
The assignment to $exception is dead and can be removed.
Loading history...
33
34
        $template = 'error500';
35
        if ($code < 500) {
36
            $template = 'error400';
37
        }
38
39
        return $this->template = $template;
40
    }
41
42
    /**
43
     * {@inheritDoc}
44
     */
45
    protected function _outputMessageSafe($template)
46
    {
47
        $builder = $this->controller->viewBuilder();
48
        $builder->setLayoutPath('')
49
            ->setTemplatePath('Error');
50
51
        // first try to use AppView class. Fallback to internal template on failure
52
        try {
53
            $view = $this->controller->createView();
54
            $this->controller->response->body($view->render($template, 'error'));
0 ignored issues
show
Bug introduced by
The method body() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

54
            $this->controller->response->/** @scrutinizer ignore-call */ 
55
                                         body($view->render($template, 'error'));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Deprecated Code introduced by
The function Cake\Http\Response::body() has been deprecated: 3.4.0 Mutable response methods are deprecated. Use `withBody()`/`withStringBody()` and `getBody()` instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

54
            /** @scrutinizer ignore-deprecated */ $this->controller->response->body($view->render($template, 'error'));

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
55
        } catch (\Exception $e) {
56
            // first log the new exception to trace the new error too.
57
            $this->log($e->getMessage());
58
59
            $helpers = ['Form', 'Html'];
60
            $this->controller->helpers = $helpers;
61
            $builder->setHelpers($helpers, false);
62
63
            $view = $this->controller->createView('View');
64
            $this->controller->response->body($view->render('BEdita/WebTools.' . $template, 'BEdita/WebTools.error'));
0 ignored issues
show
Deprecated Code introduced by
The function Cake\Http\Response::body() has been deprecated: 3.4.0 Mutable response methods are deprecated. Use `withBody()`/`withStringBody()` and `getBody()` instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

64
            /** @scrutinizer ignore-deprecated */ $this->controller->response->body($view->render('BEdita/WebTools.' . $template, 'BEdita/WebTools.error'));

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
65
        }
66
67
        $this->controller->response->type('html');
0 ignored issues
show
Deprecated Code introduced by
The function Cake\Http\Response::type() has been deprecated: 3.5.5 Use getType() or withType() instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

67
        /** @scrutinizer ignore-deprecated */ $this->controller->response->type('html');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
68
69
        return $this->controller->response;
70
    }
71
}
72