Completed
Push — master ( 41d0c8...5d03eb )
by Graham
688:52 queued 607:19
created

Image   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 10
Bugs 6 Features 2
Metric Value
c 10
b 6
f 2
dl 0
loc 133
wmc 12
lcom 1
cbo 4
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
B getAll() 0 22 5
A getById() 0 8 1
A getBySlug() 0 8 1
A update() 0 8 1
A delete() 0 4 1
A transfer() 0 8 1
A convert() 0 8 1
A getAction() 0 8 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
17
/**
18
 * @author Yassir Hannoun <[email protected]>
19
 * @author Graham Campbell <[email protected]>
20
 */
21
class Image extends AbstractApi
22
{
23
    /**
24
     * @param array $criteria
25
     *
26
     * @return ImageEntity[]
27
     */
28
    public function getAll(array $criteria = [])
29
    {
30
        $query = sprintf('%s/images?per_page=%d', $this->endpoint, 200);
31
32
        if (isset($criteria['type']) && in_array($criteria['type'], ['distribution', 'application'])) {
33
            $query = sprintf('%s&type=%s', $query, $criteria['type']);
34
        }
35
36
        if (isset($criteria['private']) && true === (bool) $criteria['private']) {
37
            $query = sprintf('%s&private=true', $query);
38
        }
39
40
        $images = $this->adapter->get($query);
41
42
        $images = json_decode($images);
43
44
        $this->extractMeta($images);
45
46
        return array_map(function ($image) {
47
            return new ImageEntity($image);
48
        }, $images->images);
49
    }
50
51
    /**
52
     * @param int $id
53
     *
54
     * @return ImageEntity
55
     */
56
    public function getById($id)
57
    {
58
        $image = $this->adapter->get(sprintf('%s/images/%d', $this->endpoint, $id));
59
60
        $image = json_decode($image);
61
62
        return new ImageEntity($image->image);
63
    }
64
65
    /**
66
     * @param string $slug
67
     *
68
     * @return ImageEntity
69
     */
70
    public function getBySlug($slug)
71
    {
72
        $image = $this->adapter->get(sprintf('%s/images/%s', $this->endpoint, $slug));
73
74
        $image = json_decode($image);
75
76
        return new ImageEntity($image->image);
77
    }
78
79
    /**
80
     * @param int    $id
81
     * @param string $name
82
     *
83
     * @throws \RuntimeException
84
     *
85
     * @return ImageEntity
86
     */
87
    public function update($id, $name)
88
    {
89
        $image = $this->adapter->put(sprintf('%s/images/%d', $this->endpoint, $id), ['name' => $name]);
90
91
        $image = json_decode($image);
92
93
        return new ImageEntity($image->image);
94
    }
95
96
    /**
97
     * @param int $id
98
     *
99
     * @throws \RuntimeException
100
     */
101
    public function delete($id)
102
    {
103
        $this->adapter->delete(sprintf('%s/images/%d', $this->endpoint, $id));
104
    }
105
106
    /**
107
     * @param int    $id
108
     * @param string $regionSlug
109
     *
110
     * @throws \RuntimeException
111
     *
112
     * @return ActionEntity
113
     */
114
    public function transfer($id, $regionSlug)
115
    {
116
        $action = $this->adapter->post(sprintf('%s/images/%d/actions', $this->endpoint, $id), ['type' => 'transfer', 'region' => $regionSlug]);
117
118
        $action = json_decode($action);
119
120
        return new ActionEntity($action->action);
121
    }
122
123
    /**
124
     * @param int $id
125
     *
126
     * @throws \RuntimeException
127
     *
128
     * @return ActionEntity
129
     */
130
    public function convert($id)
131
    {
132
        $action = $this->adapter->post(sprintf('%s/images/%d/actions', $this->endpoint, $id), ['type' => 'convert']);
133
134
        $action = json_decode($action);
135
136
        return new ActionEntity($action->action);
137
    }
138
139
    /**
140
     * @param int $id
141
     * @param int $actionId
142
     *
143
     * @return ActionEntity
144
     */
145
    public function getAction($id, $actionId)
146
    {
147
        $action = $this->adapter->get(sprintf('%s/images/%d/actions/%d', $this->endpoint, $id, $actionId));
148
149
        $action = json_decode($action);
150
151
        return new ActionEntity($action->action);
152
    }
153
}
154