|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace OpenStack\ObjectStore\v1; |
|
4
|
|
|
|
|
5
|
|
|
use OpenStack\Common\Error\BadResponseError; |
|
6
|
|
|
use OpenStack\Common\Service\AbstractService; |
|
7
|
|
|
use OpenStack\ObjectStore\v1\Models\Account; |
|
8
|
|
|
use OpenStack\ObjectStore\v1\Models\Container; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @property \OpenStack\ObjectStore\v1\Api $api |
|
12
|
|
|
*/ |
|
13
|
|
|
class Service extends AbstractService |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Retrieves an Account object. |
|
17
|
|
|
* |
|
18
|
|
|
* @return Account |
|
19
|
|
|
*/ |
|
20
|
1 |
|
public function getAccount(): Account |
|
21
|
|
|
{ |
|
22
|
1 |
|
return $this->model(Account::class); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Retrieves a collection of container resources in a generator format. |
|
27
|
|
|
* |
|
28
|
|
|
* @param array $options {@see \OpenStack\ObjectStore\v1\Api::getAccount} |
|
29
|
|
|
* @param callable|null $mapFn Allows a function to be mapped over each element in the collection. |
|
30
|
|
|
* |
|
31
|
|
|
* @return \Generator |
|
32
|
|
|
*/ |
|
33
|
1 |
|
public function listContainers(array $options = [], callable $mapFn = null): \Generator |
|
34
|
|
|
{ |
|
35
|
1 |
|
$options = array_merge($options, ['format' => 'json']); |
|
36
|
1 |
|
return $this->model(Container::class)->enumerate($this->api->getAccount(), $options, $mapFn); |
|
|
|
|
|
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Retrieves a Container object and populates its name according to the value provided. Please note that the |
|
41
|
|
|
* remote API is not contacted. |
|
42
|
|
|
* |
|
43
|
|
|
* @param string $name The unique name of the container |
|
44
|
|
|
* |
|
45
|
|
|
* @return Container |
|
46
|
|
|
*/ |
|
47
|
2 |
|
public function getContainer(string $name = null): Container |
|
48
|
|
|
{ |
|
49
|
2 |
|
return $this->model(Container::class, ['name' => $name]); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Creates a new container according to the values provided. |
|
54
|
|
|
* |
|
55
|
|
|
* @param array $data {@see \OpenStack\ObjectStore\v1\Api::putContainer} |
|
56
|
|
|
* |
|
57
|
|
|
* @return Container |
|
58
|
|
|
*/ |
|
59
|
2 |
|
public function createContainer(array $data): Container |
|
60
|
|
|
{ |
|
61
|
2 |
|
return $this->getContainer()->create($data); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Checks the existence of a container. |
|
66
|
|
|
* |
|
67
|
|
|
* @param string $name The name of the container |
|
68
|
|
|
* |
|
69
|
|
|
* @return bool TRUE if exists, FALSE if it doesn't |
|
70
|
|
|
* @throws BadResponseError Thrown for any non 404 status error |
|
71
|
|
|
*/ |
|
72
|
4 |
View Code Duplication |
public function containerExists(string $name): bool |
|
|
|
|
|
|
73
|
|
|
{ |
|
74
|
|
|
try { |
|
75
|
4 |
|
$this->execute($this->api->headContainer(), ['name' => $name]); |
|
76
|
1 |
|
return true; |
|
77
|
3 |
|
} catch (BadResponseError $e) { |
|
78
|
3 |
|
if ($e->getResponse()->getStatusCode() === 404) { |
|
79
|
2 |
|
return false; |
|
80
|
|
|
} |
|
81
|
1 |
|
throw $e; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
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: