1: | <?php |
2: | |
3: | /* |
4: | * Yate core products API wrapper library |
5: | * (c) Alexey Pavlyuts <alexey@pavlyuts.ru> |
6: | */ |
7: | |
8: | namespace Yate\Api; |
9: | |
10: | use Yate\Api\Exception\YateConfigException; |
11: | |
12: | /** |
13: | * Base Config impementation to use out-of-the-box |
14: | * |
15: | * You may extend with constructor which will take Yate core location and secrets |
16: | * from whereever you want: file, environments, e.t.c. |
17: | * |
18: | */ |
19: | class Config implements ConfigInterface |
20: | { |
21: | |
22: | protected array $uris = []; |
23: | protected array $secrets = []; |
24: | |
25: | /** |
26: | * Setup Yate API node records for one host/server |
27: | * |
28: | * Usual, multiple yate 'nodes' may reside on one host and share one API |
29: | * entry point. |
30: | * |
31: | * Then, we need each api entry point to be registered in the config with a lost of |
32: | * 'nodes' and it's access secret. |
33: | * |
34: | * Mind that each 'node' may only be registered once, mean if you include |
35: | * a 'node', say, 'hss' in two cosecutive calls of the methid, only last |
36: | * record will survive. |
37: | * |
38: | * @param string[] $nodes Node's list of component installed, like |
39: | * ['hss','smsc','ucn'] |
40: | * @param string $uri Node's api entry point, full URI like `'http://10.3.2.5/api.php'` |
41: | * @param string $secret Node's secret to authorise access |
42: | * @return self For method chaining |
43: | */ |
44: | public function withNode(array $nodes, string $uri, string $secret): self |
45: | { |
46: | foreach ($nodes as $node) { |
47: | $this->uris[$node] = $uri; |
48: | $this->secrets[$node] = $secret; |
49: | } |
50: | return $this; |
51: | } |
52: | |
53: | /** |
54: | * Setup Yate API nodes for multiple hosts/seervers |
55: | * |
56: | * In fact, it is a shorthand for iteration over array and send each array |
57: | * element to withNode() as: |
58: | * ``` |
59: | * withNode(...$element); |
60: | * ``` |
61: | * So, it is expected that each elemen is an array too and it's content match |
62: | * withNode() args by order, and, for future PHP versions, by key names. |
63: | * |
64: | * @param array[] $nodes array of withNode() argument arrays |
65: | * @return self For chaining |
66: | */ |
67: | public function withNodeList(iterable $nodes): self |
68: | { |
69: | foreach ($nodes as $node) { |
70: | $this->withNode(...$node); |
71: | } |
72: | return $this; |
73: | } |
74: | |
75: | /** |
76: | * Provides API URI for given node |
77: | * |
78: | * Should return API URI for e given node as string. If there no URI for a |
79: | * node configured, YateConfigException must be thrown. |
80: | * |
81: | * @param string $node |
82: | * @return string |
83: | * @throws YateConfigException |
84: | */ |
85: | public function getNodeUri(string $node): string |
86: | { |
87: | if (!isset($this->uris[$node])) { |
88: | throw new YateConfigException("Uri not found for node '$node'"); |
89: | } |
90: | return $this->uris[$node]; |
91: | } |
92: | |
93: | /** |
94: | * Provides access secret for given node |
95: | * |
96: | * Should return access secret for a given node as string. If there no secret for a |
97: | * node configured, YateConfigException must be thrown. |
98: | * |
99: | * @param string $node |
100: | * @return string |
101: | * @throws YateConfigException |
102: | */ |
103: | public function getNodeSecret(string $node): string |
104: | { |
105: | if (!isset($this->secrets[$node])) { |
106: | throw new YateConfigException("Secret not found for node '$node'"); |
107: | } |
108: | return $this->secrets[$node]; |
109: | } |
110: | } |
111: |