Completed
Push — fetcher_factories ( 40fc6e...10a56f )
by David
02:17
created

RequireHttpsAnnotation::setValue()   B

Complexity

Conditions 8
Paths 12

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 17
rs 7.7777
cc 8
eloc 11
nc 12
nop 1
1
<?php
2
3
namespace Mouf\Mvc\Splash\Filters;
4
5
use Mouf\Mvc\Splash\Utils\SplashException;
6
7
/**
8
 * Filter that requires the use of HTTPS (if enabled in the conf)
9
 * By passing @RequireHttps("force"), an Exception is thrown if the action is called in HTTP.
10
 * By passing @RequireHttps("no"), no test is performed.
11
 * By passing @RequireHttps("redirect"), the call is redirected to HTTPS. Does only work with GET requests.
12
 *
13
 * @Annotation
14
 */
15
class RequireHttpsAnnotation
16
{
17
    /**
18
     * The value passed to the filter.
19
     */
20
    protected $value;
21
22
    public function __construct(array $values)
23
    {
24
        $value = $values['value'];
25
        if ($value === 'force') {
26
            $this->value = 'force';
27
        } elseif (strpos($value, 'no') !== false) {
28
            $this->value = 'no';
29
        } elseif (strpos($value, 'redirect') !== false) {
30
            $this->value = 'redirect';
31
        }
32
33
        if ($this->value === null) {
34
            throw new SplashException('You need to specify a value (either "force", "no" or "redirect") to the @RequireHttpsAnnotation.');
35
        }
36
    }
37
38 View Code Duplication
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next, ContainerInterface $container)
0 ignored issues
show
Unused Code introduced by
The parameter $container is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
39
    {
40
        $uri = $request->getUri();
41
        $scheme = $uri->getScheme();
42
        if ($scheme === 'http') {
43
            if ($request->getMethod() !== 'GET') {
44
                throw new SplashException('Only GET HTTP methods can be redirected to HTTPS');
45
            }
46
            $uri = $uri->withScheme('https');
47
            return new RedirectResponse($uri);
48
        }
49
50
        return $next($request, $response);
51
    }
52
53
}
54