DefaultUrlChecker::getUrlFromRequest()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 7
ccs 3
cts 3
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
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
17
declare(strict_types=1);
18
19
namespace Phauthentic\Authentication\UrlChecker;
20
21
use Psr\Http\Message\ServerRequestInterface;
22
use Psr\Http\Message\UriInterface;
23
24
/**
25
 * Checks if a request object contains a valid URL
26
 */
27
class DefaultUrlChecker implements UrlCheckerInterface
28
{
29
    /**
30
     * @var bool
31
     */
32
    protected bool $checkFullUrl = false;
33
34
    /**
35
     * Check the full URL
36
     *
37
     * @param bool $fullUrl Full URL to check or not
38 8
     * @return $this
39
     */
40 8
    public function checkFullUrl(bool $fullUrl): self
41
    {
42 8
        $this->checkFullUrl = $fullUrl;
43
44
        return $this;
45
    }
46
47
    /**
48 44
     * {@inheritdoc}
49
     */
50 44
    public function check(ServerRequestInterface $request, string $loginUrl): bool
51
    {
52 44
        $requestUrl = $this->getUrlFromRequest($request->getUri());
53
54
        return $requestUrl === $loginUrl;
55
    }
56
57
    /**
58
     * Returns current url.
59
     *
60
     * @param \Psr\Http\Message\UriInterface $uri Server Request
61 44
     * @return string
62
     */
63 44
    protected function getUrlFromRequest(UriInterface $uri): string
64 8
    {
65
        if ($this->checkFullUrl) {
66
            return (string)$uri;
67 36
        }
68
69
        return $uri->getPath();
70
    }
71
}
72