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

GoalManager::getQueryString()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 6
Ratio 60 %

Code Coverage

Tests 6
CRAP Score 5

Importance

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