1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace OpenStack\Identity\v3\Models; |
4
|
|
|
|
5
|
|
|
use OpenStack\Common\Resource\Alias; |
6
|
|
|
use OpenStack\Common\Resource\OperatorResource; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @property \OpenStack\Identity\v3\Api $api |
10
|
|
|
*/ |
11
|
|
|
class Catalog extends OperatorResource implements \OpenStack\Common\Auth\Catalog |
12
|
|
|
{ |
13
|
|
|
/** @var []Service */ |
14
|
|
|
public $services; |
15
|
4 |
|
|
16
|
|
|
/** |
17
|
4 |
|
* @inheritdoc |
18
|
4 |
|
*/ |
19
|
4 |
|
protected function getAliases(): array |
20
|
4 |
|
{ |
21
|
|
|
return parent::getAliases() + [ |
22
|
|
|
'services' => new Alias('services', Service::class, true) |
23
|
|
|
]; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function populateFromArray(array $data): self |
27
|
|
|
{ |
28
|
|
|
foreach ($data as $service) { |
29
|
|
|
$this->services[] = $this->model(Service::class, $service); |
30
|
|
|
} |
31
|
|
|
|
32
|
5 |
|
return $this; |
33
|
|
|
} |
34
|
5 |
|
|
35
|
1 |
|
/** |
36
|
|
|
* Retrieve a base URL for a service, according to its catalog name, type, region. |
37
|
|
|
* |
38
|
4 |
|
* @param string $name The name of the service as it appears in the catalog. |
39
|
4 |
|
* @param string $type The type of the service as it appears in the catalog. |
40
|
2 |
|
* @param string $region The region of the service as it appears in the catalog. |
41
|
|
|
* @param string $urlType Unused. |
42
|
2 |
|
* |
43
|
|
|
* @return false|string FALSE if no URL found |
44
|
2 |
|
*/ |
45
|
|
|
public function getServiceUrl(string $name, string $type, string $region, string $urlType): string |
46
|
|
|
{ |
47
|
|
|
if (empty($this->services)) { |
48
|
|
|
throw new \RuntimeException('No services are defined'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
foreach ($this->services as $service) { |
52
|
|
|
if (false !== ($url = $service->getUrl($name, $type, $region, $urlType))) { |
|
|
|
|
53
|
|
|
return $url; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
throw new \RuntimeException(sprintf( |
58
|
|
|
"Endpoint URL could not be found in the catalog for this service.\nName: %s\nType: %s\nRegion: %s\nURL type: %s", |
59
|
|
|
$name, $type, $region, $urlType |
60
|
|
|
)); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: