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

RegexUrlChecker   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
eloc 9
dl 0
loc 44
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A checkFullUrl() 0 5 1
A check() 0 5 1
A getUrlFromRequest() 0 7 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 using regular expression
23
 */
24
class RegexUrlChecker 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 12
    public function checkFullUrl(bool $fullUrl): self
39
    {
40 12
        $this->checkFullUrl = $fullUrl;
41
42 12
        return $this;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 21
    public function check(ServerRequestInterface $request, string $regex): bool
49
    {
50 21
        $requestUrl = $this->getUrlFromRequest($request->getUri());
51
52 21
        return (bool)preg_match($regex, $requestUrl);
53
    }
54
55
    /**
56
     * Returns current url.
57
     *
58
     * @param \Psr\Http\Message\UriInterface $uri Server Request
59
     * @return string
60
     */
61 21
    protected function getUrlFromRequest(UriInterface $uri): string
62
    {
63 21
        if ($this->checkFullUrl) {
64 12
            return (string)$uri;
65
        }
66
67 9
        return $uri->getPath();
68
    }
69
}
70