Schema   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Test Coverage

Coverage 95%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 14
eloc 35
dl 0
loc 110
ccs 38
cts 40
cp 0.95
rs 10
c 2
b 1
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getSchemes() 0 3 1
A getBasePath() 0 3 1
A addRequestDefinition() 0 3 1
A unserialize() 0 7 1
A getHost() 0 3 1
A findOperationId() 0 14 4
A serialize() 0 7 1
A getRequestDefinition() 0 7 2
A __construct() 0 8 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 19
     * @param array $schemes
27
     */
28
    public function __construct(RequestDefinitions $requestDefinitions, $basePath = '', $host = null, array $schemes = ['http'])
29
    {
30
        foreach ($requestDefinitions as $request) {
31
            $this->addRequestDefinition($request);
32 19
        }
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
     * @todo Implement a less expensive finder
42
     * @param string $method An HTTP method
43 2
     * @param string $path A path (ex: /foo/1)
44
     *
45 2
     * @return string The operationId
46 1
     */
47
    public function findOperationId($method, $path)
48
    {
49 1
        $uriTemplateManager = new UriTemplate();
50 1
        foreach ($this->requestDefinitions as $requestDefinition) {
51
            if ($requestDefinition->getMethod() !== $method) {
52
                continue;
53
            }
54 1
            $params = $uriTemplateManager->extract($requestDefinition->getPathTemplate(), $path, true);
55
            if ($params !== null) {
56
                return $requestDefinition->getOperationId();
57 1
            }
58
        }
59 1
60
        throw new \InvalidArgumentException('Unable to resolve the operationId for path ' . $path);
61
    }
62 12
63
    public function getRequestDefinition($operationId)
64 12
    {
65
        if (!isset($this->requestDefinitions[$operationId])) {
66
            throw new \InvalidArgumentException('Unable to get the request definition for '.$operationId);
67 1
        }
68
69 1
        return $this->requestDefinitions[$operationId];
70
    }
71
72 1
    /**
73
     * @return string
74 1
     */
75
    public function getHost()
76
    {
77
        return $this->host;
78
    }
79
80 1
    /**
81
     * @return string
82 1
     */
83
    public function getBasePath()
84
    {
85
        return $this->basePath;
86 1
    }
87
88 1
    /**
89 1
     * @return array
90 1
     */
91 1
    public function getSchemes()
92 1
    {
93
        return $this->schemes;
94
    }
95
96
    public function serialize()
97 1
    {
98
        return serialize([
99 1
            'host' => $this->host,
100 1
            'basePath' => $this->basePath,
101 1
            'schemes' => $this->schemes,
102 1
            'requests' => $this->requestDefinitions
103 1
        ]);
104 1
    }
105
106 1
    public function unserialize($serialized)
107
    {
108 1
        $data = unserialize($serialized);
109
        $this->host = $data['host'];
110
        $this->basePath = $data['basePath'];
111
        $this->schemes = $data['schemes'];
112 1
        $this->requestDefinitions = $data['requests'];
113
    }
114
115
    private function addRequestDefinition(RequestDefinition $request)
116
    {
117
        $this->requestDefinitions[$request->getOperationId()] = $request;
118
    }
119
}
120