Completed
Push — master ( 90835e...e5901e )
by Oleg
02:50
created

Audiences   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 120
Duplicated Lines 28.33 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 79.59%

Importance

Changes 0
Metric Value
dl 34
loc 120
ccs 39
cts 49
cp 0.7959
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B listAll() 0 28 4
A get() 0 14 2
A create() 17 17 2
A update() 17 17 2

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\Audience;
10
11
/**
12
 * Provides methods for working with Optimizely audiences.
13
 */
14
class Audiences
15
{
16
    /**
17
     * Optimizely API Client.
18
     * @var WebMarketingROI\OptimizelyPHP\OptimizelyApiClient
19
     */
20
    private $client;
21
    
22
    /**
23
     * Constructor.
24
     */
25 4
    public function __construct($client)
26
    {
27 4
        $this->client = $client;
28 4
    }
29
    
30
    /**
31
     * List Audiences for a Project
32
     * @param integer $projectId
33
     * @param integer $page
34
     * @param integer $perPage
35
     * @return Result
36
     * @throws Exception
37
     */
38 1
    public function listAll($projectId, $page=1, $perPage=25)
39
    {
40 1
        if ($page<0) {
41
            throw new Exception('Invalid page number passed',
42
                    Exception::CODE_INVALID_ARG);
43
        }
44
        
45 1
        if ($perPage<0) {
46
            throw new Exception('Invalid page size passed',
47
                    Exception::CODE_INVALID_ARG);
48
        }
49
        
50 1
        $result = $this->client->sendApiRequest('/audiences', 
51
                array(
52 1
                    'project_id'=>$projectId,
53 1
                    'page'=>$page,
54
                    'per_page'=>$perPage
55 1
                ));
56
        
57 1
        $audiences = array();
58 1
        foreach ($result->getDecodedJsonData() as $audienceInfo) {
59 1
            $audience = new Audience($audienceInfo);
60 1
            $audiences[] = $audience;
61 1
        }
62 1
        $result->setPayload($audiences);
63
        
64 1
        return $result;
65
    }
66
    
67
    /**
68
     * Get metadata for a single Audience.
69
     * @param type $audienceId
70
     * @return Result
71
     * @throws Exception
72
     */
73 1
    public function get($audienceId)
74
    {
75 1
        if (!is_int($audienceId)) {
76
            throw new Exception("Integer audience ID expected, while got '$audienceId'",
77
                    Exception::CODE_INVALID_ARG);
78
        }
79
        
80 1
        $result = $this->client->sendApiRequest("/audiences/$audienceId");
81
        
82 1
        $audience = new Audience($result->getDecodedJsonData());
83 1
        $result->setPayload($audience);
84
        
85 1
        return $result;
86
    }
87
    
88
    /**
89
     * Create an Audience for a Project.
90
     * @param Result
91
     */
92 1 View Code Duplication
    public function create($audience)
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...
93
    {
94 1
        if (!($audience instanceOf \WebMarketingROI\OptimizelyPHP\Resource\v2\Audience)) {
95
            throw new Exception("Expected argument of type Audience",
96
                    Exception::CODE_INVALID_ARG);
97
        }
98
        
99 1
        $postData = $audience->toArray();
100
        
101 1
        $result = $this->client->sendApiRequest("/audiences", array(), 'POST', 
102 1
                $postData, array(201));
103
        
104 1
        $audience = new Audience($result->getDecodedJsonData());
105 1
        $result->setPayload($audience);
106
        
107 1
        return $result;
108
    }
109
    
110
    /**
111
     * Update an Audience for a Project
112
     * @param integer $audienceId
113
     * @param Result
114
     * @throws Exception
115
     */
116 1 View Code Duplication
    public function update($audienceId, $audience) 
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...
117
    {
118 1
        if (!($audience instanceOf \WebMarketingROI\OptimizelyPHP\Resource\v2\Audience)) {
119
            throw new Exception("Expected argument of type Audience",
120
                    Exception::CODE_INVALID_ARG);
121
        }
122
        
123 1
        $postData = $audience->toArray();
124
                
125 1
        $result = $this->client->sendApiRequest("/audiences/$audienceId", array(), 'PATCH', 
126 1
                $postData, array(200));
127
        
128 1
        $audience = new Audience($result->getDecodedJsonData());
129 1
        $result->setPayload($audience);
130
        
131 1
        return $result;
132
    }
133
}
134
135
136
137
138
139