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

Campaigns::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 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 array[Campaign]
0 ignored issues
show
Documentation introduced by
The doc-type array[Campaign] could not be parsed: Expected "]" at position 2, but found "Campaign". (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...
37
     * @throws \Exception
38
     */
39 1
    public function listAll($projectId, $page=0, $perPage=10)
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
        $response = $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 ($response as $campaignInfo) {
66 1
            $campaign = new Campaign($campaignInfo);
67 1
            $campaigns[] = $campaign;
68 1
        }
69
        
70 1
        return $campaigns;
71
    }
72
    
73
    /**
74
     * Reads a campaign.
75
     * @param integer $campaignId
76
     * @return Campaign
77
     * @throws \Exception
78
     */
79 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...
80
    {
81 1
        if (!is_int($campaignId)) {
82
            throw new \Exception("Integer campaign ID expected, while got '$campaignId'");
83
        }
84
        
85 1
        $response = $this->client->sendApiRequest("/campaigns/$campaignId");
86
        
87 1
        $campaign = new Campaign($response);
88
        
89 1
        return $campaign;
90
    }
91
    
92
    /**
93
     * Get campaign results
94
     * @param integer $campaignId The id for the campaign you want results for
95
     * @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...
96
     * @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.
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
        }
104
        
105 1
        if ($campaignId<0) {
106
            throw new \Exception("Expected positive integer campaign ID");
107
        }
108
        
109 1
        $response = $this->client->sendApiRequest("/campaigns/$campaignId/results", 
110
                array(
111 1
                    'campaign_id' => $campaignId,
112 1
                    'start_time' => $startTime,
113
                    'end_time' => $endTime
114 1
                ));
115
        
116 1
        $results = new CampaignResults($response);
117
        
118 1
        return $results;
119
    }
120
    
121
    /**
122
     * Create a new Campaign in your account
123
     * @param Campaign $campaign
124
     */
125 1
    public function create($campaign)
126
    {
127 1
        if (!($campaign instanceOf \WebMarketingROI\OptimizelyPHP\Resource\v2\Campaign)) {
128
            throw new \Exception("Expected argument of type Campaign");
129
        }
130
        
131 1
        $postData = $campaign->toArray();
132
        
133 1
        $response = $this->client->sendApiRequest("/campaigns", array(), 'POST', 
134 1
                $postData, array(201));
135
        
136 1
        return new Campaign($response);
137
    }
138
    
139
    /**
140
     * Update a Campaign
141
     * @param integer $campaignId
142
     * @param Campaign $campaign
143
     * @throws \Exception
144
     */
145 1
    public function update($campaignId, $campaign) 
146
    {
147 1
        if (!($campaign instanceOf \WebMarketingROI\OptimizelyPHP\Resource\v2\Campaign)) {
148
            throw new \Exception("Expected argument of type Campaign");
149
        }
150
        
151 1
        $postData = $campaign->toArray();
152
                
153 1
        $response = $this->client->sendApiRequest("/campaigns/$campaignId", array(), 'PATCH', 
154 1
                $postData, array(200));
155
        
156 1
        return new Campaign($response);
157
    }
158
    
159
    /**
160
     * Delete Campaign by ID
161
     * @param integer $campaignId
162
     * @throws \Exception
163
     */
164 1
    public function delete($campaignId) 
165
    {
166 1
        $response = $this->client->sendApiRequest("/campaigns/$campaignId", array(), 'DELETE', 
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
167 1
                array(), array(200));
168 1
    }
169
}
170
171
172
173