GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#206)
by Jeroen
02:07
created

IronMqDriver::configurePushOptions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 19
rs 9.4285
cc 2
eloc 11
nc 2
nop 1
1
<?php
2
3
namespace Bernard\Driver;
4
5
use IronMQ;
6
use Symfony\Component\OptionsResolver\OptionsResolver;
7
8
/**
9
 * Implements a Driver for use with Iron MQ:
10
 * http://dev.iron.io/mq/reference/api/
11
 *
12
 * @package Bernard
13
 */
14
class IronMqDriver extends AbstractPrefetchDriver
15
{
16
    protected $ironmq;
17
18
    /**
19
     * @param IronMQ   $ironmq
20
     * @param int|null $prefetch
21
     */
22
    public function __construct(IronMQ $ironmq, $prefetch = null)
23
    {
24
        parent::__construct($prefetch);
25
26
        $this->ironmq = $ironmq;
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function listQueues()
33
    {
34
        $queueNames = array();
35
        $page = 0;
36
37
        while ($queues = $this->ironmq->getQueues($page, 100)) {
38
            $queueNames += $this->pluck($queues, 'name');
39
40
            // If we get 100 results the probability of another page is high.
41
            if (count($queues) < 100) {
42
                break;
43
            }
44
45
            $page++;
46
        }
47
48
        return $queueNames;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function createQueue($queueName, array $options = [])
55
    {
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function countMessages($queueName)
62
    {
63
        if ($info = $this->ironmq->getQueue($queueName)) {
64
            return $info->size;
65
        }
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function pushMessage($queueName, $message, array $options = [])
72
    {
73
        $options = $this->validatePushOptions($options);
74
75
        $this->ironmq->postMessage($queueName, $message, $options);
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function popMessage($queueName, $duration = 5)
82
    {
83
        if ($message = $this->cache->pop($queueName)) {
84
            return $message;
85
        }
86
87
        $timeout = IronMQ::GET_MESSAGE_TIMEOUT;
88
89
        $messages = $this->ironmq->getMessages($queueName, $this->prefetch, $timeout, $duration);
90
91
        if (!$messages) {
92
            return array(null, null);
93
        }
94
95
        foreach ($messages as $message) {
96
            $this->cache->push($queueName, array($message->body, $message->id));
97
        }
98
99
        return $this->cache->pop($queueName);
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105
    public function acknowledgeMessage($queueName, $receipt)
106
    {
107
        $this->ironmq->deleteMessage($queueName, $receipt);
108
    }
109
110
    /**
111
     * IronMQ does not support an offset when peeking messages.
112
     *
113
     * {@inheritdoc}
114
     */
115
    public function peekQueue($queueName, $index = 0, $limit = 20)
116
    {
117
        if ($messages = $this->ironmq->peekMessages($queueName, $limit)) {
118
            return $this->pluck($messages, 'body');
119
        }
120
121
        return [];
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127
    public function removeQueue($queueName)
128
    {
129
        $this->ironmq->deleteQueue($queueName);
130
    }
131
132
    /**
133
     * {@inheritdoc}
134
     */
135
    public function info()
136
    {
137
        return [
138
            'prefetch' => $this->prefetch,
139
        ];
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145
    public function configurePushOptions(OptionsResolver $resolver)
146
    {
147
        //BC layer to support 2.3+ and 2.7+/3.0+ versions
148
        if (interface_exists('Symfony\Component\OptionsResolver\OptionsResolverInterface')) {
149
            //2.3+
150
            $resolver->setOptional(array(
0 ignored issues
show
Bug introduced by
The method setOptional() does not seem to exist on object<Symfony\Component...solver\OptionsResolver>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
151
                'timeout',
152
                'delay',
153
                'expires_in'
154
            ));
155
        } else {
156
            //2.7+
157
            $resolver
158
                ->setDefined('timeout')
159
                ->setDefined('delay')
160
                ->setDefined('expires_in')
161
            ;
162
        }
163
    }
164
165
    /**
166
     * The missing array_pluck but for objects array
167
     *
168
     * @param array  $objects
169
     * @param string $property
170
     *
171
     * @return array
172
     */
173
    protected function pluck(array $objects, $property)
174
    {
175
        $function = function ($object) use ($property) {
176
            return $object->$property;
177
        };
178
179
        return array_map($function, $objects);
180
    }
181
}
182