Completed
Push — master ( 0ca99d...7c70a3 )
by Oleg
02:52
created

Pages   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 118
Duplicated Lines 31.36 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 88.1%

Importance

Changes 0
Metric Value
dl 37
loc 118
ccs 37
cts 42
cp 0.881
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 1

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B listAll() 25 25 4
A get() 12 12 2
A create() 0 13 2
A update() 0 13 2
A delete() 0 5 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 07 October 2016
5
 * @copyright (c) 2016, Web Marketing ROI
6
 */
7
namespace WebMarketingROI\OptimizelyPHP\Service\v2;
8
9
use WebMarketingROI\OptimizelyPHP\Resource\v2\Page;
10
11
/**
12
 * Provides methods for working with Optimizely project pages.
13
 */
14
class Pages
15
{
16
    /**
17
     * Optimizely API Client.
18
     * @var WebMarketingROI\OptimizelyPHP\OptimizelyApiClient
19
     */
20
    private $client;
21
    
22
    /**
23
     * Constructor.
24
     */
25 5
    public function __construct($client)
26
    {
27 5
        $this->client = $client;
28 5
    }
29
    
30
    /**
31
     * List all Pages for a project.
32
     * @param integer $projectId
33
     * @param integer $page
34
     * @param integer $perPage
35
     * @return array[Page]
0 ignored issues
show
Documentation introduced by
The doc-type array[Page] could not be parsed: Expected "]" at position 2, but found "Page". (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
36
     * @throws \Exception
37
     */
38 1 View Code Duplication
    public function listAll($projectId, $page=0, $perPage=10)
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...
39
    {
40 1
        if ($page<0) {
41
            throw new \Exception('Invalid page number passed');
42
        }
43
        
44 1
        if ($perPage<0) {
45
            throw new \Exception('Invalid page size passed');
46
        }
47
        
48 1
        $response = $this->client->sendApiRequest('/pages', 
49
                array(
50 1
                    'project_id'=>$projectId,
51 1
                    'page'=>$page,
52
                    'per_page'=>$perPage
53 1
                ));
54
        
55 1
        $pages = array();
56 1
        foreach ($response as $pageInfo) {
57 1
            $page = new Page($pageInfo);
58 1
            $pages[] = $page;
59 1
        }
60
        
61 1
        return $pages;
62
    }
63
    
64
    /**
65
     * Get metadata for a single Page
66
     * @param integer $pageId
67
     * @return Page
68
     * @throws \Exception
69
     */
70 1 View Code Duplication
    public function get($pageId)
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...
71
    {
72 1
        if (!is_int($pageId)) {
73
            throw new \Exception("Integer page ID expected, while got '$pageId'");
74
        }
75
        
76 1
        $response = $this->client->sendApiRequest("/pages/$pageId");
77
        
78 1
        $page = new Page($response);
79
        
80 1
        return $page;
81
    }
82
    
83
    /**
84
     * Create a new Page in a provided Project
85
     * @param Page $page
86
     */
87 1
    public function create($page)
88
    {
89 1
        if (!($page instanceOf Page)) {
90
            throw new \Exception("Expected argument of type Page");
91
        }
92
        
93 1
        $postData = $page->toArray();
94
        
95 1
        $response = $this->client->sendApiRequest("/pages", array(), 'POST', 
96 1
                $postData, array(201));
97
        
98 1
        return new Page($response);
99
    }
100
    
101
    /**
102
     * Update a Page in a provided Project
103
     * @param integer $pageId
104
     * @param Audience $page
105
     * @throws \Exception
106
     */
107 1
    public function update($pageId, $page) 
108
    {
109 1
        if (!($page instanceOf Page)) {
110
            throw new \Exception("Expected argument of type Page");
111
        }
112
        
113 1
        $postData = $page->toArray();
114
                
115 1
        $response = $this->client->sendApiRequest("/pages/$pageId", array(), 'PATCH', 
116 1
                $postData, array(200));
117
        
118 1
        return new Page($response);
119
    }
120
    
121
    /**
122
     * Delete a Page within a Project by ID.
123
     * @param integer $pageId
124
     * @throws \Exception
125
     */
126 1
    public function delete($pageId) 
127
    {
128 1
        $response = $this->client->sendApiRequest("/pages/$pageId", array(), 'DELETE', 
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
129 1
                array(), array(200));
130 1
    }
131
}
132
133
134
135
136
137
138