Passed
Push — master ( 5f8bab...af24cd )
by Gabor
04:02
created

ListAction::getTemplateName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * WebHemi.
4
 *
5
 * PHP version 7.1
6
 *
7
 * @copyright 2012 - 2017 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\Groups;
15
16
use RuntimeException;
17
use WebHemi\Configuration\ServiceInterface as ConfigurationInterface;
18
use WebHemi\Data\Entity\User\UserGroupEntity;
19
use WebHemi\Data\Storage\User\UserGroupStorage;
20
use WebHemi\Environment\ServiceInterface as EnvironmentInterface;
21
use WebHemi\Middleware\Action\AbstractMiddlewareAction;
22
23
/**
24
 * Class ListAction
25
 */
26
class ListAction extends AbstractMiddlewareAction
27
{
28
    /** @var ConfigurationInterface */
29
    protected $configuration;
30
    /** @var EnvironmentInterface */
31
    protected $environmentManager;
32
    /** @var UserGroupStorage */
33
    protected $userGroupStorage;
34
35
    /**
36
     * GroupManagementAction constructor.
37
     *
38
     * @param ConfigurationInterface $configuration
39
     * @param EnvironmentInterface $environmentManager
40
     * @param UserGroupStorage $userGroupStorage
41
     */
42
    public function __construct(
43
        ConfigurationInterface $configuration,
44
        EnvironmentInterface $environmentManager,
45
        UserGroupStorage $userGroupStorage
46
    ) {
47
        $this->configuration = $configuration;
48
        $this->environmentManager = $environmentManager;
49
        $this->userGroupStorage = $userGroupStorage;
50
    }
51
52
    /**
53
     * Gets template map name or template file path.
54
     *
55
     * @return string
56
     */
57
    public function getTemplateName() : string
58
    {
59
        return 'admin-control-panel-groups-list';
60
    }
61
62
    /**
63
     * Gets template data.
64
     *
65
     * @return array
66
     */
67
    public function getTemplateData() : array
68
    {
69
        $data = $this->getUserGroupList();
70
71
        return [
72
            'data' => $data
73
        ];
74
    }
75
76
    /**
77
     * Gets the whole user group record list.
78
     *
79
     * @return array
80
     */
81
    protected function getUserGroupList() : array
82
    {
83
        $dataList = [];
84
        $entityList = $this->userGroupStorage->getUserGroups();
85
86
        /** @var UserGroupEntity $userGroupEntity */
87
        foreach ($entityList as $userGroupEntity) {
0 ignored issues
show
Bug introduced by
The expression $entityList of type null|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
88
            $dataList[] = [
89
                'id' => $userGroupEntity->getUserGroupId(),
90
                'name' => $userGroupEntity->getName(),
91
                'title' => $userGroupEntity->getTitle(),
92
                'description' => $userGroupEntity->getDescription()
93
            ];
94
        }
95
96
        return $dataList;
97
    }
98
}
99