Attributes   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 133
Duplicated Lines 19.55 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 80%

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 26
loc 133
ccs 40
cts 50
cp 0.8
rs 10
wmc 13
lcom 1
cbo 2

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A listAll() 26 26 5
A get() 0 14 2
A create() 0 17 2
A update() 0 17 2
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
 * @author Oleg Krivtsov <[email protected]>
4
 * @date 08 May 2017
5
 * @copyright (c) 2017, Web Marketing ROI
6
 */
7
namespace WebMarketingROI\OptimizelyPHP\Service\v2;
8
9
use WebMarketingROI\OptimizelyPHP\Exception;
10
use WebMarketingROI\OptimizelyPHP\Resource\v2\Attribute;
11
12
/**
13
 * Provides methods for working with Optimizely attributes.
14
 */
15
class Attributes
16
{
17
    /**
18
     * Optimizely API Client.
19
     * @var WebMarketingROI\OptimizelyPHP\OptimizelyApiClient
20
     */
21
    private $client;
22
    
23
    /**
24
     * Constructor.
25
     */
26 6
    public function __construct($client)
27
    {
28 6
        $this->client = $client;
29 6
    }
30
    
31
    /**
32
     * List Attributes under a Project
33
     * @param integer $projectId
34
     * @param integer $page
35
     * @param integer $perPage
36
     * @return Result
37
     * @throws Exception
38
     */
39 3 View Code Duplication
    public function listAll($projectId, $page=1, $perPage=25)
0 ignored issues
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...
40
    {
41 3
        if ($page<0) {
42 1
            throw new Exception('Invalid page number passed');
43
        }
44
        
45 2
        if ($perPage<0 || $perPage>100) {
46 1
            throw new Exception('Invalid page size passed');
47
        }
48
        
49 1
        $result = $this->client->sendApiRequest('/attributes', 
50
                array(
51 1
                    'project_id'=>$projectId,
52 1
                    'page'=>$page,
53 1
                    'per_page'=>$perPage
54
                ));
55
        
56 1
        $attributes = array();
57 1
        foreach ($result->getDecodedJsonData() as $attributeInfo) {
58 1
            $attribute = new Attribute($attributeInfo);
59 1
            $attributes[] = $attribute;
60
        }
61 1
        $result->setPayload($attributes);
62
        
63 1
        return $result;
64
    }
65
    
66
    /**
67
     * Get Attribute by ID
68
     * @param type $attributeId
69
     * @return Result
70
     * @throws Exception
71
     */
72 1
    public function get($attributeId)
73
    {
74 1
        if (!is_int($attributeId)) {
75
            throw new Exception("Integer attribute ID expected, while got '$attributeId'",
76
                    Exception::CODE_INVALID_ARG);
77
        }
78
        
79 1
        $result = $this->client->sendApiRequest("/attributes/$attributeId");
80
        
81 1
        $attribute = new Attribute($result->getDecodedJsonData());
82 1
        $result->setPayload($attribute);
83
        
84 1
        return $result;
85
    }
86
    
87
    /**
88
     * Create an Attribue
89
     * @param Result
90
     */
91 1
    public function create($attribute)
92
    {
93 1
        if (!($attribute instanceOf \WebMarketingROI\OptimizelyPHP\Resource\v2\Attribute)) {
94
            throw new Exception("Expected argument of type Attribute",
95
                    Exception::CODE_INVALID_ARG);
96
        }
97
        
98 1
        $postData = $attribute->toArray();
99
        
100 1
        $result = $this->client->sendApiRequest("/attributes", array(), 'POST', 
101 1
                $postData, array(201));
102
        
103 1
        $attribute = new Attribute($result->getDecodedJsonData());
104 1
        $result->setPayload($attribute);
105
        
106 1
        return $result;
107
    }
108
    
109
    /**
110
     * Update an Attribute by ID
111
     * @param integer $attributeId     * 
112
     * @param Attribute $attribute
113
     * @return Result
114
     * @throws Exception
115
     */
116 1
    public function update($attributeId, $attribute) 
117
    {
118 1
        if (!($attribute instanceOf \WebMarketingROI\OptimizelyPHP\Resource\v2\Attribute)) {
119
            throw new Exception("Expected argument of type Attribute",
120
                    Exception::CODE_INVALID_ARG);
121
        }
122
        
123 1
        $postData = $attribute->toArray();
124
                
125 1
        $result = $this->client->sendApiRequest("/attributes/$attributeId", array(), 'PATCH', 
126 1
                $postData, array(200));
127
        
128 1
        $attribute = new Attribute($result->getDecodedJsonData());
129 1
        $result->setPayload($attribute);
130
        
131 1
        return $result;
132
    }
133
    
134
    /**
135
     * Delete Attribute by ID
136
     * @param integer $attributeId
137
     * @return Result
138
     * @throws Exception
139
     */
140
    public function delete($attributeId) 
141
    {
142
        $result = $this->client->sendApiRequest("/attributes/$attributeId", array(), 'DELETE', 
143
                array());
144
        
145
        return $result;
146
    }
147
}
148
149
150
151
152
153
154