Completed
Push — master ( b013a4...773920 )
by Guillem
01:52
created

Schema::getRequestDefinitions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

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