Uploads::delete()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 7
ccs 3
cts 4
cp 0.75
crap 2.0625
rs 10
1
<?php
2
3
namespace Bakerkretzmar\LaravelMapbox;
4
5
use RunTimeException;
6
7
use Zttp\Zttp;
8
9
class Uploads extends MapboxRequest
10
{
11
    /**
12
     * Create a new Upload request instance.
13
     *
14
     * @param  string|null  $upload_id
15
     */
16 12
    public function __construct(string $upload_id = null)
17
    {
18 12
        $this->upload_id = $upload_id;
19 12
    }
20
21
    /**
22
     * Retrieve temporary S3 credentials.
23
     *
24
     * @see     https://docs.mapbox.com/api/maps/#retrieve-s3-credentials
25
     * @return  array
26
     */
27 2
    public function credentials()
28
    {
29 2
        return Zttp::get($this->url(Mapbox::UPLOADS_ENDPOINT, null, ['credentials']))->json();
30
    }
31
32
    /**
33
     * List Uploads.
34
     *
35
     * @see     https://docs.mapbox.com/api/maps/#list-datasets
36
     * @return  array
37
     */
38 4
    public function list()
39
    {
40 4
        return Zttp::get($this->url(Mapbox::UPLOADS_ENDPOINT))->json();
41
    }
42
43
    /**
44
     * Create an Upload.
45
     *
46
     * @see     https://docs.mapbox.com/api/maps/#create-an-upload
47
     * @param   array  $data
48
     * @return  array
49
     */
50 8
    public function create(array $data = [])
51
    {
52 8
        if (! isset($data['tileset'])) {
53
            throw new RunTimeException('Tileset required');
54
        }
55
56 8
        if (! isset($data['url']) && ! isset($data['dataset'])) {
57
            throw new RunTimeException('Dataset or URL required');
58
        }
59
60 8
        if (isset($data['url']) && isset($data['dataset'])) {
61
            throw new RunTimeException('Dataset OR URL required--not both');
62
        }
63
64 8
        $data['tileset'] = config('laravel-mapbox.username') . '.' . $data['tileset'];
65
66 8
        if (isset($data['dataset'])) {
67 2
            $data['url'] = implode('/', ['mapbox://datasets', config('laravel-mapbox.username'), $data['dataset']]);
68 2
            unset($data['dataset']);
69
        }
70
71 8
        return Zttp::post($this->url(Mapbox::UPLOADS_ENDPOINT), $data)->json();
72
    }
73
74
    /**
75
     * Retrieve an Upload.
76
     *
77
     * @see     https://docs.mapbox.com/api/maps/#retrieve-upload-status
78
     * @return  array
79
     */
80 8
    public function get()
81
    {
82 8
        if (! $this->upload_id) {
83
            throw new RunTimeException('Upload ID required');
84
        }
85
86 8
        return Zttp::get($this->url(Mapbox::UPLOADS_ENDPOINT, $this->upload_id))->json();
87
    }
88
89
    /**
90
     * Delete an Upload.
91
     *
92
     * @see     https://docs.mapbox.com/api/maps/#remove-an-upload-status
93
     * @return  bool
94
     */
95 8
    public function delete()
96
    {
97 8
        if (! $this->upload_id) {
98
            throw new RunTimeException('Upload ID required');
99
        }
100
101 8
        return Zttp::delete($this->url(Mapbox::UPLOADS_ENDPOINT, $this->upload_id))->status() === Mapbox::DELETE_SUCCESS_STATUS;
102
    }
103
}
104