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

RuleManager::add()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 9
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 9
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Acquia\LiftClient\Manager;
4
5
use Acquia\LiftClient\Entity\Rule;
6
use GuzzleHttp\Psr7\Request;
7
8
class RuleManager extends ManagerBase
9
{
10
    /**
11
     * {@inheritdoc}
12
     */
13
    protected $queryParameters = [
14
        'visible_on_page' => null,
15
        'prefetch' => null,
16
        'sort' => null,
17
        'start' => null,
18
        'rows' => null,
19
        'sort_field' => null,
20
        'status' => null,
21
    ];
22
23
    /**
24
     * Get a list of Rules.
25
     *
26
     * Example of how to structure the $options parameter:
27
     * <code>
28
     * $options = [
29
     *     'visible_on_page'  => 'node/1/*',
30
     *     'prefetch'  => true,
31
     *     'sort'  => 'asc',
32
     *     'start'  => 0,
33
     *     'rows'  => 10,
34
     *     'sort_field'  => 'updated',
35
     *     'status'  => 'published'
36
     * ];
37
     * </code>
38
     *
39
     * @see http://docs.decision-api.acquia.com/#rules_get
40
     *
41
     * @param array $options
42
     *
43
     * @throws \GuzzleHttp\Exception\RequestException
44
     *
45
     * @return \Acquia\LiftClient\Entity\Rule[]
46
     */
47 6 View Code Duplication
    public function query($options = [])
48
    {
49 6
        $url = '/rules';
50 6
        $url .= $this->getQueryString($options);
51
52
        // Now make the request.
53 6
        $request = new Request('GET', $url);
54 6
        $data = $this->getResponseJson($request);
55
56
        // Get them as Rule objects
57 3
        $rules = [];
58 3
        foreach ($data as $dataItem) {
59 3
            $rules[] = new Rule($dataItem);
60 2
        }
61
62 3
        return $rules;
63
    }
64
65
    /**
66
     * Get a specific rule.
67
     *
68
     * @see http://docs.decision-api.acquia.com/#rules__ruleId__get
69
     *
70
     * @param array $id
71
     *
72
     * @throws \GuzzleHttp\Exception\RequestException
73
     *
74
     * @return \Acquia\LiftClient\Entity\Rule
75
     */
76 6 View Code Duplication
    public function get($id)
77
    {
78 6
        $url = "/rules/{$id}";
79
80
        // Now make the request.
81 6
        $request = new Request('GET', $url);
82 6
        $data = $this->getResponseJson($request);
83
84 3
        return new Rule($data);
85
    }
86
87
    /**
88
     * Add or update a rule.
89
     *
90
     * To Update a rule, use a Rule object with an existing identifier.
91
     *
92
     * @see http://docs.decision-api.acquia.com/#rules_post
93
     *
94
     * @param \Acquia\LiftClient\Entity\Rule $rule
95
     *
96
     * @throws \GuzzleHttp\Exception\RequestException
97
     *
98
     * @return \Acquia\LiftClient\Entity\Rule
99
     */
100 6 View Code Duplication
    public function add(Rule $rule)
101
    {
102 6
        $body = $rule->json();
103 6
        $url = '/rules';
104 6
        $request = new Request('POST', $url, [], $body);
105 6
        $data = $this->getResponseJson($request);
106
107 3
        return new Rule($data);
108
    }
109
110
    /**
111
     * Deletes a rule by ID.
112
     *
113
     * @see http://docs.decision-api.acquia.com/#rules__ruleId__delete
114
     *
115
     * @param string $id
116
     *
117
     * @throws \GuzzleHttp\Exception\RequestException
118
     *
119
     * @return bool
120
     */
121 6
    public function delete($id)
122
    {
123 6
        $url = "/rules/{$id}";
124 6
        $this->client->delete($url);
125
126 3
        return true;
127
    }
128
}
129