Completed
Pull Request — master (#6)
by Nick
03:00
created

RuleManager   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 112
Duplicated Lines 18.75 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 4
dl 21
loc 112
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
D query() 0 23 9
A get() 10 10 1
A add() 9 9 1
A delete() 0 7 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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)
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...
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 a rule.
82
     *
83
     * @see http://docs.decision-api.acquia.com/#rules_post
84
     *
85
     * @param \Acquia\LiftClient\Entity\Rule $rule
86
     *
87
     * @throws \GuzzleHttp\Exception\RequestException
88
     *
89
     * @return \Acquia\LiftClient\Entity\Rule
90
     */
91 View Code Duplication
    public function add(Rule $rule)
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...
92
    {
93
        $body = $rule->json();
94
        $url = '/rules';
95
        $request = new Request('POST', $url, [], $body);
96
        $data = $this->client->getResponseJson($request);
97
98
        return new Rule($data);
99
    }
100
101
    /**
102
     * Deletes a rule by ID.
103
     *
104
     * @see http://docs.decision-api.acquia.com/#rules__ruleId__delete
105
     *
106
     * @param string $id
107
     *
108
     * @throws \GuzzleHttp\Exception\RequestException
109
     *
110
     * @return bool
111
     */
112
    public function delete($id)
113
    {
114
        $url = "/rules/{$id}";
115
        $this->client->delete($url);
116
117
        return true;
118
    }
119
}
120