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

Audiences::create()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 1
dl 0
loc 13
ccs 6
cts 7
cp 0.8571
crap 2.0116
rs 9.4285
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\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 array[Audience]
0 ignored issues
show
Documentation introduced by
The doc-type array[Audience] could not be parsed: Expected "]" at position 2, but found "Audience". (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('/audiences', 
49
                array(
50 1
                    'project_id'=>$projectId,
51 1
                    'page'=>$page,
52
                    'per_page'=>$perPage
53 1
                ));
54
        
55 1
        $audiences = array();
56 1
        foreach ($response as $audienceInfo) {
57 1
            $audience = new Audience($audienceInfo);
58 1
            $audiences[] = $audience;
59 1
        }
60
        
61 1
        return $audiences;
62
    }
63
    
64
    /**
65
     * Get metadata for a single Audience.
66
     * @param type $audienceId
67
     * @return Audience
68
     * @throws \Exception
69
     */
70 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...
71
    {
72 1
        if (!is_int($audienceId)) {
73
            throw new \Exception("Integer audience ID expected, while got '$audienceId'");
74
        }
75
        
76 1
        $response = $this->client->sendApiRequest("/audiences/$audienceId");
77
        
78 1
        $audience = new Audience($response);
79
        
80 1
        return $audience;
81
    }
82
    
83
    /**
84
     * Create an Audience for a Project.
85
     * @param Audience $audience
86
     */
87 1
    public function create($audience)
88
    {
89 1
        if (!($audience instanceOf \WebMarketingROI\OptimizelyPHP\Resource\v2\Audience)) {
90
            throw new \Exception("Expected argument of type Audience");
91
        }
92
        
93 1
        $postData = $audience->toArray();
94
        
95 1
        $response = $this->client->sendApiRequest("/audiences", array(), 'POST', 
96 1
                $postData, array(201));
97
        
98 1
        return new Audience($response);
99
    }
100
    
101
    /**
102
     * Update an Audience for a Project
103
     * @param integer $audienceId
104
     * @param Audience $audience
105
     * @throws \Exception
106
     */
107 1
    public function update($audienceId, $audience) 
108
    {
109 1
        if (!($audience instanceOf \WebMarketingROI\OptimizelyPHP\Resource\v2\Audience)) {
110
            throw new \Exception("Expected argument of type Audience");
111
        }
112
        
113 1
        $postData = $audience->toArray();
114
                
115 1
        $response = $this->client->sendApiRequest("/audiences/$audienceId", array(), 'PATCH', 
116 1
                $postData, array(200));
117
        
118 1
        return new Audience($response);
119
    }
120
}
121
122
123
124
125
126