Completed
Branch master (73f336)
by John
01:59
created

BasicURLParser   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 37
c 0
b 0
f 0
wmc 3
lcom 0
cbo 3
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B parse() 0 28 3
1
<?php
2
namespace LunixREST\Request\URLParser;
3
4
use LunixREST\Request\RequestData\URLEncodedQueryStringRequestData;
5
use LunixREST\Request\URLParser\Exceptions\InvalidRequestURLException;
6
7
class BasicURLParser implements URLParser {
8
9
    /**
10
     * Parses API request data out of a url
11
     * @param string $url
12
     * @return ParsedURL
13
     * @throws InvalidRequestURLException
14
     */
15
    public function parse(string $url): ParsedURL {
16
        $splitURL = explode('/', trim($url, '/'));
17
        if(count($splitURL) < 3){
18
            throw new InvalidRequestURLException();
19
        }
20
        //Find endpoint
21
        $version = $splitURL[0];
22
        $apiKey = $splitURL[1];
23
        $endpoint = $splitURL[2];
24
25
        $splitExtension = explode('.', $splitURL[count($splitURL) - 1]);
26
        $extension = array_pop($splitExtension);
27
28
        $instance = null;
29
30
        if(count($splitURL) == 4){
31
            $instance = implode('.', $splitExtension);
32
        } else {
33
            $endpoint = implode('.', $splitExtension);
34
        }
35
36
        $splitOnQuery = explode('?', $url);
37
        $queryString = $splitOnQuery[1] ?? '';
38
39
        $requestData = new URLEncodedQueryStringRequestData($queryString);
40
41
        return new ParsedURL($requestData, $version, $apiKey, $endpoint, $extension, $instance);
42
    }
43
}
44