Test Failed
Push — master ( bd39d1...7116ad )
by Gabor
08:54
created

ListAction::getTemplateName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * WebHemi.
4
 *
5
 * PHP version 7.2
6
 *
7
 * @copyright 2012 - 2019 Gixx-web (http://www.gixx-web.com)
8
 * @license   https://opensource.org/licenses/MIT The MIT License (MIT)
9
 *
10
 * @link http://www.gixx-web.com
11
 */
12
declare(strict_types = 1);
13
14
namespace WebHemi\Middleware\Action\Admin\ControlPanel\Applications;
15
16
use WebHemi\CSRF\ServiceInterface as CSRFInterface;
17
use WebHemi\Data\Entity\ApplicationEntity;
18
use WebHemi\Data\Entity\DomainEntity;
19
use WebHemi\Data\Entity\EntitySet;
20
use WebHemi\Data\Storage\ApplicationStorage;
21
use WebHemi\Data\Storage\DomainStorage;
22
use WebHemi\Form\PresetInterface;
23
use WebHemi\Form\ServiceAdapter\Base\ServiceAdapter as HtmlForm;
24
use WebHemi\Middleware\Action\AbstractMiddlewareAction;
25
26
/**
27
 * Class ListAction.
28
 */
29
class ListAction extends AbstractMiddlewareAction
30
{
31
    /**
32
     * @var ApplicationStorage
33
     */
34
    protected $applicationStorage;
35
    /**
36
     * @var DomainStorage
37
     */
38
    protected $domainStorage;
39
    /**
40
     * @var PresetInterface
41
     */
42
    protected $applicationFormPreset;
43
    /**
44
     * @var CSRFInterface
45
     */
46
    protected $csrfAdapter;
47
48
    /**
49
     * IndexAction constructor.
50
     *
51
     * @param ApplicationStorage     $applicationStorage
52
     * @param DomainStorage          $domainStorage
53
     * @param PresetInterface        $applicationFormPreset
54
     * @param CSRFInterface          $csrfAdapter
55
     */
56
    public function __construct(
57
        ApplicationStorage $applicationStorage,
58
        DomainStorage $domainStorage,
59
        PresetInterface $applicationFormPreset,
60
        CSRFInterface $csrfAdapter
61
    ) {
62
        $this->applicationStorage = $applicationStorage;
63
        $this->domainStorage = $domainStorage;
64
        $this->applicationFormPreset = $applicationFormPreset;
65
        $this->csrfAdapter = $csrfAdapter;
66
    }
67
68
    /**
69
     * Gets template map name or template file path.
70
     *
71
     * @return string
72
     */
73
    public function getTemplateName() : string
74
    {
75
        return 'admin-control-panel-applications-list';
76
    }
77
78
    /**
79
     * Gets template data.
80
     *
81
     * @return array
82
     */
83
    public function getTemplateData() : array
84
    {
85
        /** @var DomainEntity[] $domains */
86
        $domains = [];
87
88
        /** @var EntitySet $applications */
89
        $applications = $this->applicationStorage->getApplicationList();
90
91
        /** @var ApplicationEntity $applicationEntity */
92
        foreach ($applications as $key => $applicationEntity) {
93
            $domainId = $applicationEntity->getDomainId();
94
95
            if (!isset($domains[$domainId])) {
96
                $domains[$domainId] = $this->domainStorage->getDomainById($domainId);
97
            }
98
99
            if ($domains[$domainId] instanceof DomainEntity) {
100
                $applicationEntity->setReference(ApplicationEntity::REFERENCE_DOMAIN, $domains[$domainId]);
101
                $applications->offsetSet($key, $applicationEntity);
0 ignored issues
show
Bug introduced by
It seems like $key can also be of type string; however, parameter $offset of WebHemi\Data\Entity\EntitySet::offsetSet() does only seem to accept integer|null, maybe add an additional type check? ( Ignorable by Annotation )

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

101
                $applications->offsetSet(/** @scrutinizer ignore-type */ $key, $applicationEntity);
Loading history...
102
            }
103
        }
104
105
        /** @var HtmlForm $form */
106
        $form = $this->applicationFormPreset->getPreset();
107
108
        $csrfToken = $this->csrfAdapter->generate(180);
109
110
        $csrfElement = $form->getElement(CSRFInterface::SESSION_KEY);
111
        $csrfElement->setValues([$csrfToken]);
112
113
        return [
114
            'data' => $applications,
115
            'form' => $form,
116
            CSRFInterface::SESSION_KEY => $csrfToken
117
        ];
118
    }
119
}
120