Completed
Pull Request — master (#1)
by
unknown
09:06
created

QueueRestProxy::createMessageAsync()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 57

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 3
dl 0
loc 57
rs 8.9381
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * LICENSE: The MIT License (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
 * https://github.com/azure/azure-storage-php/LICENSE
8
 *
9
 * Unless required by applicable law or agreed to in writing, software
10
 * distributed under the License is distributed on an "AS IS" BASIS,
11
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
 * See the License for the specific language governing permissions and
13
 * limitations under the License.
14
 *
15
 * PHP version 5
16
 *
17
 * @category  Microsoft
18
 * @package   MicrosoftAzure\Storage\Queue
19
 * @author    Azure Storage PHP SDK <[email protected]>
20
 * @copyright 2016 Microsoft Corporation
21
 * @license   https://github.com/azure/azure-storage-php/LICENSE
22
 * @link      https://github.com/azure/azure-storage-php
23
 */
24
25
namespace MicrosoftAzure\Storage\Queue;
26
27
use MicrosoftAzure\Storage\Common\Internal\ServiceRestTrait;
28
use MicrosoftAzure\Storage\Common\Internal\Resources;
29
use MicrosoftAzure\Storage\Common\Internal\Validate;
30
use MicrosoftAzure\Storage\Common\Internal\Utilities;
31
use MicrosoftAzure\Storage\Common\Internal\ServiceRestProxy;
32
use MicrosoftAzure\Storage\Common\Models\GetServicePropertiesResult;
33
use MicrosoftAzure\Storage\Common\Models\ServiceProperties;
34
use MicrosoftAzure\Storage\Common\LocationMode;
35
use MicrosoftAzure\Storage\Queue\Internal\IQueue;
36
use MicrosoftAzure\Storage\Queue\Models\ListQueuesOptions;
37
use MicrosoftAzure\Storage\Queue\Models\ListQueuesResult;
38
use MicrosoftAzure\Storage\Queue\Models\CreateQueueOptions;
39
use MicrosoftAzure\Storage\Queue\Models\QueueServiceOptions;
40
use MicrosoftAzure\Storage\Queue\Models\GetQueueMetadataResult;
41
use MicrosoftAzure\Storage\Queue\Models\CreateMessageOptions;
42
use MicrosoftAzure\Storage\Queue\Models\QueueACL;
43
use MicrosoftAzure\Storage\Queue\Models\QueueMessage;
44
use MicrosoftAzure\Storage\Queue\Models\ListMessagesOptions;
45
use MicrosoftAzure\Storage\Queue\Models\ListMessagesResult;
46
use MicrosoftAzure\Storage\Queue\Models\PeekMessagesOptions;
47
use MicrosoftAzure\Storage\Queue\Models\PeekMessagesResult;
48
use MicrosoftAzure\Storage\Queue\Models\UpdateMessageResult;
49
use MicrosoftAzure\Storage\Common\Internal\Http\HttpFormatter;
50
51
/**
52
 * This class constructs HTTP requests and receive HTTP responses for queue
53
 * service layer.
54
 *
55
 * @category  Microsoft
56
 * @package   MicrosoftAzure\Storage\Queue
57
 * @author    Azure Storage PHP SDK <[email protected]>
58
 * @copyright 2016 Microsoft Corporation
59
 * @license   https://github.com/azure/azure-storage-php/LICENSE
60
 * @link      https://github.com/azure/azure-storage-php
61
 */
62
class QueueRestProxy extends ServiceRestProxy implements IQueue
63
{
64
    use ServiceRestTrait;
65
66
    /**
67
     * Lists all queues in the storage account.
68
     *
69
     * @param ListQueuesOptions $options The optional list queue options.
70
     *
71
     * @return ListQueuesResult
72
     */
73
    public function listQueues(ListQueuesOptions $options = null)
74
    {
75
        return $this->listQueuesAsync($options)->wait();
76
    }
77
78
    /**
79
     * Creates promise to list all queues in the storage account.
80
     *
81
     * @param ListQueuesOptions $options The optional list queue options.
82
     *
83
     * @return \GuzzleHttp\Promise\PromiseInterface
84
     */
85
    public function listQueuesAsync(ListQueuesOptions $options = null)
86
    {
87
        $method      = Resources::HTTP_GET;
88
        $headers     = array();
89
        $postParams  = array();
90
        $queryParams = array();
91
        $path        = Resources::EMPTY_STRING;
92
        
93
        if (is_null($options)) {
94
            $options = new ListQueuesOptions();
95
        }
96
        
97
        $maxResults = $options->getMaxResults();
98
        $include    = $options->getIncludeMetadata();
99
        $include    = $include ? 'metadata' : null;
100
        $prefix     = $options->getPrefix();
101
        $marker     = $options->getNextMarker();
102
        
103
        $this->addOptionalQueryParam($queryParams, Resources::QP_COMP, 'list');
104
        $this->addOptionalQueryParam($queryParams, Resources::QP_PREFIX, $prefix);
105
        $this->addOptionalQueryParam($queryParams, Resources::QP_MARKER, $marker);
106
        $this->addOptionalQueryParam($queryParams, Resources::QP_INCLUDE, $include);
107
        $this->addOptionalQueryParam(
108
            $queryParams,
109
            Resources::QP_MAX_RESULTS,
110
            $maxResults
111
        );
112
113
        $dataSerializer = $this->dataSerializer;
114
        
115
        return $this->sendAsync(
116
            $method,
117
            $headers,
118
            $queryParams,
119
            $postParams,
120
            $path,
121
            Resources::STATUS_OK,
122
            Resources::EMPTY_STRING,
123
            $options
124 View Code Duplication
        )->then(function ($response) use ($dataSerializer) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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
            $parsed = $dataSerializer->unserialize($response->getBody());
126
            return ListQueuesResult::create(
127
                $parsed,
128
                Utilities::getLocationFromHeaders($response->getHeaders())
129
            );
130
        }, null);
131
    }
132
133
    /**
134
     * Clears all messages from the queue.
135
     *
136
     * If a queue contains a large number of messages, Clear Messages may time out
137
     * before all messages have been deleted. In this case the Queue service will
138
     * return status code 500 (Internal Server Error), with the additional error
139
     * code OperationTimedOut. If the operation times out, the client should
140
     * continue to retry Clear Messages until it succeeds, to ensure that all
141
     * messages have been deleted.
142
     *
143
     * @param string              $queueName The name of the queue.
144
     * @param QueueServiceOptions $options   The optional parameters.
145
     *
146
     * @return void
147
     */
148
    public function clearMessages($queueName, QueueServiceOptions $options = null)
149
    {
150
        $this->clearMessagesAsync($queueName, $options)->wait();
151
    }
152
153
    /**
154
     * Creates promise to clear all messages from the queue.
155
     *
156
     * If a queue contains a large number of messages, Clear Messages may time out
157
     * before all messages have been deleted. In this case the Queue service will
158
     * return status code 500 (Internal Server Error), with the additional error
159
     * code OperationTimedOut. If the operation times out, the client should
160
     * continue to retry Clear Messages until it succeeds, to ensure that all
161
     * messages have been deleted.
162
     *
163
     * @param string              $queueName The name of the queue.
164
     * @param QueueServiceOptions $options   The optional parameters.
165
     *
166
     * @return \GuzzleHttp\Promise\PromiseInterface
167
     */
168
    public function clearMessagesAsync(
169
        $queueName,
170
        QueueServiceOptions $options = null
171
    ) {
172
        Validate::isString($queueName, 'queueName');
173
        Validate::notNullOrEmpty($queueName, 'queueName');
174
        
175
        $method      = Resources::HTTP_DELETE;
176
        $headers     = array();
177
        $postParams  = array();
178
        $queryParams = array();
179
        $path        = $queueName . '/messages';
180
        $body        = Resources::EMPTY_STRING;
181
        
182
        if (is_null($options)) {
183
            $options = new QueueServiceOptions();
184
        }
185
        
186
        $options->setLocationMode(LocationMode::PRIMARY_ONLY);
187
188
        return $this->sendAsync(
189
            $method,
190
            $headers,
191
            $queryParams,
192
            $postParams,
193
            $path,
194
            Resources::STATUS_NO_CONTENT,
195
            $body,
196
            $options
197
        );
198
    }
199
200
    /**
201
     * Adds a message to the queue and optionally sets a visibility timeout
202
     * for the message.
203
     *
204
     * @param string               $queueName   The name of the queue.
205
     * @param string               $messageText The message contents.
206
     * @param CreateMessageOptions $options     The optional parameters.
207
     *
208
     * @return void
209
     */
210
    public function createMessage(
211
        $queueName,
212
        $messageText,
213
        CreateMessageOptions $options = null
214
    ) {
215
        $this->createMessageAsync($queueName, $messageText, $options)->wait();
216
    }
217
218
    /**
219
     * Creates promise to add a message to the queue and optionally sets a
220
     * visibility timeout for the message.
221
     *
222
     * @param string               $queueName   The name of the queue.
223
     * @param string               $messageText The message contents.
224
     * @param CreateMessageOptions $options     The optional parameters.
225
     *
226
     * @return \GuzzleHttp\Promise\PromiseInterface
227
     */
228
    public function createMessageAsync(
229
        $queueName,
230
        $messageText,
231
        CreateMessageOptions $options = null
232
    ) {
233
        Validate::isString($queueName, 'queueName');
234
        Validate::notNullOrEmpty($queueName, 'queueName');
235
        Validate::isString($messageText, 'messageText');
236
        
237
        $method      = Resources::HTTP_POST;
238
        $headers     = array();
239
        $postParams  = array();
240
        $queryParams = array();
241
        $path        = $queueName . '/messages';
242
        $body        = Resources::EMPTY_STRING;
0 ignored issues
show
Unused Code introduced by
$body 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...
243
        $message     = new QueueMessage();
244
        $message->setMessageText($messageText);
245
        $body = $message->toXml($this->dataSerializer);
0 ignored issues
show
Compatibility introduced by
$this->dataSerializer of type object<MicrosoftAzure\St...ialization\ISerializer> is not a sub-type of object<MicrosoftAzure\St...lization\XmlSerializer>. It seems like you assume a concrete implementation of the interface MicrosoftAzure\Storage\C...rialization\ISerializer to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
246
        
247
        
248
        if (is_null($options)) {
249
            $options = new CreateMessageOptions();
250
        }
251
        
252
        $this->addOptionalHeader(
253
            $headers,
254
            Resources::CONTENT_TYPE,
255
            Resources::URL_ENCODED_CONTENT_TYPE
256
        );
257
        
258
        $visibility = $options->getVisibilityTimeoutInSeconds();
259
        $timeToLive = $options->getTimeToLiveInSeconds();
260
        
261
        $this->addOptionalQueryParam(
262
            $queryParams,
263
            Resources::QP_VISIBILITY_TIMEOUT,
264
            $visibility
265
        );
266
        $this->addOptionalQueryParam(
267
            $queryParams,
268
            Resources::QP_MESSAGE_TTL,
269
            $timeToLive
270
        );
271
        
272
        $options->setLocationMode(LocationMode::PRIMARY_ONLY);
273
274
        return $this->sendAsync(
275
            $method,
276
            $headers,
277
            $queryParams,
278
            $postParams,
279
            $path,
280
            Resources::STATUS_CREATED,
281
            $body,
0 ignored issues
show
Security Bug introduced by
It seems like $body defined by $message->toXml($this->dataSerializer) on line 245 can also be of type false; however, MicrosoftAzure\Storage\C...eRestProxy::sendAsync() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
282
            $options
283
        );
284
    }
285
286
    /**
287
     * Creates a new queue under the storage account.
288
     *
289
     * @param string                    $queueName The queue name.
290
     * @param Models\CreateQueueOptions  $options   The Optional parameters.
291
     *
292
     * @return void
293
     */
294
    public function createQueue(
295
        $queueName,
296
        Models\CreateQueueOptions $options = null
297
    ) {
298
        $this->createQueueAsync($queueName, $options)->wait();
299
    }
300
301
    /**
302
     * Creates promise to create a new queue under the storage account.
303
     *
304
     * @param string                     $queueName The queue name.
305
     * @param Models\CreateQueueOptions  $options   The Optional parameters.
306
     *
307
     * @return \GuzzleHttp\Promise\PromiseInterface
308
     */
309
    public function createQueueAsync(
310
        $queueName,
311
        Models\CreateQueueOptions $options = null
312
    ) {
313
        Validate::isString($queueName, 'queueName');
314
        Validate::notNullOrEmpty($queueName, 'queueName');
315
        
316
        $method      = Resources::HTTP_PUT;
317
        $headers     = array();
0 ignored issues
show
Unused Code introduced by
$headers 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...
318
        $postParams  = array();
319
        $queryParams = array();
320
        $path        = $queueName;
321
        
322
        if (is_null($options)) {
323
            $options = new CreateQueueOptions();
324
        }
325
326
        $metadata = $options->getMetadata();
327
        $headers  = $this->generateMetadataHeaders($metadata);
328
        
329
        $options->setLocationMode(LocationMode::PRIMARY_ONLY);
330
331
        return $this->sendAsync(
332
            $method,
333
            $headers,
334
            $queryParams,
335
            $postParams,
336
            $path,
337
            array(Resources::STATUS_CREATED, Resources::STATUS_NO_CONTENT),
338
            Resources::EMPTY_STRING,
339
            $options
340
        );
341
    }
342
343
    /**
344
     * Deletes a specified message from the queue.
345
     *
346
     * @param string              $queueName  The name of the queue.
347
     * @param string              $messageId  The id of the message.
348
     * @param string              $popReceipt The valid pop receipt value returned
349
     * from an earlier call to the Get Messages or Update Message operation.
350
     * @param QueueServiceOptions $options    The optional parameters.
351
     *
352
     * @return void
353
     */
354
    public function deleteMessage(
355
        $queueName,
356
        $messageId,
357
        $popReceipt,
358
        QueueServiceOptions $options = null
359
    ) {
360
        $this->deleteMessageAsync(
361
            $queueName,
362
            $messageId,
363
            $popReceipt,
364
            $options
365
        )->wait();
366
    }
367
368
    /**
369
     * Creates promise to delete a specified message from the queue.
370
     *
371
     * @param string              $queueName  The name of the queue.
372
     * @param string              $messageId  The id of the message.
373
     * @param string              $popReceipt The valid pop receipt value returned
374
     * from an earlier call to the Get Messages or Update Message operation.
375
     * @param QueueServiceOptions $options    The optional parameters.
376
     *
377
     * @return \GuzzleHttp\Promise\PromiseInterface
378
     */
379
    public function deleteMessageAsync(
380
        $queueName,
381
        $messageId,
382
        $popReceipt,
383
        QueueServiceOptions $options = null
384
    ) {
385
        Validate::isString($queueName, 'queueName');
386
        Validate::notNullOrEmpty($queueName, 'queueName');
387
        Validate::isString($messageId, 'messageId');
388
        Validate::notNullOrEmpty($messageId, 'messageId');
389
        Validate::isString($popReceipt, 'popReceipt');
390
        Validate::notNullOrEmpty($popReceipt, 'popReceipt');
391
        
392
        $method      = Resources::HTTP_DELETE;
393
        $headers     = array();
394
        $postParams  = array();
395
        $queryParams = array();
396
        $path        = $queueName . '/messages/' . $messageId;
397
        $body        = Resources::EMPTY_STRING;
398
        
399
        if (is_null($options)) {
400
            $options = new QueueServiceOptions();
401
        }
402
        
403
        $this->addOptionalQueryParam(
404
            $queryParams,
405
            Resources::QP_POPRECEIPT,
406
            $popReceipt
407
        );
408
        
409
        $options->setLocationMode(LocationMode::PRIMARY_ONLY);
410
411
        return $this->sendAsync(
412
            $method,
413
            $headers,
414
            $queryParams,
415
            $postParams,
416
            $path,
417
            Resources::STATUS_NO_CONTENT,
418
            $body,
419
            $options
420
        );
421
    }
422
423
    /**
424
     * Deletes a queue.
425
     *
426
     * @param string              $queueName The queue name.
427
     * @param QueueServiceOptions $options   The optional parameters.
428
     *
429
     * @return void
430
     */
431
    public function deleteQueue($queueName, QueueServiceOptions $options = null)
432
    {
433
        $this->deleteQueueAsync($queueName, $options)->wait();
434
    }
435
436
    /**
437
     * Creates promise to delete a queue.
438
     *
439
     * @param string              $queueName The queue name.
440
     * @param QueueServiceOptions $options   The optional parameters.
441
     *
442
     * @return \GuzzleHttp\Promise\PromiseInterface
443
     */
444 View Code Duplication
    public function deleteQueueAsync(
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...
445
        $queueName,
446
        QueueServiceOptions $options = null
447
    ) {
448
        Validate::isString($queueName, 'queueName');
449
        Validate::notNullOrEmpty($queueName, 'queueName');
450
        
451
        $method      = Resources::HTTP_DELETE;
452
        $headers     = array();
453
        $postParams  = array();
454
        $queryParams = array();
455
        $path        = $queueName;
456
        
457
        if (is_null($options)) {
458
            $options = new QueueServiceOptions();
459
        }
460
        
461
        $options->setLocationMode(LocationMode::PRIMARY_ONLY);
462
463
        return $this->sendAsync(
464
            $method,
465
            $headers,
466
            $queryParams,
467
            $postParams,
468
            $path,
469
            Resources::STATUS_NO_CONTENT,
470
            Resources::EMPTY_STRING,
471
            $options
472
        );
473
    }
474
475
    /**
476
     * Returns queue properties, including user-defined metadata.
477
     *
478
     * @param string              $queueName The queue name.
479
     * @param QueueServiceOptions $options   The optional parameters.
480
     *
481
     * @return Models\GetQueueMetadataResult
482
     */
483
    public function getQueueMetadata($queueName, QueueServiceOptions $options = null)
484
    {
485
        return $this->getQueueMetadataAsync($queueName, $options)->wait();
486
    }
487
488
    /**
489
     * Creates promise to return queue properties, including user-defined metadata.
490
     *
491
     * @param string              $queueName The queue name.
492
     * @param QueueServiceOptions $options   The optional parameters.
493
     *
494
     * @return \GuzzleHttp\Promise\PromiseInterface
495
     */
496
    public function getQueueMetadataAsync(
497
        $queueName,
498
        QueueServiceOptions $options = null
499
    ) {
500
        Validate::isString($queueName, 'queueName');
501
        Validate::notNullOrEmpty($queueName, 'queueName');
502
        
503
        $method      = Resources::HTTP_GET;
504
        $headers     = array();
505
        $postParams  = array();
506
        $queryParams = array();
507
        $path        = $queueName;
508
        $body        = Resources::EMPTY_STRING;
509
        
510
        if (is_null($options)) {
511
            $options = new QueueServiceOptions();
512
        }
513
        
514
        $this->addOptionalQueryParam($queryParams, Resources::QP_COMP, 'metadata');
515
        
516
        return $this->sendAsync(
517
            $method,
518
            $headers,
519
            $queryParams,
520
            $postParams,
521
            $path,
522
            Resources::STATUS_OK,
523
            $body,
524
            $options
525
        )->then(function ($response) {
526
            $responseHeaders = HttpFormatter::formatHeaders($response->getHeaders());
527
            $metadata = Utilities::getMetadataArray($responseHeaders);
528
            $maxCount = intval(
529
                Utilities::tryGetValue(
530
                    $responseHeaders,
531
                    Resources::X_MS_APPROXIMATE_MESSAGES_COUNT
532
                )
533
            );
534
        
535
            return new GetQueueMetadataResult($maxCount, $metadata);
536
        }, null);
537
    }
538
539
    /**
540
     * Lists all messages in the queue.
541
     *
542
     * @param string              $queueName The queue name.
543
     * @param ListMessagesOptions $options   The optional parameters.
544
     *
545
     * @return Models\ListMessagesResult
546
     */
547
    public function listMessages($queueName, ListMessagesOptions $options = null)
548
    {
549
        return $this->listMessagesAsync($queueName, $options)->wait();
550
    }
551
552
    /**
553
     * Creates promise to list all messages in the queue.
554
     *
555
     * @param string              $queueName The queue name.
556
     * @param ListMessagesOptions $options   The optional parameters.
557
     *
558
     * @return \GuzzleHttp\Promise\PromiseInterface
559
     */
560
    public function listMessagesAsync(
561
        $queueName,
562
        ListMessagesOptions $options = null
563
    ) {
564
        Validate::isString($queueName, 'queueName');
565
        Validate::notNullOrEmpty($queueName, 'queueName');
566
        
567
        $method      = Resources::HTTP_GET;
568
        $headers     = array();
569
        $queryParams = array();
570
        $postParams  = array();
571
        $path        = $queueName . '/messages';
572
        
573
        if (is_null($options)) {
574
            $options = new ListMessagesOptions();
575
        }
576
        
577
        $messagesCount = $options->getNumberOfMessages();
578
        $visibility    = $options->getVisibilityTimeoutInSeconds();
579
        
580
        $this->addOptionalQueryParam(
581
            $queryParams,
582
            Resources::QP_NUM_OF_MESSAGES,
583
            $messagesCount
584
        );
585
        $this->addOptionalQueryParam(
586
            $queryParams,
587
            Resources::QP_VISIBILITY_TIMEOUT,
588
            $visibility
589
        );
590
591
        $dataSerializer = $this->dataSerializer;
592
        
593
        $options->setLocationMode(LocationMode::PRIMARY_ONLY);
594
595
        return $this->sendAsync(
596
            $method,
597
            $headers,
598
            $queryParams,
599
            $postParams,
600
            $path,
601
            Resources::STATUS_OK,
602
            Resources::EMPTY_STRING,
603
            $options
604
        )->then(function ($response) use ($dataSerializer) {
605
            $parsed = $dataSerializer->unserialize($response->getBody());
606
            return ListMessagesResult::create($parsed);
607
        }, null);
608
    }
609
610
    /**
611
     * Retrieves a message from the front of the queue, without changing
612
     * the message visibility.
613
     *
614
     * @param string              $queueName The queue name.
615
     * @param PeekMessagesOptions $options   The optional parameters.
616
     *
617
     * @return Models\PeekMessagesResult
618
     */
619
    public function peekMessages($queueName, PeekMessagesOptions $options = null)
620
    {
621
        return $this->peekMessagesAsync($queueName, $options)->wait();
622
    }
623
624
    /**
625
     * Creates promise to retrieve a message from the front of the queue,
626
     * without changing the message visibility.
627
     *
628
     * @param string              $queueName The queue name.
629
     * @param PeekMessagesOptions $options   The optional parameters.
630
     *
631
     * @return \GuzzleHttp\Promise\PromiseInterface
632
     */
633
    public function peekMessagesAsync(
634
        $queueName,
635
        PeekMessagesOptions $options = null
636
    ) {
637
        Validate::isString($queueName, 'queueName');
638
        Validate::notNullOrEmpty($queueName, 'queueName');
639
        
640
        $method      = Resources::HTTP_GET;
641
        $headers     = array();
642
        $queryParams = array();
643
        $postParams  = array();
644
        $path        = $queueName . '/messages';
645
        
646
        if (is_null($options)) {
647
            $options = new PeekMessagesOptions();
648
        }
649
        
650
        $messagesCount = $options->getNumberOfMessages();
651
        
652
        $this->addOptionalQueryParam($queryParams, Resources::QP_PEEK_ONLY, 'true');
653
        $this->addOptionalQueryParam(
654
            $queryParams,
655
            Resources::QP_NUM_OF_MESSAGES,
656
            $messagesCount
657
        );
658
        
659
        $dataSerializer = $this->dataSerializer;
660
661
        return $this->sendAsync(
662
            $method,
663
            $headers,
664
            $queryParams,
665
            $postParams,
666
            $path,
667
            Resources::STATUS_OK,
668
            Resources::EMPTY_STRING,
669
            $options
670
        )->then(function ($response) use ($dataSerializer) {
671
            $parsed = $dataSerializer->unserialize($response->getBody());
672
            return PeekMessagesResult::create($parsed);
673
        }, null);
674
    }
675
676
    /**
677
     * Sets user-defined metadata on the queue. To delete queue metadata, call
678
     * this API without specifying any metadata in $metadata.
679
     *
680
     * @param string              $queueName The queue name.
681
     * @param array               $metadata  The metadata array.
682
     * @param QueueServiceOptions $options   The optional parameters.
683
     *
684
     * @return void
685
     */
686
    public function setQueueMetadata(
687
        $queueName,
688
        array $metadata = null,
689
        QueueServiceOptions $options = null
690
    ) {
691
        $this->setQueueMetadataAsync($queueName, $metadata, $options)->wait();
692
    }
693
694
    /**
695
     * Creates promise to set user-defined metadata on the queue. To delete
696
     * queue metadata, call this API without specifying any metadata in $metadata.
697
     *
698
     * @param string              $queueName The queue name.
699
     * @param array               $metadata  The metadata array.
700
     * @param QueueServiceOptions $options   The optional parameters.
701
     *
702
     * @return \GuzzleHttp\Promise\PromiseInterface
703
     */
704
    public function setQueueMetadataAsync(
705
        $queueName,
706
        array $metadata = null,
707
        QueueServiceOptions $options = null
708
    ) {
709
        Validate::isString($queueName, 'queueName');
710
        Validate::notNullOrEmpty($queueName, 'queueName');
711
        Utilities::validateMetadata($metadata);
712
        
713
        $method      = Resources::HTTP_PUT;
714
        $headers     = array();
0 ignored issues
show
Unused Code introduced by
$headers 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...
715
        $queryParams = array();
716
        $postParams  = array();
717
        $path        = $queueName;
718
        $body        = Resources::EMPTY_STRING;
719
        
720
        if (is_null($options)) {
721
            $options = new QueueServiceOptions();
722
        }
723
        
724
        $this->addOptionalQueryParam($queryParams, Resources::QP_COMP, 'metadata');
725
        
726
        $metadataHeaders = $this->generateMetadataHeaders($metadata);
727
        $headers         = $metadataHeaders;
728
        
729
        $options->setLocationMode(LocationMode::PRIMARY_ONLY);
730
731
        return $this->sendAsync(
732
            $method,
733
            $headers,
734
            $queryParams,
735
            $postParams,
736
            $path,
737
            Resources::STATUS_NO_CONTENT,
738
            $body,
739
            $options
740
        );
741
    }
742
743
    /**
744
     * Updates the visibility timeout of a message and/or the message contents.
745
     *
746
     * @param string              $queueName                  The queue name.
747
     * @param string              $messageId                  The id of the message.
748
     * @param string              $popReceipt                 The valid pop receipt
749
     * value returned from an earlier call to the Get Messages or Update Message
750
     * operation.
751
     * @param string              $messageText                The message contents.
752
     * @param int                 $visibilityTimeoutInSeconds Specifies the new
753
     * visibility timeout value, in seconds, relative to server time.
754
     * The new value must be larger than or equal to 0, and cannot be larger
755
     * than 7 days. The visibility timeout of a message cannot be set to a value
756
     * later than the expiry time. A message can be updated until it has been
757
     * deleted or has expired.
758
     * @param QueueServiceOptions $options                    The optional
759
     * parameters.
760
     *
761
     * @return Models\UpdateMessageResult
762
     */
763
    public function updateMessage(
764
        $queueName,
765
        $messageId,
766
        $popReceipt,
767
        $messageText,
768
        $visibilityTimeoutInSeconds,
769
        QueueServiceOptions $options = null
770
    ) {
771
        return $this->updateMessageAsync(
772
            $queueName,
773
            $messageId,
774
            $popReceipt,
775
            $messageText,
776
            $visibilityTimeoutInSeconds,
777
            $options
778
        )->wait();
779
    }
780
781
    /**
782
     * Creates promise to update the visibility timeout of a message and/or the
783
     * message contents.
784
     *
785
     * @param string              $queueName                  The queue name.
786
     * @param string              $messageId                  The id of the message.
787
     * @param string              $popReceipt                 The valid pop receipt
788
     * value returned from an earlier call to the Get Messages or Update Message
789
     * operation.
790
     * @param string              $messageText                The message contents.
791
     * @param int                 $visibilityTimeoutInSeconds Specifies the new
792
     * visibility timeout value, in seconds, relative to server time.
793
     * The new value must be larger than or equal to 0, and cannot be larger
794
     * than 7 days. The visibility timeout of a message cannot be set to a value
795
     * later than the expiry time. A message can be updated until it has been
796
     * deleted or has expired.
797
     * @param QueueServiceOptions $options                    The optional
798
     * parameters.
799
     *
800
     * @return \GuzzleHttp\Promise\PromiseInterface
801
     */
802
    public function updateMessageAsync(
803
        $queueName,
804
        $messageId,
805
        $popReceipt,
806
        $messageText,
807
        $visibilityTimeoutInSeconds,
808
        QueueServiceOptions $options = null
809
    ) {
810
        Validate::isString($queueName, 'queueName');
811
        Validate::notNullOrEmpty($queueName, 'queueName');
812
        Validate::isString($messageId, 'messageId');
813
        Validate::notNullOrEmpty($messageId, 'messageId');
814
        Validate::isString($popReceipt, 'popReceipt');
815
        Validate::notNullOrEmpty($popReceipt, 'popReceipt');
816
        Validate::isString($messageText, 'messageText');
817
        Validate::isInteger(
818
            $visibilityTimeoutInSeconds,
819
            'visibilityTimeoutInSeconds'
820
        );
821
        Validate::notNull(
822
            $visibilityTimeoutInSeconds,
823
            'visibilityTimeoutInSeconds'
824
        );
825
        
826
        $method      = Resources::HTTP_PUT;
827
        $headers     = array();
828
        $postParams  = array();
829
        $queryParams = array();
830
        $path        = $queueName . '/messages' . '/' . $messageId;
831
        $body        = Resources::EMPTY_STRING;
832
        
833
        if (is_null($options)) {
834
            $options = new QueueServiceOptions();
835
        }
836
        
837
        $this->addOptionalQueryParam(
838
            $queryParams,
839
            Resources::QP_VISIBILITY_TIMEOUT,
840
            $visibilityTimeoutInSeconds
841
        );
842
        $this->addOptionalQueryParam(
843
            $queryParams,
844
            Resources::QP_POPRECEIPT,
845
            $popReceipt
846
        );
847
        
848
        if (!empty($messageText)) {
849
            $this->addOptionalHeader(
850
                $headers,
851
                Resources::CONTENT_TYPE,
852
                Resources::URL_ENCODED_CONTENT_TYPE
853
            );
854
        
855
            $message = new QueueMessage();
856
            $message->setMessageText($messageText);
857
            $body = $message->toXml($this->dataSerializer);
0 ignored issues
show
Compatibility introduced by
$this->dataSerializer of type object<MicrosoftAzure\St...ialization\ISerializer> is not a sub-type of object<MicrosoftAzure\St...lization\XmlSerializer>. It seems like you assume a concrete implementation of the interface MicrosoftAzure\Storage\C...rialization\ISerializer to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
858
        }
859
        
860
        $options->setLocationMode(LocationMode::PRIMARY_ONLY);
861
862
        return $this->sendAsync(
863
            $method,
864
            $headers,
865
            $queryParams,
866
            $postParams,
867
            $path,
868
            Resources::STATUS_NO_CONTENT,
869
            $body,
870
            $options
871
        )->then(function ($response) {
872
            $responseHeaders = HttpFormatter::formatHeaders($response->getHeaders());
873
            return UpdateMessageResult::create($responseHeaders);
874
        }, null);
875
    }
876
877
    /**
878
     * Gets the access control list (ACL)
879
     *
880
     * @param string                     $queue   The queue name.
881
     * @param Models\QueueServiceOptions $options The optional parameters.
882
     *
883
     * @return Models\QueueACL
884
     *
885
     * @see https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/get-queue-acl
886
     */
887
    public function getQueueAcl(
888
        $queue,
889
        Models\QueueServiceOptions $options = null
890
    ) {
891
        return $this->getQueueAclAsync($queue, $options)->wait();
892
    }
893
894
    /**
895
     * Creates the promise to gets the access control list (ACL)
896
     *
897
     * @param string                     $queue   The queue name.
898
     * @param Models\QueueServiceOptions $options The optional parameters.
899
     *
900
     * @return \GuzzleHttp\Promise\PromiseInterface
901
     *
902
     * @see https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/get-queue-acl
903
     */
904 View Code Duplication
    public function getQueueAclAsync(
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...
905
        $queue,
906
        Models\QueueServiceOptions $options = null
907
    ) {
908
        Validate::isString($queue, 'queue');
909
        
910
        $method      = Resources::HTTP_GET;
911
        $headers     = array();
912
        $postParams  = array();
913
        $queryParams = array();
914
        $statusCode  = Resources::STATUS_OK;
0 ignored issues
show
Unused Code introduced by
$statusCode 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...
915
        $path        = $queue;
916
        
917
        if (is_null($options)) {
918
            $options = new QueueServiceOptions();
919
        }
920
        
921
        $this->addOptionalQueryParam(
922
            $queryParams,
923
            Resources::QP_COMP,
924
            'acl'
925
        );
926
927
        $dataSerializer = $this->dataSerializer;
928
        
929
        $promise = $this->sendAsync(
930
            $method,
931
            $headers,
932
            $queryParams,
933
            $postParams,
934
            $path,
935
            Resources::STATUS_OK,
936
            Resources::EMPTY_STRING,
937
            $options
938
        );
939
940
        return $promise->then(function ($response) use ($dataSerializer) {
941
            $parsed       = $dataSerializer->unserialize($response->getBody());
942
            return QueueACL::create($parsed);
943
        }, null);
944
    }
945
    
946
    /**
947
     * Sets the ACL.
948
     *
949
     * @param string                     $queue   name
950
     * @param Models\QueueACL            $acl     access control list for Queue
951
     * @param Models\QueueServiceOptions $options optional parameters
952
     *
953
     * @return void
954
     *
955
     * @see https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/set-queue-acl
956
     */
957
    public function setQueueAcl(
958
        $queue,
959
        Models\QueueACL $acl,
960
        Models\QueueServiceOptions $options = null
961
    ) {
962
        $this->setQueueAclAsync($queue, $acl, $options)->wait();
963
    }
964
965
    /**
966
     * Creates promise to set the ACL
967
     *
968
     * @param string                     $queue   name
969
     * @param Models\QueueACL            $acl     access control list for Queue
970
     * @param Models\QueueServiceOptions $options optional parameters
971
     *
972
     * @return \GuzzleHttp\Promise\PromiseInterface
973
     *
974
     * @see https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/set-queue-acl
975
     */
976 View Code Duplication
    public function setQueueAclAsync(
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...
977
        $queue,
978
        Models\QueueACL $acl,
979
        Models\QueueServiceOptions $options = null
980
    ) {
981
        Validate::isString($queue, 'queue');
982
        Validate::notNullOrEmpty($acl, 'acl');
983
        
984
        $method      = Resources::HTTP_PUT;
985
        $headers     = array();
986
        $postParams  = array();
987
        $queryParams = array();
988
        $body        = $acl->toXml($this->dataSerializer);
0 ignored issues
show
Compatibility introduced by
$this->dataSerializer of type object<MicrosoftAzure\St...ialization\ISerializer> is not a sub-type of object<MicrosoftAzure\St...lization\XmlSerializer>. It seems like you assume a concrete implementation of the interface MicrosoftAzure\Storage\C...rialization\ISerializer to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
989
        $path        = $queue;
990
        
991
        if (is_null($options)) {
992
            $options = new QueueServiceOptions();
993
        }
994
        
995
        $this->addOptionalQueryParam(
996
            $queryParams,
997
            Resources::QP_COMP,
998
            'acl'
999
        );
1000
1001
        $options->setLocationMode(LocationMode::PRIMARY_ONLY);
1002
        
1003
        return $this->sendAsync(
1004
            $method,
1005
            $headers,
1006
            $queryParams,
1007
            $postParams,
1008
            $path,
1009
            Resources::STATUS_NO_CONTENT,
1010
            $body,
0 ignored issues
show
Security Bug introduced by
It seems like $body defined by $acl->toXml($this->dataSerializer) on line 988 can also be of type false; however, MicrosoftAzure\Storage\C...eRestProxy::sendAsync() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
1011
            $options
1012
        );
1013
    }
1014
}
1015