Completed
Push — master ( 621ea2...dd23a3 )
by Nick
12s
created

RuleManager::query()   D

Complexity

Conditions 9
Paths 256

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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