Passed
Push — master ( 4bc322...e35e92 )
by Peter
04:39
created

AdminAbstract::getExceptionContext()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Framework\Http\Controllers\Admin;
6
7
use AbterPhp\Framework\Domain\Entities\IStringerEntity;
8
use AbterPhp\Framework\Http\Controllers\ControllerAbstract;
9
use AbterPhp\Framework\I18n\ITranslator;
10
use AbterPhp\Framework\Session\FlashService;
11
use Opulence\Routing\Urls\UrlGenerator;
12
use Psr\Log\LoggerInterface;
13
14
abstract class AdminAbstract extends ControllerAbstract
15
{
16
    const ENTITY_PLURAL   = '';
17
    const ENTITY_SINGULAR = '';
18
19
    const ENTITY_LOAD_FAILURE = 'framework:load-failure';
20
21
    const URL_EDIT = '%s-edit';
22
23
    const RESOURCE_DEFAULT = '%s';
24
    const RESOURCE_HEADER  = '%s-header';
25
    const RESOURCE_FOOTER  = '%s-footer';
26
    const RESOURCE_TYPE    = 'void';
27
28
    const LOG_CONTEXT_EXCEPTION  = 'Exception';
29
    const LOG_PREVIOUS_EXCEPTION = 'Previous exception #%d';
30
31
    /** @var ITranslator */
32
    protected $translator;
33
34
    /** @var UrlGenerator */
35
    protected $urlGenerator;
36
37
    /** @var LoggerInterface */
38
    protected $logger;
39
40
    /** @var string */
41
    protected $resource = '';
42
43
    /**
44
     * AdminAbstract constructor.
45
     *
46
     * @param FlashService    $flashService
47
     * @param ITranslator     $translator
48
     * @param UrlGenerator    $urlGenerator
49
     * @param LoggerInterface $logger
50
     */
51
    public function __construct(
52
        FlashService $flashService,
53
        ITranslator $translator,
54
        UrlGenerator $urlGenerator,
55
        LoggerInterface $logger
56
    ) {
57
        parent::__construct($flashService);
58
59
        $this->translator   = $translator;
60
        $this->urlGenerator = $urlGenerator;
61
        $this->logger       = $logger;
62
    }
63
64
    /**
65
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
66
     *
67
     * @param IStringerEntity|null $entity
68
     */
69
    protected function addCustomAssets(?IStringerEntity $entity = null)
0 ignored issues
show
Unused Code introduced by
The parameter $entity is not used and could be removed. ( Ignorable by Annotation )

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

69
    protected function addCustomAssets(/** @scrutinizer ignore-unused */ ?IStringerEntity $entity = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
70
    {
71
        $this->prepareCustomAssets();
72
    }
73
74
    protected function prepareCustomAssets()
75
    {
76
        $this->view->setVar('page', $this->getResourceName(static::RESOURCE_DEFAULT));
77
        $this->view->setVar('pageHeader', $this->getResourceName(static::RESOURCE_HEADER));
78
        $this->view->setVar('pageFooter', $this->getResourceName(static::RESOURCE_FOOTER));
79
        $this->view->setVar('pageType', $this->getResourceTypeName(static::RESOURCE_DEFAULT));
80
        $this->view->setVar('pageTypeHeader', $this->getResourceTypeName(static::RESOURCE_HEADER));
81
        $this->view->setVar('pageTypeFooter', $this->getResourceTypeName(static::RESOURCE_FOOTER));
82
    }
83
84
    /**
85
     * @param string $template
86
     *
87
     * @return string
88
     */
89
    protected function getResourceName(string $template)
90
    {
91
        return sprintf($template, static::ENTITY_SINGULAR);
92
    }
93
94
    /**
95
     * @param string $template
96
     *
97
     * @return string
98
     */
99
    protected function getResourceTypeName(string $template)
100
    {
101
        return sprintf($template, static::RESOURCE_TYPE);
102
    }
103
104
    /**
105
     * @param \Exception $exception
106
     *
107
     * @return array
108
     */
109
    protected function getExceptionContext(\Exception $exception): array
110
    {
111
        $result = [static::LOG_CONTEXT_EXCEPTION => $exception->getMessage()];
112
113
        $i = 1;
114
        while ($exception = $exception->getPrevious()) {
115
            $result[sprintf(static::LOG_PREVIOUS_EXCEPTION, $i++)] = $exception->getMessage();
116
        }
117
118
        return $result;
119
    }
120
}
121