Image::delete()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the DigitalOceanV2 library.
5
 *
6
 * (c) Antoine Corcy <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace DigitalOceanV2\Api;
13
14
use DigitalOceanV2\Entity\Action as ActionEntity;
15
use DigitalOceanV2\Entity\Image as ImageEntity;
16
use DigitalOceanV2\Exception\HttpException;
17
18
/**
19
 * @author Yassir Hannoun <[email protected]>
20
 * @author Graham Campbell <[email protected]>
21
 */
22
class Image extends AbstractApi
23
{
24
    /**
25
     * @param array $criteria
26
     *
27
     * @return ImageEntity[]
28
     */
29
    public function getAll(array $criteria = [])
30
    {
31
        $query = sprintf('%s/images?per_page=%d', $this->endpoint, 200);
32
33
        if (isset($criteria['type']) && in_array($criteria['type'], ['distribution', 'application'])) {
34
            $query = sprintf('%s&type=%s', $query, $criteria['type']);
35
        }
36
37
        if (isset($criteria['private']) && true === (bool) $criteria['private']) {
38
            $query = sprintf('%s&private=true', $query);
39
        }
40
41
        $images = $this->adapter->get($query);
42
43
        $images = json_decode($images);
44
45
        $this->extractMeta($images);
46
47
        return array_map(function ($image) {
48
            return new ImageEntity($image);
49
        }, $images->images);
50
    }
51
52
    /**
53
     * @param int $id
54
     *
55
     * @return ImageEntity
56
     */
57
    public function getById($id)
58
    {
59
        $image = $this->adapter->get(sprintf('%s/images/%d', $this->endpoint, $id));
60
61
        $image = json_decode($image);
62
63
        return new ImageEntity($image->image);
64
    }
65
66
    /**
67
     * @param string $slug
68
     *
69
     * @return ImageEntity
70
     */
71
    public function getBySlug($slug)
72
    {
73
        $image = $this->adapter->get(sprintf('%s/images/%s', $this->endpoint, $slug));
74
75
        $image = json_decode($image);
76
77
        return new ImageEntity($image->image);
78
    }
79
80
    /**
81
     * @param int    $id
82
     * @param string $name
83
     *
84
     * @throws HttpException
85
     *
86
     * @return ImageEntity
87
     */
88
    public function update($id, $name)
89
    {
90
        $image = $this->adapter->put(sprintf('%s/images/%d', $this->endpoint, $id), ['name' => $name]);
91
92
        $image = json_decode($image);
93
94
        return new ImageEntity($image->image);
95
    }
96
97
    /**
98
     * @param int $id
99
     *
100
     * @throws HttpException
101
     */
102
    public function delete($id)
103
    {
104
        $this->adapter->delete(sprintf('%s/images/%d', $this->endpoint, $id));
105
    }
106
107
    /**
108
     * @param int    $id
109
     * @param string $regionSlug
110
     *
111
     * @throws HttpException
112
     *
113
     * @return ActionEntity
114
     */
115
    public function transfer($id, $regionSlug)
116
    {
117
        $action = $this->adapter->post(sprintf('%s/images/%d/actions', $this->endpoint, $id), ['type' => 'transfer', 'region' => $regionSlug]);
118
119
        $action = json_decode($action);
120
121
        return new ActionEntity($action->action);
122
    }
123
124
    /**
125
     * @param int $id
126
     *
127
     * @throws HttpException
128
     *
129
     * @return ActionEntity
130
     */
131
    public function convert($id)
132
    {
133
        $action = $this->adapter->post(sprintf('%s/images/%d/actions', $this->endpoint, $id), ['type' => 'convert']);
134
135
        $action = json_decode($action);
136
137
        return new ActionEntity($action->action);
138
    }
139
140
    /**
141
     * @param int $id
142
     * @param int $actionId
143
     *
144
     * @return ActionEntity
145
     */
146
    public function getAction($id, $actionId)
147
    {
148
        $action = $this->adapter->get(sprintf('%s/images/%d/actions/%d', $this->endpoint, $id, $actionId));
149
150
        $action = json_decode($action);
151
152
        return new ActionEntity($action->action);
153
    }
154
}
155