Completed
Push — master ( 675c2f...342161 )
by Guillem
01:39
created

Schema::isMatchingPath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
ccs 3
cts 4
cp 0.75
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2.0625
1
<?php declare(strict_types=1);
2
3
namespace ElevenLabs\Api;
4
5
use ElevenLabs\Api\Definition\RequestDefinition;
6
use ElevenLabs\Api\Definition\RequestDefinitions;
7
use Rize\UriTemplate;
8
9
class Schema implements \Serializable
10
{
11
    /** @var RequestDefinitions */
12
    private $requestDefinitions;
13
14
    /** @var string */
15
    private $host;
16
17
    /** @var string */
18
    private $basePath;
19
20
    /** @var array */
21
    private $schemes;
22
23
    /**
24
     * @param string[] $schemes
25
     */
26 19
    public function __construct(
27
        RequestDefinitions $requestDefinitions,
28
        string $basePath = '',
29
        string $host = '',
30
        array $schemes = ['http']
31
    ) {
32 19
        $this->requestDefinitions = $requestDefinitions;
33 19
        $this->host = $host;
34 19
        $this->basePath = $basePath;
35 19
        $this->schemes = $schemes;
36 19
    }
37
38
    /**
39
     * Find the operationId associated to a given path and method.
40
     *
41
     * @throws \InvalidArgumentException If no matching operation ID is found.
42
     */
43 2
    public function findOperationId(string $method, string $path): string
44
    {
45 2
        foreach ($this->requestDefinitions as $requestDefinition) {
46 1
            if ($requestDefinition->getMethod() !== $method) {
47
                continue;
48
            }
49 1
            if ($this->isMatchingPath($requestDefinition->getPathTemplate(), $path)) {
50 1
                return $requestDefinition->getOperationId();
51
            }
52
        }
53
54 1
        throw new \InvalidArgumentException('Unable to resolve the operationId for path ' . $path);
55
    }
56
57 1
    public function getRequestDefinitions(): RequestDefinitions
58
    {
59 1
        return $this->requestDefinitions;
60
    }
61
62 12
    public function getRequestDefinition(string $operationId): RequestDefinition
63
    {
64 12
        return $this->requestDefinitions->getRequestDefinition($operationId);
65
    }
66
67 1
    public function getHost(): string
68
    {
69 1
        return $this->host;
70
    }
71
72 1
    public function getBasePath(): string
73
    {
74 1
        return $this->basePath;
75
    }
76
77
    /**
78
     * @return string[]
79
     */
80 1
    public function getSchemes(): array
81
    {
82 1
        return $this->schemes;
83
    }
84
85
    // Serializable
86 1
    public function serialize()
87
    {
88 1
        return serialize([
89 1
            'host' => $this->host,
90 1
            'basePath' => $this->basePath,
91 1
            'schemes' => $this->schemes,
92 1
            'requests' => $this->requestDefinitions
93
        ]);
94
    }
95
96
    // Serializable
97 1
    public function unserialize($serialized)
98
    {
99 1
        $data = unserialize($serialized);
100 1
        $this->host = $data['host'];
101 1
        $this->basePath = $data['basePath'];
102 1
        $this->schemes = $data['schemes'];
103 1
        $this->requestDefinitions = $data['requests'];
104 1
    }
105
106 1
    private function isMatchingPath(string $pathTemplate, string $requestPath): bool
107
    {
108 1
        if ($pathTemplate === $requestPath) {
109
            return true;
110
        }
111
112 1
        return (new UriTemplate())->extract($pathTemplate, $requestPath, true) !== null;
113
    }
114
}
115