RequestHelper   B
last analyzed

Complexity

Total Complexity 40

Size/Duplication

Total Lines 221
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 40
lcom 1
cbo 3
dl 0
loc 221
rs 8.2608
c 0
b 0
f 0

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getCurrentRequest() 0 8 2
A getCurrentHost() 0 8 2
A getSessionAttribute() 0 8 3
A setSessionAttribute() 0 8 3
A hasSessionAttribute() 0 8 3
A getSessionId() 0 8 3
A getSessionName() 0 8 3
A hasRequestBagParam() 0 8 2
A hasRequestBagParams() 0 10 3
A getRequestBagParam() 0 8 2
A getQueryBagParam() 0 8 3
A hasAttributesBagParam() 0 8 2
A hasAttributesBagParams() 0 10 3
A getAttributesBagParam() 0 8 2
A getCurrentLocale() 0 8 2
A getCurrentCurrency() 0 4 1

How to fix   Complexity   

Complex Class

Complex classes like RequestHelper often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use RequestHelper, and based on these observations, apply Extract Interface, too.

1
<?php
2
/*
3
 * WellCommerce Open-Source E-Commerce Platform
4
 *
5
 * This file is part of the WellCommerce package.
6
 *
7
 * (c) Adam Piotrowski <[email protected]>
8
 *
9
 * For the full copyright and license information,
10
 * please view the LICENSE file that was distributed with this source code.
11
 */
12
13
namespace WellCommerce\Bundle\CoreBundle\Helper\Request;
14
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\HttpFoundation\RequestStack;
17
18
/**
19
 * Class RequestHelper
20
 *
21
 * @author  Adam Piotrowski <[email protected]>
22
 */
23
final class RequestHelper implements RequestHelperInterface
24
{
25
    /**
26
     * @var RequestStack
27
     */
28
    private $requestStack;
29
    
30
    /**
31
     * @var null|Request
32
     */
33
    private $request;
34
    
35
    /**
36
     * @var string
37
     */
38
    private $fallbackLocale;
39
    
40
    /**
41
     * RequestHelper constructor.
42
     *
43
     * @param RequestStack $requestStack
44
     * @param string       $fallbackLocale
45
     */
46
    public function __construct(RequestStack $requestStack, string $fallbackLocale)
47
    {
48
        $this->requestStack   = $requestStack;
49
        $this->fallbackLocale = $fallbackLocale;
50
    }
51
    
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function getCurrentRequest()
56
    {
57
        if (null === $this->request) {
58
            $this->request = $this->requestStack->getMasterRequest();
59
        }
60
        
61
        return $this->request;
62
    }
63
    
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function getCurrentHost()
68
    {
69
        if (!$this->getCurrentRequest() instanceof Request) {
70
            return null;
71
        }
72
        
73
        return $this->getCurrentRequest()->getHost();
74
    }
75
    
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function getSessionAttribute(string $name, $default = null)
80
    {
81
        if (null !== $this->getCurrentRequest() && $this->request->hasSession()) {
82
            return $this->request->getSession()->get($name, $default);
83
        }
84
        
85
        return $default;
86
    }
87
    
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function setSessionAttribute(string $name, $value)
92
    {
93
        if (null === $this->getCurrentRequest() || false === $this->request->hasSession()) {
94
            throw new \LogicException('Cannot set session attributes without valid session.');
95
        }
96
        
97
        return $this->request->getSession()->set($name, $value);
98
    }
99
    
100
    /**
101
     * {@inheritdoc}
102
     */
103
    public function hasSessionAttribute(string $name): bool
104
    {
105
        if (null !== $this->getCurrentRequest() && $this->request->hasSession()) {
106
            return $this->request->getSession()->has($name);
107
        }
108
        
109
        return false;
110
    }
111
    
112
    /**
113
     * {@inheritdoc}
114
     */
115
    public function getSessionId(): string
116
    {
117
        if (null !== $this->getCurrentRequest() && $this->request->hasSession()) {
118
            return $this->request->getSession()->getId();
119
        }
120
        
121
        return '';
122
    }
123
    
124
    /**
125
     * {@inheritdoc}
126
     */
127
    public function getSessionName(): string
128
    {
129
        if (null !== $this->getCurrentRequest() && $this->request->hasSession()) {
130
            return $this->request->getSession()->getName();
131
        }
132
        
133
        return '';
134
    }
135
    
136
    /**
137
     * {@inheritdoc}
138
     */
139
    public function hasRequestBagParam(string $name): bool
140
    {
141
        if ($this->getCurrentRequest() instanceof Request) {
142
            return $this->request->request->has($name);
143
        }
144
        
145
        return false;
146
    }
147
    
148
    /**
149
     * {@inheritdoc}
150
     */
151
    public function hasRequestBagParams(array $params = []): bool
152
    {
153
        foreach ($params as $param) {
154
            if (!$this->hasRequestBagParam($param)) {
155
                return false;
156
            }
157
        }
158
        
159
        return true;
160
    }
161
    
162
    /**
163
     * {@inheritdoc}
164
     */
165
    public function getRequestBagParam(string $name, $default = null, int $filter = FILTER_SANITIZE_SPECIAL_CHARS)
166
    {
167
        if (false === $this->hasRequestBagParam($name)) {
168
            return $default;
169
        }
170
        
171
        return $this->request->request->filter($name, $default, $filter);
172
    }
173
    
174
    /**
175
     * {@inheritdoc}
176
     */
177
    public function getQueryBagParam(string $name, $default = null, int $filter = FILTER_SANITIZE_SPECIAL_CHARS)
178
    {
179
        if (null === $this->getCurrentRequest() || false === $this->request->query->has($name)) {
180
            return $default;
181
        }
182
        
183
        return $this->request->query->filter($name, $default, $filter);
184
    }
185
    
186
    /**
187
     * {@inheritdoc}
188
     */
189
    public function hasAttributesBagParam(string $name): bool
190
    {
191
        if ($this->getCurrentRequest() instanceof Request) {
192
            return $this->request->attributes->has($name);
193
        }
194
        
195
        return false;
196
    }
197
    
198
    /**
199
     * {@inheritdoc}
200
     */
201
    public function hasAttributesBagParams(array $params = []): bool
202
    {
203
        foreach ($params as $param) {
204
            if (!$this->hasAttributesBagParam($param)) {
205
                return false;
206
            }
207
        }
208
        
209
        return true;
210
    }
211
    
212
    /**
213
     * {@inheritdoc}
214
     */
215
    public function getAttributesBagParam(string $name, $default = null, int $filter = FILTER_SANITIZE_SPECIAL_CHARS)
216
    {
217
        if (false === $this->hasAttributesBagParam($name)) {
218
            return $default;
219
        }
220
        
221
        return $this->request->attributes->filter($name, $default, $filter);
222
    }
223
    
224
    /**
225
     * {@inheritdoc}
226
     */
227
    public function getCurrentLocale(): string
228
    {
229
        if (null !== $this->getCurrentRequest()) {
230
            return $this->getCurrentRequest()->getLocale();
231
        }
232
        
233
        return $this->fallbackLocale;
234
    }
235
    
236
    /**
237
     * {@inheritdoc}
238
     */
239
    public function getCurrentCurrency(): string
240
    {
241
        return $this->getSessionAttribute('_currency', '');
242
    }
243
}
244