Completed
Pull Request — 0.3.x (#17)
by Arnaud
05:01
created

HttpUtils::checkUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Krtv\Bundle\SingleSignOnIdentityProviderBundle\Security\Http;
4
5
use Symfony\Component\HttpFoundation\RedirectResponse;
6
use Symfony\Component\HttpFoundation\Request;
7
use Symfony\Component\HttpKernel\UriSigner;
8
use Symfony\Component\Security\Http;
9
10
/**
11
 * Class HttpUtils
12
 * @package Krtv\Bundle\SingleSignOnIdentityProviderBundle\Security\Http
13
 */
14
class HttpUtils
15
{
16
    /**
17
     * @var UriSigner
18
     */
19
    private $uriSigner;
20
21
    /**
22
     * @var Http\HttpUtils
23
     */
24
    private $httpUtils;
25
26
    /**
27
     * @var string
28
     */
29
    private $otpParameter;
30
31
    /**
32
     * @var string
33
     */
34
    private $targetPathParameter;
35
36
    /**
37
     * @param UriSigner      $uriSigner
38
     * @param Http\HttpUtils $httpUtils
39
     * @param string         $otpParameter
40
     * @param string         $targetPathParameter
41
     */
42 7
    public function __construct(UriSigner $uriSigner, Http\HttpUtils $httpUtils, $otpParameter, $targetPathParameter)
43
    {
44 7
        $this->uriSigner = $uriSigner;
45 7
        $this->httpUtils = $httpUtils;
46
47 7
        $this->otpParameter = $otpParameter;
48 7
        $this->targetPathParameter = $targetPathParameter;
49 7
    }
50
51
    /**
52
     * @param Request $request
53
     * @param string  $path
54
     * @param int     $status
55
     * @return RedirectResponse
56
     */
57 5
    public function createRedirectResponse(Request $request, $path, $status = 302)
58
    {
59 5
        return $this->httpUtils->createRedirectResponse($request, $path, $status);
60
    }
61
62
    /**
63
     * @param Request $request
64
     * @param string  $path
65
     * @param int     $status
66
     * @return RedirectResponse
67
     */
68 2
    public function createSignedRedirectResponse(Request $request, $path, $status = 302)
69
    {
70 2
        return $this->httpUtils->createRedirectResponse($request, $this->uriSigner->sign($path), $status);
71
    }
72
73
    /**
74
     * @param Request $request
75
     * @param string  $otp
76
     * @return string
77
     */
78 3
    public function createWrappedTargetPath(Request $request, $otp)
79
    {
80 3
        $targetPath = $request->get($this->targetPathParameter);
81
82 3
        return sprintf(
83 3
            (false === strpos($targetPath, '?')) ? '%s?%s=%s' : '%s&%s=%s',
84
            $targetPath,
85 3
            $this->otpParameter,
86 3
            rawurlencode($otp)
87
        );
88
    }
89
90
    /**
91
     * @param Request $request
92
     * @return bool
93
     */
94 6
    public function hasTargetPath(Request $request)
95
    {
96 6
        return $request->get($this->targetPathParameter) != '';
97
    }
98
99
    /**
100
     * @param string $uri
101
     * @return bool
102
     */
103 5
    public function checkUrl($uri)
104
    {
105 5
        return $this->uriSigner->check($uri);
106
    }
107
}
108