Completed
Pull Request — master (#13)
by Guillem
02:24
created

Schema::getSchemes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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 18
    public function __construct(RequestDefinitions $requestDefinitions, $basePath = '', $host = null, array $schemes = ['http'])
29
    {
30 18
        foreach ($requestDefinitions as $request) {
31 15
            $this->addRequestDefinition($request);
32
        }
33 18
        $this->host = $host;
34 18
        $this->basePath = $basePath;
35 18
        $this->schemes = $schemes;
36 18
    }
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 12
    /**
64
     * @return \Generator
65
     */
66 12
    public function getRequestDefinitions()
67 1
    {
68
        foreach ($this->requestDefinitions as $operationId => $request) {
69
            yield $operationId => $request;
70 11
        }
71
    }
72
73
    /**
74
     * @return RequestDefinition
75
     */
76 1
    public function getRequestDefinition($operationId)
77
    {
78 1
79
        if (!isset($this->requestDefinitions[$operationId])) {
80
            throw new \InvalidArgumentException('Unable to get the request definition for '.$operationId);
81
        }
82
83
        return $this->requestDefinitions[$operationId];
84 1
    }
85
86 1
    /**
87
     * @return string
88
     */
89
    public function getHost()
90
    {
91
        return $this->host;
92 1
    }
93
94 1
    /**
95
     * @return string
96
     */
97 1
    public function getBasePath()
98
    {
99 1
        return $this->basePath;
100 1
    }
101 1
102 1
    /**
103 1
     * @return array
104
     */
105
    public function getSchemes()
106
    {
107 1
        return $this->schemes;
108
    }
109 1
110 1
    public function serialize()
111 1
    {
112 1
        return serialize([
113 1
            'host' => $this->host,
114 1
            'basePath' => $this->basePath,
115
            'schemes' => $this->schemes,
116 15
            'requests' => $this->requestDefinitions
117
        ]);
118 15
    }
119 15
120
    public function unserialize($serialized)
121
    {
122
        $data = unserialize($serialized);
123
        $this->host = $data['host'];
124
        $this->basePath = $data['basePath'];
125
        $this->schemes = $data['schemes'];
126
        $this->requestDefinitions = $data['requests'];
127
    }
128
129
    private function addRequestDefinition(RequestDefinition $request)
130
    {
131
        $this->requestDefinitions[$request->getOperationId()] = $request;
132
    }
133
}
134