Completed
Pull Request — master (#6)
by Nick
15:48
created

RuleManager::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 11
Ratio 100 %

Importance

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