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

RuleManager   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 115
Duplicated Lines 18.26 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
D query() 0 23 9
A get() 11 11 1
A add() 10 10 1
A delete() 0 8 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\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