OutlookRestClient::deleteEvent()   A
last analyzed

Complexity

Conditions 2
Paths 3

Size

Total Lines 19
Code Lines 11

Duplication

Lines 19
Ratio 100 %

Importance

Changes 0
Metric Value
dl 19
loc 19
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 11
nc 3
nop 1
1
<?php namespace OutlookRestClient\Facade;
2
/**
3
 * Copyright 2017 OpenStack Foundation
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 * http://www.apache.org/licenses/LICENSE-2.0
8
 * Unless required by applicable law or agreed to in writing, software
9
 * distributed under the License is distributed on an "AS IS" BASIS,
10
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
 * See the License for the specific language governing permissions and
12
 * limitations under the License.
13
 **/
14
use GuzzleHttp\Client;
15
use GuzzleHttp\Exception\ClientException;
16
use GuzzleHttp\Psr7\Request;
17
use OutlookRestClient\Facade\Responses\CalendarCollectionResponse;
18
use OutlookRestClient\Facade\Responses\CalendarResponse;
19
use OutlookRestClient\Facade\Responses\ErrorResponse;
20
use OutlookRestClient\Facade\Responses\EventResponse;
21
use OutlookRestClient\Facade\Utils\HttpMethods;
22
use OutlookRestClient\IOutlookRestClient;
23
use OutlookRestClient\Facade\Requests\CalendarVO;
24
use OutlookRestClient\Facade\Requests\EventVO;
25
26
/**
27
 * Class OutlookRestClient
28
 * @see https://docs.microsoft.com/en-us/outlook/rest/compare-graph-outlook
29
 * @package OutlookRestClient\Facade
30
 */
