Passed
Push — master ( a717b5...dd05c8 )
by Florian
03:07 queued 01:19
created

DefaultUrlChecker::_getUrlFromRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
declare(strict_types=1);
3
/**
4
 * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
5
 * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
6
 *
7
 * Licensed under The MIT License
8
 * For full copyright and license information, please see the LICENSE.txt
9
 * Redistributions of files must retain the above copyright notice.
10
 *
11
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
12
 * @link          https://cakephp.org CakePHP(tm) Project
13
 * @since         1.0.0
14
 * @license       https://opensource.org/licenses/mit-license.php MIT License
15
 */
16
namespace Phauthentic\Authentication\UrlChecker;
17
18
use Psr\Http\Message\ServerRequestInterface;
19
use Psr\Http\Message\UriInterface;
20
21
/**
22
 * Checks if a request object contains a valid URL
23
 */
24
class DefaultUrlChecker implements UrlCheckerInterface
25
{
26
27
    /**
28
     * @var bool
29
     */
30
    protected $checkFullUrl = false;
31
32
    /**
33
     * Check the full URL
34
     *
35
     * @param bool $fullUrl Full URL to check or not
36
     * @return $this
37
     */
38 6
    public function checkFullUrl(bool $fullUrl): self
39
    {
40 6
        $this->checkFullUrl = $fullUrl;
41
42 6
        return $this;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 33
    public function check(ServerRequestInterface $request, string $loginUrl): bool
49
    {
50 33
        $requestUrl = $this->getUrlFromRequest($request->getUri());
51
52 33
        return $requestUrl === $loginUrl;
53
    }
54
55
    /**
56
     * Returns current url.
57
     *
58
     * @param \Psr\Http\Message\UriInterface $uri Server Request
59
     * @return string
60
     */
61 33
    protected function getUrlFromRequest(UriInterface $uri): string
62
    {
63 33
        if ($this->checkFullUrl) {
64 6
            return (string)$uri;
65
        }
66
67 27
        return $uri->getPath();
68
    }
69
}
70