AjaxcomSymfony4Trait::setAjaxcomRequiredServices()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 20
rs 9.9666
cc 1
nc 1
nop 9

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Everlution\AjaxcomBundle\Controller;
6
7
use Everlution\AjaxcomBundle\DataObject\Callback;
8
use Everlution\AjaxcomBundle\Mutation;
9
use Everlution\AjaxcomBundle\Service\Ajaxcom;
10
use Symfony\Component\HttpFoundation\RequestStack;
11
use Symfony\Component\HttpFoundation\Response;
12
13
trait AjaxcomSymfony4Trait
14
{
15
    /**
16
     * @var RequestStack
17
     */
18
    private $requestStack;
19
    /**
20
     * @var Ajaxcom
21
     */
22
    private $ajaxcom;
23
    /**
24
     * @var Mutation\AddBlocks
25
     */
26
    private $ajaxcomAddBlocks;
27
    /**
28
     * @var Mutation\AppendBlocks
29
     */
30
    private $ajaxcomAppendBlocks;
31
    /**
32
     * @var Mutation\RemoveBlocks
33
     */
34
    private $ajaxcomRemoveBlocks;
35
    /**
36
     * @var Mutation\Callbacks
37
     */
38
    private $ajaxcomCallbacks;
39
    /**
40
     * @var Mutation\ReplaceClass
41
     */
42
    private $ajaxcomReplaceClass;
43
    /**
44
     * @var Mutation\ChangeUrl
45
     */
46
    private $ajaxcomChangeUrl;
47
    /**
48
     * @var Mutation\PrependBlocks
49
     */
50
    private $ajaxcomPrependBlocks;
51
52
    /** @required */
53
    public function setAjaxcomRequiredServices(
54
        RequestStack $requestStack,
55
        Ajaxcom $ajaxcom,
56
        Mutation\AddBlocks $ajaxcomAddBlocks,
57
        Mutation\AppendBlocks $ajaxcomAppendBlocks,
58
        Mutation\RemoveBlocks $ajaxcomRemoveBlocks,
59
        Mutation\Callbacks $ajaxcomCallbacks,
60
        Mutation\ReplaceClass $ajaxcomReplaceClass,
61
        Mutation\ChangeUrl $ajaxcomChangeUrl,
62
        Mutation\PrependBlocks $ajaxcomPrependBlocks
63
    ): void {
64
        $this->requestStack = $requestStack;
65
        $this->ajaxcom = $ajaxcom;
66
        $this->ajaxcomAddBlocks = $ajaxcomAddBlocks;
67
        $this->ajaxcomAppendBlocks = $ajaxcomAppendBlocks;
68
        $this->ajaxcomRemoveBlocks = $ajaxcomRemoveBlocks;
69
        $this->ajaxcomCallbacks = $ajaxcomCallbacks;
70
        $this->ajaxcomReplaceClass = $ajaxcomReplaceClass;
71
        $this->ajaxcomChangeUrl = $ajaxcomChangeUrl;
72
        $this->ajaxcomPrependBlocks = $ajaxcomPrependBlocks;
73
    }
74
75
    protected function render(string $view, array $parameters = array(), Response $response = null): Response
76
    {
77
        $request = $this->requestStack->getMainRequest();
0 ignored issues
show
Unused Code introduced by
The assignment to $request is dead and can be removed.
Loading history...
78
79
        if ($this->isAjax()) {
80
            return $this->ajaxcom->handle($view, $parameters);
81
        }
82
83
        return parent::render($view, $parameters, $response);
84
    }
85
86
    protected function renderAjaxBlock(string $id): self
87
    {
88
        $this->ajaxcomAddBlocks->add($id);
89
90
        return $this;
91
    }
92
93
    protected function dontRenderAjaxBlock(string $id): self
94
    {
95
        $this->ajaxcomAddBlocks->remove($id);
96
97
        return $this;
98
    }
99
100
    protected function addAjaxBlock(string $id): self
101
    {
102
        $this->ajaxcomAddBlocks->add($id);
103
104
        return $this;
105
    }
106
107
    protected function appendAjaxBlock(string $id): self
108
    {
109
        $this->ajaxcomAppendBlocks->add($id);
110
111
        return $this;
112
    }
113
114
    protected function refreshAjaxBlock(string $id): self
115
    {
116
        $this->ajaxcomAddBlocks->refresh($id);
117
118
        return $this;
119
    }
120
121
    protected function removeAjaxBlock(string $selector): self
122
    {
123
        $this->ajaxcomRemoveBlocks->add($selector);
124
125
        return $this;
126
    }
127
128
    protected function addCallback(string $functionName, array $parameters = []): self
129
    {
130
        $this->ajaxcomCallbacks->add(new Callback($functionName, $parameters));
131
132
        return $this;
133
    }
134
135
    protected function replaceClass(string $selector, string $class): self
136
    {
137
        $this->ajaxcomReplaceClass->add($selector, $class);
138
139
        return $this;
140
    }
141
142
    protected function doNotChangeUrl(): self
143
    {
144
        $this->ajaxcomChangeUrl->doNotChangeUrl();
145
146
        return $this;
147
    }
148
149
    protected function prependAjaxBlock(string $id): self
150
    {
151
        $this->ajaxcomPrependBlocks->add($id);
152
153
        return $this;
154
    }
155
156
    protected function isAjax(): bool
157
    {
158
        /** @var Request $request */
159
        $request = $this->requestStack->getMainRequest();
160
        $isAjax = $request->server->getBoolean(Ajaxcom::AJAX_COM_HEADER, false);
161
162
        return $isAjax;
163
    }
164
}
165