31
final class OutlookRestClient implements IOutlookRestClient
32
{
33
34
    /**
35
     * @var string
36
     */
37
    const BaseUrl = 'https://outlook.office.com/api/v2.0/';
38
39
    /**
40
     * @var Client
41
     */
42
    private $client;
43
44
    /**
45
     * @var int
46
     */
47
    private $timeout = 60;
48
49
    /**
50
     * @var null|ITokenManager
51
     */
52
    private $token_manager = null;
53
54
    /**
55
     * OutlookRestClient constructor.
56
     */
57
    public function __construct()
58
    {
59
        $this->client        = new Client(['base_uri' => self::BaseUrl]);
60
        $this->token_manager = new TokenManager();
61
    }
62
63
    /**
64
     * @return array
65
     */
66
    private function getDefaultHeaders()
67
    {
68
        $access_token = $this->token_manager->getAccessToken();
69
        if(is_null($access_token)) throw new \LogicException("access token is null!");
70
        return [
71
            'Content-Type'  => 'application/json',
72
            'Authorization' => 'Bearer ' . $access_token['access_token']
73
        ];
74
    }
75
76
    /**
77
     * @param CalendarVO $calendar
78
     * @return CalendarResponse|ErrorResponse
79
     */
80
    public function createCalendar(CalendarVO $calendar)
81
    {
82
        $http_request = new Request
83
        (
84
            HttpMethods::Post,
85
            'me/calendars',
86
            $this->getDefaultHeaders(),
87
            json_encode($calendar->toArray())
88
        );
89
90
        try{
91
            $http_response =  $this->client->send($http_request, [
92
                'timeout'  => $this->timeout,
93
            ]);
94
            return new CalendarResponse((string)$http_response->getBody());
95
        }
96
        catch (ClientException $ex){
97
            return new ErrorResponse( (string)$ex->getResponse()->getBody());
98
        }
99
    }
100
101
    /**
102
     * @param string $calendar_id
103
     * @param CalendarVO $calendar
104
     * @return  CalendarResponse|ErrorResponse
105
     */
106
    public function updateCalendar($calendar_id, CalendarVO $calendar)
107
    {
108
        $http_request = new Request
109
        (
110
            HttpMethods::Patch,
111
            "me/calendars/{$calendar_id}",
112
            $this->getDefaultHeaders(),
113
            json_encode($calendar->toArray())
114
        );
115
116
        try{
117
            $http_response =  $this->client->send($http_request, [
118
                'timeout'  => $this->timeout,
119
            ]);
120
            return new CalendarResponse((string)$http_response->getBody());
121
        }
122
        catch (ClientException $ex){
123
            return new ErrorResponse( (string)$ex->getResponse()->getBody());
124
        }
125
    }
126
127
    /**
128
     * @param string $calendar_id
129
     * @return bool|ErrorResponse
130
     */
131 View Code Duplication
    public function deleteCalendar($calendar_id)
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...
132
    {
133
        $http_request = new Request
134
        (
135
            HttpMethods::Delete,
136
            "me/calendars/{$calendar_id}",
137
            $this->getDefaultHeaders()
138
        );
139
140
        try{
141
            $http_response =  $this->client->send($http_request, [
142
                'timeout'  => $this->timeout,
143
            ]);
144
            return $http_response->getStatusCode() == 204;
145
        }
146
        catch (ClientException $ex){
147
            return new ErrorResponse((string)$ex->getResponse()->getBody());
148
        }
149
    }
150
151
    /**
152
     * @param string $calendar_id
153
     * @param EventVO $event
154
     * @return ErrorResponse|EventResponse
155
     */
156
    public function createEvent($calendar_id, EventVO $event)
157
    {
158
        $http_request = new Request
159
        (
160
            HttpMethods::Post,
161
            "me/calendars/{$calendar_id}/events",
162
            $this->getDefaultHeaders(),
163
            json_encode($event->toArray())
164
        );
165
166
        try{
167
            $http_response =  $this->client->send($http_request, [
168
                'timeout'  => $this->timeout,
169
            ]);
170
            return new EventResponse((string)$http_response->getBody());
171
        }
172
        catch (ClientException $ex){
173
            return new ErrorResponse( (string)$ex->getResponse()->getBody());
174
        }
175
    }
176
177
    /**
178
     * @param string $event_id
179
     * @param EventVO $event
180
     * @return EventResponse|ErrorResponse
181
     */
182
    public function updateEvent($event_id, EventVO $event)
183
    {
184
        $http_request = new Request
185
        (
186
            HttpMethods::Patch,
187
            "me/events/{$event_id}",
188
            $this->getDefaultHeaders(),
189
            json_encode($event->toArray())
190
        );
191
192
        try{
193
            $http_response =  $this->client->send($http_request, [
194
                'timeout'  => $this->timeout,
195
            ]);
196
            return new EventResponse((string)$http_response->getBody());
197
        }
198
        catch (ClientException $ex){
199
            return new ErrorResponse( (string)$ex->getResponse()->getBody());
200
        }
201
    }
202
203
    /**
204
     * @param string $event_id
205
     * @return bool|ErrorResponse
206
     */
207 View Code Duplication
    public function deleteEvent($event_id)
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...
208
    {
209
        $http_request = new Request
210
        (
211
            HttpMethods::Delete,
212
            "me/events/{$event_id}",
213
            $this->getDefaultHeaders()
214
        );
215
216
        try{
217
            $http_response =  $this->client->send($http_request, [
218
                'timeout'  => $this->timeout,
219
            ]);
220
            return $http_response->getStatusCode() == 204;
221
        }
222
        catch (ClientException $ex){
223
            return new ErrorResponse((string)$ex->getResponse()->getBody());
224
        }
225
    }
226
227
    public function setAccessToken(array $access_token = null)
228
    {
229
        $this->token_manager->storeToken($access_token);
0 ignored issues
show
Bug introduced by
It seems like $access_token defined by parameter $access_token on line 227 can also be of type null; however, OutlookRestClient\Facade...enManager::storeToken() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
230
    }
231
232
    /**
233
     * @return mixed
234
     */
235
    public function isAccessTokenExpired()
236
    {
237
       return $this->token_manager->isAccessTokenExpired();
238
    }
239
240
    /**
241
     * @return CalendarCollectionResponse|ErrorResponse
242
     */
243 View Code Duplication
    public function getCalendars()
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...
244
    {
245
246
        $http_request = new Request
247
        (
248
            HttpMethods::Get,
249
            'me/calendars',
250
            $this->getDefaultHeaders()
251
        );
252
253
        try{
254
            $http_response =  $this->client->send($http_request, [
255
                'timeout'  => $this->timeout,
256
            ]);
257
            return new CalendarCollectionResponse((string)$http_response->getBody());
258
        }
259
        catch (ClientException $ex){
260
            return new ErrorResponse( (string)$ex->getResponse()->getBody());
261
        }
262
    }
263
264
    /**
265
     * sets function to be called when an access token is fetched
266
     * @param callable $token_callback
267
     * @return void
268
     */
269
    public function setTokenCallback(callable $token_callback)
270
    {
271
        $this->token_manager->setTokenCallback($token_callback);
272
    }
273
}