Passed
Push — master ( 64ab61...b96172 )
by Gabor
09:36
created

FakeAction::getTemplateData()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 57
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 57
rs 9.6818
c 0
b 0
f 0
cc 3
eloc 34
nc 4
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace WebHemi\Middleware\Action;
4
5
use WebHemi\Application\SessionManager;
6
use WebHemi\Data\Entity\User\UserEntity;
7
use WebHemi\Data\Entity\User\UserGroupEntity;
8
use WebHemi\Data\Storage\User\UserStorage;
9
use WebHemi\Data\Coupler\UserToGroupCoupler;
10
use WebHemi\Data\Coupler\UserToPolicyCoupler;
11
use WebHemi\Data\Coupler\UserGroupToPolicyCoupler;
12
use WebHemi\Form\FormInterface;
13
use WebHemi\Form\Web\TestForm;
14
use WebHemi\Middleware\AbstractMiddlewareAction;
15
16
/**
17
 * Class FakeAction
18
 *
19
 * @codeCoverageIgnore - only for test purposes
20
 */
21
class FakeAction extends AbstractMiddlewareAction
22
{
23
    /** @var UserStorage */
24
    private $userStorage;
25
    /** @var UserToPolicyCoupler */
26
    private $userToPolicyCoupler;
27
    /** @var UserToGroupCoupler */
28
    private $userToGroupCoupler;
29
    /** @var UserGroupToPolicyCoupler */
30
    private $userGroupToPolicyCoupler;
31
    /** @var TestForm */
32
    private $loginForm;
33
    /** @var SessionManager */
34
    private $session;
35
36
    public function __construct(
37
        UserStorage $userStorage,
38
        FormInterface $loginForm,
39
        SessionManager $session,
40
        UserToPolicyCoupler $userToPolicyCoupler,
41
        UserToGroupCoupler $userToGroupCoupler,
42
        UserGroupToPolicyCoupler $userGroupToPolicyCoupler
43
    ) {
44
        $this->userStorage = $userStorage;
45
        $this->loginForm = $loginForm;
0 ignored issues
show
Documentation Bug introduced by
$loginForm is of type object<WebHemi\Form\FormInterface>, but the property $loginForm was declared to be of type object<WebHemi\Form\Web\TestForm>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
46
        $this->session = $session;
47
        $this->userToPolicyCoupler = $userToPolicyCoupler;
48
        $this->userToGroupCoupler = $userToGroupCoupler;
49
        $this->userGroupToPolicyCoupler = $userGroupToPolicyCoupler;
50
    }
51
52
    public function getTemplateName()
53
    {
54
        return 'blog-list';
55
    }
56
57
    public function getTemplateData()
0 ignored issues
show
Coding Style introduced by
getTemplateData uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
58
    {
59
        /** @var UserEntity $userEntity */
60
        $userEntity = $this->userStorage->getUserById(1);
61
62
        $policies = $this->userToPolicyCoupler->getEntityDependencies($userEntity);
63
        $groups = $this->userToGroupCoupler->getEntityDependencies($userEntity);
64
        $groupPolicies = [];
65
66
        /** @var UserGroupEntity $group */
67
        foreach ($groups as $group) {
68
            $groupPolicies[$group->getKeyData()] = $this->userGroupToPolicyCoupler->getEntityDependencies($group);
69
        }
70
71
72
        // Give special name
73
        $this->loginForm->setName('login');
74
        // Turn off aut complete feature.
75
        $this->loginForm->setAutoComplete(false);
76
        // test data setter
77
        $this->loginForm->setData((array)$this->request->getParsedBody());
78
79
        if (!empty($this->request->getParsedBody())) {
80
            $this->session->set('session', $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
81
            $this->session->regenerateId();
82
        }
83
84
        return [
85
            'blogPosts' => [
86
                [
87
                    'title'       => 'Fake test 1',
88
                    'slug'        => 'fake_1',
89
                    'publishedAt' => time(),
90
                    'author'      => [
91
                        'name' => $userEntity->getUserName()
92
                    ],
93
                    'content'     => 'Lorem ipsum dolor sit amet...'
94
                ],
95
                [
96
                    'title'       => 'Fake test 2',
97
                    'slug'        => 'fake_2',
98
                    'publishedAt' => time(),
99
                    'author'      => [
100
                        'name' => 'Jane Doe'
101
                    ],
102
                    'content'     => 'Lorem ipsum dolor sit amet...'
103
                ]
104
            ],
105
            'postData' => var_export($this->request->getParsedBody(), true),
106
            'session' => var_export($this->session->toArray(), true),
107
            'user' => var_export($userEntity, true),
108
            'policy' => var_export($policies, true),
109
            'group' => var_export($groups, true),
110
            'grouppolicy' => var_export($groupPolicies, true),
111
            'loginForm' => $this->loginForm
112
        ];
113
    }
114
}
115