1
|
|
|
<?php |
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() |
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) |
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($name = null) |
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) |
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($name) |
|
|
|
|
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
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.