Completed
Push — master ( 96059f...fe2237 )
by Oleg
06:07
created

Campaigns::listAll()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 6.0061

Importance

Changes 0
Metric Value
cc 6
eloc 18
c 0
b 0
f 0
nc 5
nop 3
dl 0
loc 30
ccs 17
cts 18
cp 0.9444
crap 6.0061
rs 8.439
1
<?php
2
/**
3
 * @author Oleg Krivtsov <[email protected]>
4
 * @date 06 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\Campaign;
11
use WebMarketingROI\OptimizelyPHP\Resource\v2\CampaignResults;
12
13
/**
14
 * Provides methods for working with Optimizely campaigns.
15
 */
16
class Campaigns
17
{
18
    /**
19
     * Optimizely API Client.
20
     * @var WebMarketingROI\OptimizelyPHP\OptimizelyApiClient
21
     */
22
    private $client;
23
    
24
    /**
25
     * Constructor.
26
     */
27 8
    public function __construct($client)
28
    {
29 8
        $this->client = $client;
30 8
    }
31
    
32
    /**
33
     * Returns the list of campaigns.
34
     * @param integer $projectId
35
     * @param integer $page
36
     * @param integer $perPage
37
     * @return Result
38
     * @throws Exception
39
     */
40 3
    public function listAll($projectId, $page=1, $perPage=25)
41
    {
42 3
        if ($projectId<0) {
43
            throw new Exception("Expected positive integer project ID");
44
        }
45
        
46 3
        if ($page<0) {
47 1
            throw new Exception('Invalid page number passed');
48
        }
49
        
50 2
        if ($perPage<0 || $perPage>100) {
51 1
            throw new Exception('Invalid page size passed');
52
        }
53
        
54 1
        $result = $this->client->sendApiRequest('/campaigns', 
55
                array(
56 1
                    'project_id'=>$projectId,
57 1
                    'page'=>$page,
58
                    'per_page'=>$perPage
59 1
                ));
60
        
61 1
        $campaigns = array();
62 1
        foreach ($result->getDecodedJsonData() as $campaignInfo) {
63 1
            $campaign = new Campaign($campaignInfo);
64 1
            $campaigns[] = $campaign;
65 1
        }
66 1
        $result->setPayload($campaigns);
67
        
68 1
        return $result;
69
    }
70
    
71
    /**
72
     * Reads a campaign.
73
     * @param integer $campaignId
74
     * @return Result
75
     * @throws Exception
76
     */
77 1
    public function get($campaignId)
78
    {
79 1
        if (!is_int($campaignId)) {
80
            throw new Exception("Integer campaign ID expected, while got '$campaignId'");
81
        }
82
        
83 1
        $result = $this->client->sendApiRequest("/campaigns/$campaignId");
84
        
85 1
        $campaign = new Campaign($result->getDecodedJsonData());
86 1
        $result->setPayload($campaign);
87
        
88 1
        return $result;
89
    }
90
    
91
    /**
92
     * Get campaign results
93
     * @param integer $campaignId The id for the campaign you want results for
94
     * @param string $starTime The earliest time to count events in results. Defaults to the time that the campaign was first activated.
0 ignored issues
show
Documentation introduced by
There is no parameter named $starTime. Did you maybe mean $startTime?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
95
     * @param string $endTime The latest time to count events in results. Defaults to the time the campaign was last active or the current time if the campaign is still running.
96
     * @return Result
97
     * @throws Exception
98
     */
99 1
    public function getResults($campaignId, $startTime = null, $endTime = null)
100
    {
101 1
        if (!is_int($campaignId)) {
102
            throw new Exception("Integer campaign ID expected, while got '$campaignId'",
103
                    Exception::CODE_INVALID_ARG);
104
        }
105
        
106 1
        if ($campaignId<0) {
107
            throw new Exception("Expected positive integer campaign ID",
108
                    Exception::CODE_INVALID_ARG);
109
        }
110
        
111 1
        $result = $this->client->sendApiRequest("/campaigns/$campaignId/results", 
112
                array(
113 1
                    'campaign_id' => $campaignId,
114 1
                    'start_time' => $startTime,
115
                    'end_time' => $endTime
116 1
                ));
117
        
118 1
        $campaignResults = new CampaignResults($result->getDecodedJsonData());
119 1
        $result->setPayload($campaignResults);
120
        
121 1
        return $result;
122
    }
123
    
124
    /**
125
     * Create a new Campaign in your account
126
     * @param Campaign $campaign
127
     * @return Result
128
     * @throws Exception
129
     */
130 1 View Code Duplication
    public function create($campaign)
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...
131
    {
132 1
        if (!($campaign instanceOf Campaign)) {
133
            throw new Exception("Expected argument of type Campaign",
134
                    Exception::CODE_INVALID_ARG);
135
        }
136
        
137 1
        $postData = $campaign->toArray();
138
        
139 1
        $result = $this->client->sendApiRequest("/campaigns", array(), 'POST', 
140 1
                $postData);
141
        
142 1
        $campaign = new Campaign($result->getDecodedJsonData());
143 1
        $result->setPayload($campaign);
144
        
145 1
        return $result;
146
    }
147
    
148
    /**
149
     * Update a Campaign
150
     * @param integer $campaignId
151
     * @param Campaign $campaign
152
     * @return Result
153
     * @throws Exception
154
     */
155 1 View Code Duplication
    public function update($campaignId, $campaign) 
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...
156
    {
157 1
        if (!($campaign instanceOf Campaign)) {
158
            throw new Exception("Expected argument of type Campaign",
159
                    Exception::CODE_INVALID_ARG);
160
        }
161
        
162 1
        $postData = $campaign->toArray();
163
                
164 1
        $result = $this->client->sendApiRequest("/campaigns/$campaignId", array(), 'PATCH', 
165 1
                $postData);
166
        
167 1
        $campaign = new Campaign($result->getDecodedJsonData());
168 1
        $result->setPayload($campaign);
169
        
170 1
        return $result;
171
    }
172
    
173
    /**
174
     * Delete Campaign by ID
175
     * @param integer $campaignId
176
     * @return Result
177
     * @throws Exception
178
     */
179 1
    public function delete($campaignId) 
180
    {
181 1
        $result = $this->client->sendApiRequest("/campaigns/$campaignId", array(), 'DELETE', 
182 1
                array());
183
        
184 1
        return $result;
185
    }
186
}
187
188
189
190