Completed
Push — master ( 1d1446...5246d9 )
by Oleg
02:12
created

Campaigns::create()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2.032

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 1
dl 0
loc 17
ccs 8
cts 10
cp 0.8
crap 2.032
rs 9.4285
c 0
b 0
f 0
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 (!is_int($projectId)) {
43
            throw new Exception("Integer project ID expected, while got '$projectId'");
44
        }
45
        
46 3
        if ($projectId<0) {
47
            throw new Exception("Expected positive integer project ID");
48
        }
49
        
50 3
        if ($page<0) {
51 1
            throw new Exception('Invalid page number passed');
52
        }
53
        
54 2
        if ($perPage<0 || $perPage>100) {
55 1
            throw new Exception('Invalid page size passed');
56
        }
57
        
58 1
        $result = $this->client->sendApiRequest('/campaigns', 
59
                array(
60 1
                    'project_id'=>$projectId,
61 1
                    'page'=>$page,
62
                    'per_page'=>$perPage
63 1
                ));
64
        
65 1
        $campaigns = array();
66 1
        foreach ($result->getDecodedJsonData() as $campaignInfo) {
67 1
            $campaign = new Campaign($campaignInfo);
68 1
            $campaigns[] = $campaign;
69 1
        }
70 1
        $result->setPayload($campaigns);
71
        
72 1
        return $result;
73
    }
74
    
75
    /**
76
     * Reads a campaign.
77
     * @param integer $campaignId
78
     * @return Result
79
     * @throws Exception
80
     */
81 1 View Code Duplication
    public function get($campaignId)
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...
82
    {
83 1
        if (!is_int($campaignId)) {
84
            throw new Exception("Integer campaign ID expected, while got '$campaignId'");
85
        }
86
        
87 1
        $result = $this->client->sendApiRequest("/campaigns/$campaignId");
88
        
89 1
        $campaign = new Campaign($result->getDecodedJsonData());
90 1
        $result->setPayload($campaign);
91
        
92 1
        return $result;
93
    }
94
    
95
    /**
96
     * Get campaign results
97
     * @param integer $campaignId The id for the campaign you want results for
98
     * @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...
99
     * @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.
100
     * @return Result
101
     * @throws Exception
102
     */
103 1
    public function getResults($campaignId, $startTime = null, $endTime = null)
104
    {
105 1
        if (!is_int($campaignId)) {
106
            throw new Exception("Integer campaign ID expected, while got '$campaignId'",
107
                    Exception::CODE_INVALID_ARG);
108
        }
109
        
110 1
        if ($campaignId<0) {
111
            throw new Exception("Expected positive integer campaign ID",
112
                    Exception::CODE_INVALID_ARG);
113
        }
114
        
115 1
        $result = $this->client->sendApiRequest("/campaigns/$campaignId/results", 
116
                array(
117 1
                    'campaign_id' => $campaignId,
118 1
                    'start_time' => $startTime,
119
                    'end_time' => $endTime
120 1
                ));
121
        
122 1
        $campaignResults = new CampaignResults($result->getDecodedJsonData());
123 1
        $result->setPayload($campaignResults);
124
        
125 1
        return $result;
126
    }
127
    
128
    /**
129
     * Create a new Campaign in your account
130
     * @param Campaign $campaign
131
     * @return Result
132
     * @throws Exception
133
     */
134 1
    public function create($campaign)
135
    {
136 1
        if (!($campaign instanceOf Campaign)) {
137
            throw new Exception("Expected argument of type Campaign",
138
                    Exception::CODE_INVALID_ARG);
139
        }
140
        
141 1
        $postData = $campaign->toArray();
142
        
143 1
        $result = $this->client->sendApiRequest("/campaigns", array(), 'POST', 
144 1
                $postData);
145
        
146 1
        $campaign = new Campaign($result->getDecodedJsonData());
147 1
        $result->setPayload($campaign);
148
        
149 1
        return $result;
150
    }
151
    
152
    /**
153
     * Update a Campaign
154
     * @param integer $campaignId
155
     * @param Campaign $campaign
156
     * @return Result
157
     * @throws Exception
158
     */
159 1
    public function update($campaignId, $campaign) 
160
    {
161 1
        if (!($campaign instanceOf Campaign)) {
162
            throw new Exception("Expected argument of type Campaign",
163
                    Exception::CODE_INVALID_ARG);
164
        }
165
        
166 1
        $postData = $campaign->toArray();
167
                
168 1
        $result = $this->client->sendApiRequest("/campaigns/$campaignId", array(), 'PATCH', 
169 1
                $postData);
170
        
171 1
        $campaign = new Campaign($result->getDecodedJsonData());
172 1
        $result->setPayload($campaign);
173
        
174 1
        return $result;
175
    }
176
    
177
    /**
178
     * Delete Campaign by ID
179
     * @param integer $campaignId
180
     * @return Result
181
     * @throws Exception
182
     */
183 1
    public function delete($campaignId) 
184
    {
185 1
        $result = $this->client->sendApiRequest("/campaigns/$campaignId", array(), 'DELETE', 
186 1
                array());
187
        
188 1
        return $result;
189
    }
190
}
191
192
193
194