Completed
Pull Request — master (#20)
by Woody
02:21 queued 51s
created

Schema::unserialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 7
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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
    public function __construct(
27
        RequestDefinitions $requestDefinitions,
28 19
        string $basePath = '',
29
        string $host = '',
30 19
        array $schemes = ['http']
31 16
    ) {
32
        $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
    public function findOperationId(string $method, string $path): string
44
    {
45
        foreach ($this->requestDefinitions as $requestDefinition) {
46
            if ($requestDefinition->getMethod() !== $method) {
47 2
                continue;
48
            }
49 2
            if ($this->isMatchingPath($requestDefinition->getPathTemplate(), $path)) {
50 2
                return $requestDefinition->getOperationId();
51 1
            }
52
        }
53
54 1
        throw new \InvalidArgumentException('Unable to resolve the operationId for path ' . $path);
55 1
    }
56 1
57
    public function getRequestDefinitions(): RequestDefinitions
58
    {
59
        return $this->requestDefinitions;
60 1
    }
61
62
    public function getRequestDefinition(string $operationId): RequestDefinition
63
    {
64
        return $this->requestDefinitions->getRequestDefinition($operationId);
65
    }
66 1
67
    public function getHost(): string
68 1
    {
69 1
        return $this->host;
70
    }
71 1
72
    public function getBasePath(): string
73
    {
74
        return $this->basePath;
75
    }
76 12
77
    /**
78 12
     * @return string[]
79 1
     */
80
    public function getSchemes(): array
81
    {
82 11
        return $this->schemes;
83
    }
84
85
    // Serializable
86
    public function serialize()
87
    {
88 1
        return serialize([
89
            'host' => $this->host,
90 1
            'basePath' => $this->basePath,
91
            'schemes' => $this->schemes,
92
            'requests' => $this->requestDefinitions
93
        ]);
94
    }
95
96 1
    // Serializable
97
    public function unserialize($serialized)
98 1
    {
99
        $data = unserialize($serialized);
100
        $this->host = $data['host'];
101
        $this->basePath = $data['basePath'];
102
        $this->schemes = $data['schemes'];
103
        $this->requestDefinitions = $data['requests'];
104 1
    }
105
106 1
    private function isMatchingPath(string $pathTemplate, string $requestPath): bool
107
    {
108
        if ($pathTemplate === $requestPath) {
109 1
            return true;
110
        }
111 1
112 1
        return (new UriTemplate())->extract($pathTemplate, $requestPath, true) !== null;
113 1
    }
114
}
115