Passed
Push — master ( 8587fa...100bd0 )
by Gabor
04:15
created

DashboardAction::getTemplateData()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 32
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 18
nc 2
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;
15
16
use WebHemi\Environment\ServiceInterface as EnvironmentInterface;
17
use WebHemi\Auth\ServiceInterface as AuthInterface;
18
use WebHemi\Middleware\Action\AbstractMiddlewareAction;
19
20
/**
21
 * Class DashboardAction.
22
 */
23
class DashboardAction extends AbstractMiddlewareAction
24
{
25
    /** @var AuthInterface */
26
    private $authAdapter;
27
    /** @var EnvironmentInterface */
28
    private $environmentManager;
29
30
    /**
31
     * DashboardAction constructor.
32
     *
33
     * @param AuthInterface        $authAdapter
34
     * @param EnvironmentInterface $environmentManager
35
     */
36
    public function __construct(
37
        AuthInterface $authAdapter,
38
        EnvironmentInterface $environmentManager
39
    ) {
40
        $this->authAdapter = $authAdapter;
41
        $this->environmentManager = $environmentManager;
42
    }
43
44
    /**
45
     * Gets template map name or template file path.
46
     *
47
     * @return string
48
     */
49
    public function getTemplateName() : string
50
    {
51
        return 'admin-dashboard';
52
    }
53
54
    /**
55
     * Gets template data.
56
     *
57
     * @return array
58
     */
59
    public function getTemplateData() : array
60
    {
61
        global $dependencyInjection;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
62
63
        $userEntity = $this->authAdapter->getIdentity();
64
        /** @var \WebHemi\Data\Coupler\UserToGroupCoupler $userToGroupCoupler */
65
        $userToGroupCoupler = $dependencyInjection->get(\WebHemi\Data\Coupler\UserToGroupCoupler::class);
66
        /** @var \WebHemi\Data\Coupler\UserToPolicyCoupler $userToPolicyCoupler */
67
        $userToPolicyCoupler = $dependencyInjection->get(\WebHemi\Data\Coupler\UserToPolicyCoupler::class);
68
        /** @var \WebHemi\Data\Coupler\UserGroupToPolicyCoupler $userToGroupToPolicyCoupler */
69
        $userToGroupToPolicyCoupler = $dependencyInjection->get(\WebHemi\Data\Coupler\UserGroupToPolicyCoupler::class);
70
71
        $userGroups = $userToGroupCoupler->getEntityDependencies($userEntity);
0 ignored issues
show
Bug introduced by
It seems like $userEntity defined by $this->authAdapter->getIdentity() on line 63 can be null; however, WebHemi\Data\Coupler\Abs...getEntityDependencies() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
72
        $userPolicies = $userToPolicyCoupler->getEntityDependencies($userEntity);
0 ignored issues
show
Bug introduced by
It seems like $userEntity defined by $this->authAdapter->getIdentity() on line 63 can be null; however, WebHemi\Data\Coupler\Abs...getEntityDependencies() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
73
74
        $userGroupPolicies = [];
75
76
        foreach ($userGroups as $userGroupEntity) {
77
            $userGroupPolicies = array_merge(
78
                $userGroupPolicies,
79
                $userToGroupToPolicyCoupler->getEntityDependencies($userGroupEntity)
80
            );
81
        }
82
83
        // @TODO TBD
84
        return [
85
            'user' => $userEntity,
86
            'user_groups' => $userGroups,
87
            'user_policies' => $userPolicies,
88
            'user_group_policies' => $userGroupPolicies,
89
        ];
90
    }
91
}
92