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

Events::createCustomEvent()   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 2
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\Event;
10
use WebMarketingROI\OptimizelyPHP\Resource\v2\ClickEvent;
11
use WebMarketingROI\OptimizelyPHP\Resource\v2\CustomEvent;
12
13
/**
14
 * Provides methods for working with Optimizely events.
15
 */
16
class Events
17
{
18
    /**
19
     * Optimizely API Client.
20
     * @var WebMarketingROI\OptimizelyPHP\OptimizelyApiClient
21
     */
22
    private $client;
23
    
24
    /**
25
     * Constructor.
26
     */
27 6
    public function __construct($client)
28
    {
29 6
        $this->client = $client;
30 6
    }
31
    
32
    /**
33
     * Get all events for a Project
34
     * @param integer $projectId
35
     * @param integer $includeClassic
36
     * @param integer $page
37
     * @param integer $perPage
38
     * @return array[Event]
0 ignored issues
show
Documentation introduced by
The doc-type array[Event] could not be parsed: Expected "]" at position 2, but found "Event". (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...
39
     * @throws \Exception
40
     */
41 1 View Code Duplication
    public function listAll($projectId, $includeClassic, $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...
42
    {
43 1
        if ($page<0) {
44
            throw new \Exception('Invalid page number passed');
45
        }
46
        
47 1
        if ($perPage<0) {
48
            throw new \Exception('Invalid page size passed');
49
        }
50
        
51 1
        $response = $this->client->sendApiRequest('/events', 
52
                array(
53 1
                    'project_id'=>$projectId,
54 1
                    'include_classic'=>$includeClassic,
55 1
                    'page'=>$page,
56
                    'per_page'=>$perPage
57 1
                ));
58
        
59 1
        $events = array();
60 1
        foreach ($response as $eventInfo) {
61 1
            $event = new Event($eventInfo);
62 1
            $events[] = $event;
63 1
        }
64
        
65 1
        return $events;
66
    }
67
    
68
    /**
69
     * Get Event by ID
70
     * @param type $eventId
71
     * @return Event
72
     * @throws \Exception
73
     */
74 1 View Code Duplication
    public function get($eventId)
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...
75
    {
76 1
        if (!is_int($eventId)) {
77
            throw new \Exception("Integer event ID expected, while got '$eventId'");
78
        }
79
        
80 1
        $response = $this->client->sendApiRequest("/events/$eventId");
81
        
82 1
        $event = new Event($response);
83
        
84 1
        return $event;
85
    }
86
    
87
    /**
88
     * Creates a new click event.
89
     * @param integer $pageId
90
     * @param ClickEvent $event
91
     */
92 1
    public function createClickEvent($pageId, $event)
93
    {
94 1
        if (!($event instanceOf ClickEvent)) {
95
            throw new \Exception("Expected argument of type ClickEvent");
96
        }
97
        
98 1
        $postData = $event->toArray();
99
        
100 1
        $response = $this->client->sendApiRequest("/pages/$pageId/click_events", array(), 'POST', 
101 1
                $postData, array(201));
102
        
103 1
        return new ClickEvent($response);
104
    }
105
    
106
    /**
107
     * Creates a new custom event.
108
     * @param integer $pageId
109
     * @param CustomEvent $event
110
     */
111 1
    public function createCustomEvent($pageId, $event)
112
    {
113 1
        if (!($event instanceOf CustomEvent)) {
114
            throw new \Exception("Expected argument of type CustomEvent");
115
        }
116
        
117 1
        $postData = $event->toArray();
118
        
119 1
        $response = $this->client->sendApiRequest("/pages/$pageId/custom_events", array(), 'POST', 
120 1
                $postData, array(201));
121
        
122 1
        return new CustomEvent($response);
123
    }
124
        
125
    /**
126
     * Updates the given click event.
127
     * @param integer $pageId
128
     * @param integer $eventId
129
     * @param ClickEvent $event
130
     * @throws \Exception
131
     */
132 1 View Code Duplication
    public function updateClickEvent($pageId, $eventId, $event) 
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...
133
    {
134 1
        if (!($event instanceOf ClickEvent)) {
135
            throw new \Exception("Expected argument of type ClickEvent");
136
        }
137
        
138 1
        $postData = $event->toArray();
139
                
140 1
        $response = $this->client->sendApiRequest("/pages/$pageId/click_events/$eventId", array(), 'PATCH', 
141 1
                $postData, array(200));
142
        
143 1
        return new ClickEvent($response);
144
    }
145
    
146
    /**
147
     * Updates the given custom event.
148
     * @param integer $pageId
149
     * @param integer $eventId
150
     * @param CustomEvent $event
151
     * @throws \Exception
152
     */
153 1 View Code Duplication
    public function updateCustomEvent($pageId, $eventId, $event) 
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...
154
    {
155 1
        if (!($event instanceOf CustomEvent)) {
156
            throw new \Exception("Expected argument of type CustomEvent");
157
        }
158
        
159 1
        $postData = $event->toArray();
160
                
161 1
        $response = $this->client->sendApiRequest("/pages/$pageId/custom_events/$eventId", array(), 'PATCH', 
162 1
                $postData, array(200));
163
        
164 1
        return new CustomEvent($response);
165
    }
166
}
167
168
169
170
171
172