Completed
Push — master ( 8b4718...61d59f )
by Oleg
02:27
created

Audiences::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
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\Exception;
10
use WebMarketingROI\OptimizelyPHP\Resource\v2\Audience;
11
12
/**
13
 * Provides methods for working with Optimizely audiences.
14
 */
15
class Audiences
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 Audiences for 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('/audiences', 
50
                array(
51 1
                    'project_id'=>$projectId,
52 1
                    'page'=>$page,
53
                    'per_page'=>$perPage
54 1
                ));
55
        
56 1
        $audiences = array();
57 1
        foreach ($result->getDecodedJsonData() as $audienceInfo) {
58 1
            $audience = new Audience($audienceInfo);
59 1
            $audiences[] = $audience;
60 1
        }
61 1
        $result->setPayload($audiences);
62
        
63 1
        return $result;
64
    }
65
    
66
    /**
67
     * Get metadata for a single Audience.
68
     * @param type $audienceId
69
     * @return Result
70
     * @throws Exception
71
     */
72 1 View Code Duplication
    public function get($audienceId)
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...
73
    {
74 1
        if (!is_int($audienceId)) {
75
            throw new Exception("Integer audience ID expected, while got '$audienceId'",
76
                    Exception::CODE_INVALID_ARG);
77
        }
78
        
79 1
        $result = $this->client->sendApiRequest("/audiences/$audienceId");
80
        
81 1
        $audience = new Audience($result->getDecodedJsonData());
82 1
        $result->setPayload($audience);
83
        
84 1
        return $result;
85
    }
86
    
87
    /**
88
     * Create an Audience for a Project.
89
     * @param Result
90
     */
91 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...
92
    {
93 1
        if (!($audience instanceOf \WebMarketingROI\OptimizelyPHP\Resource\v2\Audience)) {
94
            throw new Exception("Expected argument of type Audience",
95
                    Exception::CODE_INVALID_ARG);
96
        }
97
        
98 1
        $postData = $audience->toArray();
99
        
100 1
        $result = $this->client->sendApiRequest("/audiences", array(), 'POST', 
101 1
                $postData, array(201));
102
        
103 1
        $audience = new Audience($result->getDecodedJsonData());
104 1
        $result->setPayload($audience);
105
        
106 1
        return $result;
107
    }
108
    
109
    /**
110
     * Update an Audience for a Project
111
     * @param integer $audienceId
112
     * @param Result
113
     * @throws Exception
114
     */
115 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...
116
    {
117 1
        if (!($audience instanceOf \WebMarketingROI\OptimizelyPHP\Resource\v2\Audience)) {
118
            throw new Exception("Expected argument of type Audience",
119
                    Exception::CODE_INVALID_ARG);
120
        }
121
        
122 1
        $postData = $audience->toArray();
123
                
124 1
        $result = $this->client->sendApiRequest("/audiences/$audienceId", array(), 'PATCH', 
125 1
                $postData, array(200));
126
        
127 1
        $audience = new Audience($result->getDecodedJsonData());
128 1
        $result->setPayload($audience);
129
        
130 1
        return $result;
131
    }
132
}
133
134
135
136
137
138