RegexURLParser::parse()   B
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 17
cts 17
cp 1
rs 8.439
c 0
b 0
f 0
cc 6
eloc 19
nc 6
nop 1
crap 6
1
<?php
2
namespace LunixREST\RequestFactory\URLParser\RegexURLParser;
3
4
use LunixREST\RequestFactory\URLParser\Exceptions\UnableToParseURLException;
5
use LunixREST\RequestFactory\URLParser\Exceptions\UnableToProvideMIMEException;
6
use LunixREST\RequestFactory\URLParser\MIMEProvider;
7
use LunixREST\RequestFactory\URLParser\ParsedURL;
8
use LunixREST\RequestFactory\URLParser\RegexURLParser\Exceptions\InvalidRegexPatternException;
9
use LunixREST\RequestFactory\URLParser\Exceptions\InvalidRequestURLException;
10
use LunixREST\RequestFactory\URLParser\URLParser;
11
use Psr\Http\Message\UriInterface;
12
13
14
/**
15
 * An implementation of a URLParser that uses named regex capture groups to parse urls.
16
 * Class RegexURLParser
17
 * @package LunixREST\RequestFactory\URLParser\RegexURLParser
18
 */
19
class RegexURLParser implements URLParser
20
{
21
    /**
22
     * @var string
23
     */
24
    protected $pattern;
25
    /**
26
     * @var MIMEProvider|null
27
     */
28
    private $MIMEProvider;
29
30
    /**
31
     * RegexURLParser constructor.
32
     * @param string $pattern - a pattern that matches named groups: endpoint. And optionally: element, version, apiKey, acceptableExtension
33
     * @param MIMEProvider|null $MIMEProvider
34
     * @throws InvalidRegexPatternException
35
     */
36 10
    public function __construct(string $pattern, ?MIMEProvider $MIMEProvider = null)
37
    {
38 10
        $this->pattern = $pattern;
39
40 10
        if(@preg_match($pattern, null) === false) {
41 1
            throw new InvalidRegexPatternException("Unable to parse regex pattern", preg_last_error());
42
        }
43 9
        $this->MIMEProvider = $MIMEProvider;
44 9
    }
45
46
    /**
47
     * Parses API request data out of a url
48
     * @param UriInterface $uri
49
     * @return ParsedURL
50
     * @throws UnableToParseURLException
51
     * @throws InvalidRequestURLException
52
     */
53 8
    public function parse(UriInterface $uri): ParsedURL
54
    {
55 8
        $matches = [];
56 8
        if(preg_match($this->pattern, $uri->getPath(), $matches) === 0) {
57 1
            throw new InvalidRequestURLException("Unable to parse request path: did not match regex");
58
        }
59 7
        if(!($endpoint = $matches["endpoint"] ?? null)) {
60 1
            throw new InvalidRequestURLException("Unable to match endpoint in url");
61
        }
62 6
        $element = $matches["element"] ?? null;
63 6
        $version = $matches["version"] ?? null;
64 6
        $apiKey = $matches["apiKey"] ?? null;
65
66 6
        $acceptableMimeTypes = [];
67 6
        if(($acceptableExtension = $matches["acceptableExtension"] ?? null)) {
68 3
            if(!$this->MIMEProvider) {
69 1
                throw new UnableToParseURLException("Unable to accept acceptable extensions");
70
            } else {
71
                try {
72 2
                    $acceptableMimeTypes[] = $this->MIMEProvider->provideMIME($acceptableExtension);
73 1
                } catch (UnableToProvideMIMEException $exception) {
74 1
                    throw new UnableToParseURLException($exception->getMessage());
75
                }
76
            }
77
        }
78
79 4
        return new ParsedURL($endpoint, $element, $version, $apiKey, $acceptableMimeTypes, $uri->getQuery());
80
    }
81
82
    /**
83
     * @return string
84
     */
85 1
    public function getPattern(): string
86
    {
87 1
        return $this->pattern;
88
    }
89
90
}
91