AdminAbstract::addCustomAssets()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Admin\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
    public const ENTITY_PLURAL   = '';
17
    public const ENTITY_SINGULAR = '';
18
19
    public const ENTITY_LOAD_FAILURE = 'framework:load-failure';
20
21
    public const URL_EDIT = '%s-edit';
22
23
    public const RESOURCE_DEFAULT = '%s';
24
    public const RESOURCE_HEADER  = '%s-header';
25
    public const RESOURCE_FOOTER  = '%s-footer';
26
    public const RESOURCE_TYPE    = 'void';
27
28
    public const LOG_CONTEXT_EXCEPTION  = 'Exception';
29
    public const LOG_PREVIOUS_EXCEPTION = 'Previous exception #%d';
30
31
    public const ROUTING_PATH = '';
32
33
    /** @var ITranslator */
34
    protected ITranslator $translator;
35
36
    /** @var UrlGenerator */
37
    protected $urlGenerator;
38
39
    /** @var string */
40
    protected string $resource = '';
41
42
    /**
43
     * AdminAbstract constructor.
44
     *
45
     * @param FlashService    $flashService
46
     * @param LoggerInterface $logger
47
     * @param ITranslator     $translator
48
     * @param UrlGenerator    $urlGenerator
49
     */
50
    public function __construct(
51
        FlashService $flashService,
52
        LoggerInterface $logger,
53
        ITranslator $translator,
54
        UrlGenerator $urlGenerator
55
    ) {
56
        parent::__construct($flashService, $logger);
57
58
        $this->translator   = $translator;
59
        $this->urlGenerator = $urlGenerator;
60
    }
61
62
    /**
63
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
64
     *
65
     * @param IStringerEntity|null $entity
66
     */
67
    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

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