RequestHelper::getCurrentHost()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
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