Completed
Pull Request — master (#21)
by Yuan
01:59
created

GoalManager::getQueryString()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 0
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 2
nop 1
crap 12
1
<?php
2
3
namespace Acquia\LiftClient\Manager;
4
5
use Acquia\LiftClient\Entity\Entity;
6
use Acquia\LiftClient\Entity\Goal;
7
use Acquia\LiftClient\Entity\GoalAddResponse;
8
use Acquia\LiftClient\Exception\LiftSdkException;
9
use GuzzleHttp\Psr7\Request;
10
11
class GoalManager extends ManagerBase
12
{
13
    /**
14
     * {@inheritdoc}
15
     */
16
    protected $queryParameters = [
17
        'global' => null,
18
        'limit_by_site' => null,
19
    ];
20
21
    /**
22
     * Get a list of Goals.
23
     *
24
     * Example of how to structure the $options parameter:
25
     * <code>
26
     * $options = [
27
     *     'limit_by_site'  => 'my-site-id'
28
     * ];
29
     * </code>
30
     *
31
     * @see http://docs.decision-api.acquia.com/#goals_get
32
     *
33
     * @param array $options
34
     *
35
     * @throws \GuzzleHttp\Exception\RequestException
36
     *
37 10
     * @return \Acquia\LiftClient\Entity\Goal[]
38
     */
39 6 View Code Duplication
    public function query($options = [])
40 6
    {
41
        $url = '/goals';
42
        $url .= $this->getQueryString($options);
43 6
44 6
        // Now make the request.
45
        $request = new Request('GET', $url);
46
        $data = $this->getResponseJson($request);
47 3
48 3
        // Get them as Goal objects
49 3
        $goals = [];
50 2
        foreach ($data as $dataItem) {
51
            $goals[] = new Goal($dataItem);
52 3
        }
53 8
54
        return $goals;
55
    }
56
57
    /**
58
     * Get a specific goal.
59
     *
60
     * @see http://docs.decision-api.acquia.com/#goals__goal_id__get
61
     *
62
     * @param array $id
63
     *
64
     * @throws \GuzzleHttp\Exception\RequestException
65
     *
66 6
     * @return \Acquia\LiftClient\Entity\Goal
67
     */
68 6
    public function get($id)
69
    {
70
        $url = "/goals/{$id}";
71 6
72 6
        // Now make the request.
73
        $request = new Request('GET', $url);
74 3
        $data = $this->getResponseJson($request);
75
76
        return new Goal($data);
77
    }
78
79
    /**
80
     * Add a goal.
81
     *
82
     * @see http://docs.decision-api.acquia.com/#goals_post
83
     *
84
     * @param \Acquia\LiftClient\Entity\Goal $goal
85
     *
86
     * @throws \GuzzleHttp\Exception\RequestException
87
     *
88 9
     * @return \Acquia\LiftClient\Entity\GoalAddResponse
89
     */
90
    public function add(Goal $goal)
91
    {
92 9
        // goals only supports adding a list of goals
93 9
        // we do not want to support that in the SDK for consistency reasons, so we convert it to an array here.
94 9
        $goals = new Entity([$goal->getArrayCopy()]);
95 9
        $body = $goals->json();
96 9
        $url = '/goals';
97
        $request = new Request('POST', $url, [], $body);
98 6
        $data = $this->getResponseJson($request);
99
100
        return new GoalAddResponse($data);
101
    }
102
103
    /**
104
     * Deletes a goal by ID.
105
     *
106
     * @see http://docs.decision-api.acquia.com/#goals__goal_id__delete
107
     *
108
     * @param string $id
109
     *
110
     * @throws \GuzzleHttp\Exception\RequestException
111
     *
112 6
     * @return bool
113
     */
114 6
    public function delete($id)
115 6
    {
116
        $url = "/goals/{$id}";
117 3
        $this->client->delete($url);
118
119
        return true;
120
    }
121
122
    /**
123
     * Get query string of using the options.
124
     *
125
     * @param $options The options
126
     *
127
     * @throws \Acquia\LiftClient\Exception\LiftSdkException
128
     *
129
     * @return string  The query string
130
     */
131
    protected function getQueryString($options) {
132
        if (isset($options['global']) && !in_array($options['global'], ['true', 'false'])) {
133
            throw new LiftSdkException('Global parameter must be a string value of "true" or "false".');
134
        }
135
136
        return parent::getQueryString($options);
137
    }
138
}
139