1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace OpenStack\BlockStorage\v2\Models; |
4
|
|
|
|
5
|
|
|
use OpenStack\Common\Resource\Alias; |
6
|
|
|
use OpenStack\Common\Resource\OperatorResource; |
7
|
|
|
use OpenStack\Common\Resource\Creatable; |
8
|
|
|
use OpenStack\Common\Resource\Deletable; |
9
|
|
|
use OpenStack\Common\Resource\HasMetadata; |
10
|
|
|
use OpenStack\Common\Resource\HasWaiterTrait; |
11
|
|
|
use OpenStack\Common\Resource\Listable; |
12
|
|
|
use OpenStack\Common\Resource\Retrievable; |
13
|
|
|
use OpenStack\Common\Resource\Updateable; |
14
|
|
|
use OpenStack\Common\Transport\Utils; |
15
|
|
|
use Psr\Http\Message\ResponseInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @property \OpenStack\BlockStorage\v2\Api $api |
19
|
|
|
*/ |
20
|
|
|
class Snapshot extends OperatorResource implements Listable, Creatable, Updateable, Deletable, Retrievable, HasMetadata |
21
|
|
|
{ |
22
|
|
|
use HasWaiterTrait; |
23
|
|
|
|
24
|
|
|
/** @var string */ |
25
|
|
|
public $id; |
26
|
|
|
|
27
|
|
|
/** @var string */ |
28
|
|
|
public $name; |
29
|
|
|
|
30
|
|
|
/** @var string */ |
31
|
|
|
public $status; |
32
|
|
|
|
33
|
|
|
/** @var string */ |
34
|
|
|
public $description; |
35
|
|
|
|
36
|
|
|
/** @var \DateTimeImmutable */ |
37
|
|
|
public $createdAt; |
38
|
|
|
|
39
|
|
|
/** @var array */ |
40
|
|
|
public $metadata = []; |
41
|
|
|
|
42
|
|
|
/** @var string */ |
43
|
|
|
public $volumeId; |
44
|
|
|
|
45
|
|
|
/** @var int */ |
46
|
|
|
public $size; |
47
|
|
|
|
48
|
|
|
protected $resourceKey = 'snapshot'; |
49
|
|
|
protected $resourcesKey = 'snapshots'; |
50
|
|
|
protected $markerKey = 'id'; |
51
|
|
|
|
52
|
|
|
protected $aliases = [ |
53
|
|
|
'volume_id' => 'volumeId', |
54
|
|
|
]; |
55
|
|
|
|
56
|
2 |
|
/** |
57
|
|
|
* @inheritdoc |
58
|
2 |
|
*/ |
59
|
2 |
|
protected function getAliases() |
60
|
2 |
|
{ |
61
|
|
|
$aliases = parent::getAliases(); |
62
|
|
|
$aliases['created_at'] = new Alias('createdAt', \DateTimeImmutable::class); |
63
|
1 |
|
return $aliases; |
|
|
|
|
64
|
|
|
} |
65
|
1 |
|
|
66
|
1 |
|
public function populateFromResponse(ResponseInterface $response): self |
67
|
|
|
{ |
68
|
|
|
parent::populateFromResponse($response); |
69
|
|
|
$this->metadata = $this->parseMetadata($response); |
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function retrieve() |
74
|
1 |
|
{ |
75
|
|
|
$response = $this->executeWithState($this->api->getSnapshot()); |
76
|
1 |
|
$this->populateFromResponse($response); |
77
|
1 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
1 |
|
* @param array $userOptions {@see \OpenStack\BlockStorage\v2\Api::postSnapshots} |
81
|
|
|
* |
82
|
1 |
|
* @return Creatable |
83
|
1 |
|
*/ |
84
|
|
|
public function create(array $userOptions): Creatable |
85
|
1 |
|
{ |
86
|
|
|
$response = $this->execute($this->api->postSnapshots(), $userOptions); |
87
|
1 |
|
return $this->populateFromResponse($response); |
88
|
1 |
|
} |
89
|
|
|
|
90
|
2 |
|
public function update() |
91
|
|
|
{ |
92
|
2 |
|
$this->executeWithState($this->api->putSnapshot()); |
93
|
2 |
|
} |
94
|
2 |
|
|
95
|
|
|
public function delete() |
96
|
|
|
{ |
97
|
1 |
|
$this->executeWithState($this->api->deleteSnapshot()); |
98
|
|
|
} |
99
|
1 |
|
|
100
|
1 |
|
public function getMetadata(): array |
101
|
1 |
|
{ |
102
|
1 |
|
$response = $this->executeWithState($this->api->getSnapshotMetadata()); |
103
|
|
|
$this->metadata = $this->parseMetadata($response); |
104
|
1 |
|
return $this->metadata; |
105
|
|
|
} |
106
|
1 |
|
|
107
|
1 |
|
public function mergeMetadata(array $metadata) |
108
|
1 |
|
{ |
109
|
|
|
$this->getMetadata(); |
110
|
4 |
|
$this->metadata = array_merge($this->metadata, $metadata); |
111
|
|
|
$this->executeWithState($this->api->putSnapshotMetadata()); |
112
|
4 |
|
} |
113
|
4 |
|
|
114
|
|
|
public function resetMetadata(array $metadata) |
115
|
|
|
{ |
116
|
|
|
$this->metadata = $metadata; |
117
|
|
|
$this->executeWithState($this->api->putSnapshotMetadata()); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function parseMetadata(ResponseInterface $response): array |
121
|
|
|
{ |
122
|
|
|
$json = Utils::jsonDecode($response); |
123
|
|
|
return isset($json['metadata']) ? $json['metadata'] : []; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.