Completed
Push — master ( 7b6a9e...621ea2 )
by Nick
15:23
created

SlotManager::query()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 8.7972
c 0
b 0
f 0
cc 4
eloc 13
nc 8
nop 1
1
<?php
2
3
namespace Acquia\LiftClient\Manager;
4
5
use Acquia\LiftClient\Entity\Slot;
6
use GuzzleHttp\Psr7\Request;
7
8
class SlotManager extends ManagerBase
9
{
10
    /**
11
     * Get a list of slots.
12
     *
13
     * Example of how to structure the $options parameter:
14
     * <code>
15
     * $options = [
16
     *     'visible_on_page'  => 'http://localhost/blog/*',
17
     *     'status' => 'enabled',
18
     * ];
19
     * </code>
20
     *
21
     * @see http://docs.decision-api.acquia.com/#slots_get
22
     *
23
     * @param array $options
24
     *
25
     * @throws \GuzzleHttp\Exception\RequestException
26
     *
27
     * @return \Acquia\LiftClient\Entity\Slot[]
28
     */
29
    public function query($options = [])
30
    {
31
        $variables = $options + [
32
            'limit' => 1000,
33
            'start' => 0,
34
          ];
35
36
        $url = '/slots';
37
        $url .= isset($variables['visible_on_page']) ? "&visible_on_page={$variables['visible_on_page']}" : '';
38
        $url .= isset($variables['status']) ? "&status={$variables['status']}" : '';
39
40
        // Now make the request.
41
        $request = new Request('GET', $url);
42
        $data = $this->client->getResponseJson($request);
43
44
        // Get them as Slot objects
45
        $slots = [];
46
        foreach ($data as $dataItem) {
47
            $slots[] = new Slot($dataItem);
48
        }
49
50
        return $slots;
51
    }
52
53
    /**
54
     * Get a specific slot.
55
     *
56
     * Example of how to structure the $options parameter:
57
     *
58
     * @see http://docs.decision-api.acquia.com/#slots__slotId__get
59
     *
60
     * @param array $id
61
     *
62
     * @throws \GuzzleHttp\Exception\RequestException
63
     *
64
     * @return \Acquia\LiftClient\Entity\Slot
65
     */
66 View Code Duplication
    public function get($id) {
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
67
        $url = "/slots/{$id}";
68
69
        // Now make the request.
70
        $request = new Request('GET', $url);
71
        $data = $this->client->getResponseJson($request);
72
73
        return new Slot($data);
74
    }
75
76
    /**
77
     * Add a slot.
78
     *
79
     * @param \Acquia\LiftClient\Entity\Slot $slot
80
     *
81
     * @throws \GuzzleHttp\Exception\RequestException
82
     *
83
     * @return \Acquia\LiftClient\Entity\Slot
84
     */
85 View Code Duplication
    public function add(Slot $slot) {
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
86
        $body = $slot->json();
87
        $url = '/slots';
88
        $request = new Request('POST', $url, [], $body);
89
        $data = $this->client->getResponseJson($request);
90
91
        return new Slot($data);
92
    }
93
94
    /**
95
     * Deletes a slot by ID.
96
     *
97
     * @param string $id
98
     *
99
     * @throws \GuzzleHttp\Exception\RequestException
100
     *
101
     * @return bool
102
     */
103
    public function delete($id) {
104
        $url = "/slots/{$id}";
105
        $this->client->delete($url);
106
107
        return true;
108
    }
109
}
110