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

Events::createCustomEvent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 10

Duplication

Lines 17
Ratio 100 %

Code Coverage

Tests 8
CRAP Score 2.032

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 2
dl 17
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 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 Result
39
     * @throws Exception
40
     */
41 1
    public function listAll($projectId, $includeClassic, $page=1, $perPage=25)
42
    {
43 1
        if ($page<0) {
44
            throw new Exception('Invalid page number passed',
45
                    Exception::CODE_INVALID_ARG);
46
        }
47
        
48 1
        if ($perPage<0) {
49
            throw new Exception('Invalid page size passed',
50
                    Exception::CODE_INVALID_ARG);
51
        }
52
        
53 1
        $result = $this->client->sendApiRequest('/events', 
54
                array(
55 1
                    'project_id'=>$projectId,
56 1
                    'include_classic'=>$includeClassic,
57 1
                    'page'=>$page,
58
                    'per_page'=>$perPage
59 1
                ));
60
        
61 1
        $events = array();
62 1
        foreach ($result->getDecodedJsonData() as $eventInfo) {
63 1
            $event = new Event($eventInfo);
64 1
            $events[] = $event;
65 1
        }
66 1
        $result->setPayload($events);
67
        
68 1
        return $result;
69
    }
70
    
71
    /**
72
     * Get Event by ID
73
     * @param type $eventId
74
     * @return Result
75
     * @throws Exception
76
     */
77 1
    public function get($eventId)
78
    {
79 1
        if (!is_int($eventId)) {
80
            throw new Exception("Integer event ID expected, while got '$eventId'",
81
                    Exception::CODE_INVALID_ARG);
82
        }
83
        
84 1
        $result = $this->client->sendApiRequest("/events/$eventId");
85
        
86 1
        $event = new Event($result->getDecodedJsonData());
87 1
        $result->setPayload($event);
88
        
89 1
        return $result;
90
    }
91
    
92
    /**
93
     * Creates a new click event.
94
     * @param integer $pageId
95
     * @param ClickEvent $event
96
     * @return Result
97
     * @throws Exception
98
     */
99 1 View Code Duplication
    public function createClickEvent($pageId, $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...
100
    {
101 1
        if (!($event instanceOf ClickEvent)) {
102
            throw new Exception("Expected argument of type ClickEvent",
103
                    Exception::CODE_INVALID_ARG);
104
        }
105
        
106 1
        $postData = $event->toArray();
107
        
108 1
        $result = $this->client->sendApiRequest("/pages/$pageId/click_events", array(), 'POST', 
109 1
                $postData);
110
        
111 1
        $event = new ClickEvent($result->getDecodedJsonData());
112 1
        $result->setPayload($event);
113
        
114 1
        return $result;
115
    }
116
    
117
    /**
118
     * Creates a new custom event.
119
     * @param integer $pageId
120
     * @param CustomEvent $event
121
     * @return Result
122
     * @throws Exception
123
     */
124 1 View Code Duplication
    public function createCustomEvent($pageId, $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...
125
    {
126 1
        if (!($event instanceOf CustomEvent)) {
127
            throw new Exception("Expected argument of type CustomEvent",
128
                    Exception::CODE_INVALID_ARG);
129
        }
130
        
131 1
        $postData = $event->toArray();
132
        
133 1
        $result = $this->client->sendApiRequest("/pages/$pageId/custom_events", array(), 'POST', 
134 1
                $postData);
135
        
136 1
        $event = new CustomEvent($result->getDecodedJsonData());
137 1
        $result->setPayload($event);
138
        
139 1
        return $result;
140
    }
141
        
142
    /**
143
     * Updates the given click event.
144
     * @param integer $pageId
145
     * @param integer $eventId
146
     * @param ClickEvent $event
147
     * @return Result
148
     * @throws Exception
149
     */
150 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...
151
    {
152 1
        if (!($event instanceOf ClickEvent)) {
153
            throw new Exception("Expected argument of type ClickEvent",
154
                    Exception::CODE_INVALID_ARG);
155
        }
156
        
157 1
        $postData = $event->toArray();
158
                
159 1
        $result = $this->client->sendApiRequest("/pages/$pageId/click_events/$eventId", array(), 'PATCH', 
160 1
                $postData);
161
        
162 1
        $event = new ClickEvent($result->getDecodedJsonData());
163 1
        $result->setPayload($event);
164
        
165 1
        return $result;
166
    }
167
    
168
    /**
169
     * Updates the given custom event.
170
     * @param integer $pageId
171
     * @param integer $eventId
172
     * @param CustomEvent $event
173
     * @return Result
174
     * @throws Exception
175
     */
176 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...
177
    {
178 1
        if (!($event instanceOf CustomEvent)) {
179
            throw new Exception("Expected argument of type CustomEvent",
180
                    Exception::CODE_INVALID_ARG);
181
        }
182
        
183 1
        $postData = $event->toArray();
184
                
185 1
        $result = $this->client->sendApiRequest("/pages/$pageId/custom_events/$eventId", array(), 'PATCH', 
186 1
                $postData);        
187
        
188 1
        $event = new CustomEvent($result->getDecodedJsonData());
189 1
        $result->setPayload($event);
190
        
191 1
        return $result;
192
    }
193
}
194
195
196
197
198
199