Completed
Pull Request — 0.3.x (#21)
by
unknown
19:23
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
     *
56
     * @return RedirectResponse
57
     * @throws \LogicException
58
     * @throws \InvalidArgumentException
59
     */
60 5
    public function createRedirectResponse(Request $request, $path, $status = 302)
61
    {
62 5
        return new RedirectResponse($this->httpUtils->generateUri($request, $path), $status);
63
    }
64
65
    /**
66
     * @param Request $request
67
     * @param string  $path
68
     * @param int     $status
69
     *
70
     * @return RedirectResponse
71
     * @throws \LogicException
72
     * @throws \InvalidArgumentException
73
     */
74 2
    public function createSignedRedirectResponse(Request $request, $path, $status = 302)
75
    {
76 2
        return new RedirectResponse($this->httpUtils->generateUri($request, $this->uriSigner->sign($path)), $status);
77
    }
78
79
    /**
80
     * @param Request $request
81
     * @param string  $otp
82
     * @return string
83
     */
84 3
    public function createWrappedTargetPath(Request $request, $otp)
85
    {
86 3
        $targetPath = $request->get($this->targetPathParameter);
87
88 3
        return sprintf(
89 3
            (false === strpos($targetPath, '?')) ? '%s?%s=%s' : '%s&%s=%s',
90 3
            $targetPath,
91 3
            $this->otpParameter,
92 3
            rawurlencode($otp)
93
        );
94
    }
95
96
    /**
97
     * @param Request $request
98
     * @return bool
99
     */
100 6
    public function hasTargetPath(Request $request)
101
    {
102 6
        return $request->get($this->targetPathParameter) != '';
103
    }
104
105
    /**
106
     * @param string $uri
107
     * @return bool
108
     */
109 5
    public function checkUrl($uri)
110
    {
111 5
        return $this->uriSigner->check($uri);
112
    }
113
}
114