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

Campaigns::get()   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 06 October 2016
5
 * @copyright (c) 2016, Web Marketing ROI
6
 */
7
namespace WebMarketingROI\OptimizelyPHP\Service\v2;
8
9
use WebMarketingROI\OptimizelyPHP\Resource\v2\Campaign;
10
use WebMarketingROI\OptimizelyPHP\Resource\v2\CampaignResults;
11
12
/**
13
 * Provides methods for working with Optimizely campaigns.
14
 */
15
class Campaigns
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
     * Returns the list of campaigns.
33
     * @param integer $projectId
34
     * @param integer $page
35
     * @param integer $perPage
36
     * @return Result
37
     * @throws Exception
38
     */
39 1
    public function listAll($projectId, $page=1, $perPage=25)
40
    {
41 1
        if (!is_int($projectId)) {
42
            throw new Exception("Integer project ID expected, while got '$projectId'");
43
        }
44
        
45 1
        if ($projectId<0) {
46
            throw new Exception("Expected positive integer project ID");
47
        }
48
        
49 1
        if ($page<0) {
50
            throw new Exception('Invalid page number passed');
51
        }
52
        
53 1
        if ($perPage<0) {
54
            throw new Exception('Invalid page size passed');
55
        }
56
        
57 1
        $result = $this->client->sendApiRequest('/campaigns', 
58
                array(
59 1
                    'project_id'=>$projectId,
60 1
                    'page'=>$page,
61
                    'per_page'=>$perPage
62 1
                ));
63
        
64 1
        $campaigns = array();
65 1
        foreach ($result->getDecodedJsonData() as $campaignInfo) {
66 1
            $campaign = new Campaign($campaignInfo);
67 1
            $campaigns[] = $campaign;
68 1
        }
69 1
        $result->setPayload($campaigns);
70
        
71 1
        return $result;
72
    }
73
    
74
    /**
75
     * Reads a campaign.
76
     * @param integer $campaignId
77
     * @return Result
78
     * @throws Exception
79
     */
80 1
    public function get($campaignId)
81
    {
82 1
        if (!is_int($campaignId)) {
83
            throw new Exception("Integer campaign ID expected, while got '$campaignId'");
84
        }
85
        
86 1
        $result = $this->client->sendApiRequest("/campaigns/$campaignId");
87
        
88 1
        $campaign = new Campaign($result->getDecodedJsonData());
89 1
        $result->setPayload($campaign);
90
        
91 1
        return $result;
92
    }
93
    
94
    /**
95
     * Get campaign results
96
     * @param integer $campaignId The id for the campaign you want results for
97
     * @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...
98
     * @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.
99
     * @return Result
100
     * @throws Exception
101
     */
102 1
    public function getResults($campaignId, $startTime = null, $endTime = null)
103
    {
104 1
        if (!is_int($campaignId)) {
105
            throw new Exception("Integer campaign ID expected, while got '$campaignId'",
106
                    Exception::CODE_INVALID_ARG);
107
        }
108
        
109 1
        if ($campaignId<0) {
110
            throw new Exception("Expected positive integer campaign ID",
111
                    Exception::CODE_INVALID_ARG);
112
        }
113
        
114 1
        $result = $this->client->sendApiRequest("/campaigns/$campaignId/results", 
115
                array(
116 1
                    'campaign_id' => $campaignId,
117 1
                    'start_time' => $startTime,
118
                    'end_time' => $endTime
119 1
                ));
120
        
121 1
        $campaignResults = new CampaignResults($result->getDecodedJsonData());
122 1
        $result->setPayload($campaignResults);
123
        
124 1
        return $result;
125
    }
126
    
127
    /**
128
     * Create a new Campaign in your account
129
     * @param Campaign $campaign
130
     * @return Result
131
     * @throws Exception
132
     */
133 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...
134
    {
135 1
        if (!($campaign instanceOf Campaign)) {
136
            throw new Exception("Expected argument of type Campaign",
137
                    Exception::CODE_INVALID_ARG);
138
        }
139
        
140 1
        $postData = $campaign->toArray();
141
        
142 1
        $result = $this->client->sendApiRequest("/campaigns", array(), 'POST', 
143 1
                $postData);
144
        
145 1
        $campaign = new Campaign($result->getDecodedJsonData());
146 1
        $result->setPayload($campaign);
147
        
148 1
        return $result;
149
    }
150
    
151
    /**
152
     * Update a Campaign
153
     * @param integer $campaignId
154
     * @param Campaign $campaign
155
     * @return Result
156
     * @throws Exception
157
     */
158 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...
159
    {
160 1
        if (!($campaign instanceOf Campaign)) {
161
            throw new Exception("Expected argument of type Campaign",
162
                    Exception::CODE_INVALID_ARG);
163
        }
164
        
165 1
        $postData = $campaign->toArray();
166
                
167 1
        $result = $this->client->sendApiRequest("/campaigns/$campaignId", array(), 'PATCH', 
168 1
                $postData);
169
        
170 1
        $campaign = new Campaign($result->getDecodedJsonData());
171 1
        $result->setPayload($campaign);
172
        
173 1
        return $result;
174
    }
175
    
176
    /**
177
     * Delete Campaign by ID
178
     * @param integer $campaignId
179
     * @return Result
180
     * @throws Exception
181
     */
182 1
    public function delete($campaignId) 
183
    {
184 1
        $result = $this->client->sendApiRequest("/campaigns/$campaignId", array(), 'DELETE', 
185 1
                array());
186
        
187 1
        return $result;
188
    }
189
}
190
191
192
193