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 (#393)
by
unknown
06:35
created

Driver::popMessage()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 11.7224

Importance

Changes 0
Metric Value
dl 0
loc 32
ccs 11
cts 24
cp 0.4583
rs 8.7857
c 0
b 0
f 0
cc 6
nc 6
nop 2
crap 11.7224
1
<?php
2
3
namespace Bernard\Driver\FlatFile;
4
5
/**
6
 * Flat file driver to provide a simple job queue without any
7
 * database.
8
 *
9
 * @author Markus Bachmann <[email protected]>
10
 */
11
class Driver implements \Bernard\Driver
12
{
13
    private $baseDirectory;
14
15
    private $permissions;
16
17
    /**
18
     * @param string $baseDirectory The base directory
19
     * @param int    $permissions   permissions to create the file with
20
     */
21 9
    public function __construct($baseDirectory, $permissions = 0740)
22
    {
23 9
        $this->baseDirectory = $baseDirectory;
24 9
        $this->permissions = $permissions;
25 9
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30 1
    public function listQueues()
31
    {
32 1
        $it = new \FilesystemIterator($this->baseDirectory, \FilesystemIterator::SKIP_DOTS);
33
34 1
        $queues = [];
35
36 1
        foreach ($it as $file) {
37 1
            if (!$file->isDir()) {
38 1
                continue;
39
            }
40
41 1
            array_push($queues, $file->getBasename());
42 1
        }
43
44 1
        return $queues;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 9
    public function createQueue($queueName)
51
    {
52 9
        $queueDir = $this->getQueueDirectory($queueName);
53
54 9
        if (is_dir($queueDir)) {
55 1
            return;
56
        }
57
58 9
        mkdir($queueDir, 0755, true);
59 9
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function countMessages($queueName)
65
    {
66
        $iterator = new \RecursiveDirectoryIterator(
67
            $this->getQueueDirectory($queueName),
68
            \FilesystemIterator::SKIP_DOTS
69
        );
70
        $iterator = new \RecursiveIteratorIterator($iterator);
71
        $iterator = new \RegexIterator($iterator, '#\.job$#');
72
73
        return iterator_count($iterator);
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 8
    public function pushMessage($queueName, $message)
80
    {
81 8
        $queueDir = $this->getQueueDirectory($queueName);
82
83 8
        $filename = $this->getJobFilename($queueName);
84
85 8
        file_put_contents($queueDir.DIRECTORY_SEPARATOR.$filename, $message);
86 8
        chmod($queueDir.DIRECTORY_SEPARATOR.$filename, $this->permissions);
87 8
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92 3
    public function popMessage($queueName, $duration = 5)
93
    {
94 3
        $runtime = microtime(true) + $duration;
95 3
        $queueDir = $this->getQueueDirectory($queueName);
96
97 3
        $it = new \GlobIterator($queueDir.DIRECTORY_SEPARATOR.'*.job', \FilesystemIterator::KEY_AS_FILENAME);
98 3
        $files = array_keys(iterator_to_array($it));
99
100 3
        natsort($files);
101
102 3
        while (microtime(true) < $runtime) {
103 3
            if ($files) {
104 3
                $id = array_pop($files);
105 3
                if (@rename($queueDir.DIRECTORY_SEPARATOR.$id, $queueDir.DIRECTORY_SEPARATOR.$id.'.proceed')) {
106 3
                    return [file_get_contents($queueDir.DIRECTORY_SEPARATOR.$id.'.proceed'), $id];
107
                }
108
109
                return $this->processFileOrFail($queueDir, $id);
110
            }
111
112
            $nano = time_nanosleep(0, 1000000);
113
            if ($nano === false) {
114
                // fallback when time_nanosleep fails
115
                usleep(1000);
116
            } elseif (is_array($nano)) {
117
                // Interrupted by a pcntl_signal
118
                return array(null, null);
119
            }
120
        }
121
122
        return [null, null];
123
    }
124
125
    /**
126
     * @param string $queueDir
127
     * @param string $id
128
     * 
129
     * @return array
130
     */
131
    private function processFileOrFail($queueDir, $id) {
132
        $name = $queueDir.DIRECTORY_SEPARATOR.$id;
133
        $newName = $name.'.proceed';
134
135
        if (!@rename($name, $newName)) {
136
            throw new InsufficientPermissionsException('Unable to process file: '.$name);
137
        }
138
139
        return [file_get_contents($newName), $id];
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145 1
    public function acknowledgeMessage($queueName, $receipt)
146
    {
147 1
        $queueDir = $this->getQueueDirectory($queueName);
148 1
        $path = $queueDir.DIRECTORY_SEPARATOR.$receipt.'.proceed';
149
150 1
        if (!is_file($path)) {
151
            return;
152
        }
153
154 1
        unlink($path);
155 1
    }
156
157
    /**
158
     * {@inheritdoc}
159
     */
160 1
    public function peekQueue($queueName, $index = 0, $limit = 20)
161
    {
162 1
        $queueDir = $this->getQueueDirectory($queueName);
163
164 1
        $it = new \GlobIterator($queueDir.DIRECTORY_SEPARATOR.'*.job', \FilesystemIterator::KEY_AS_FILENAME);
165 1
        $files = array_keys(iterator_to_array($it));
166
167 1
        natsort($files);
168 1
        $files = array_reverse($files);
169
170 1
        $files = array_slice($files, $index, $limit);
171
172 1
        $messages = [];
173
174 1
        foreach ($files as $file) {
175 1
            array_push($messages, file_get_contents($queueDir.DIRECTORY_SEPARATOR.$file));
176 1
        }
177
178 1
        return $messages;
179
    }
180
181
    /**
182
     * {@inheritdoc}
183
     */
184 2
    public function removeQueue($queueName)
185
    {
186 2
        $iterator = new \RecursiveDirectoryIterator(
187 2
            $this->getQueueDirectory($queueName),
188
            \FilesystemIterator::SKIP_DOTS
189 2
        );
190 2
        $iterator = new \RecursiveIteratorIterator($iterator);
191 2
        $iterator = new \RegexIterator($iterator, '#\.job(.proceed)?$#');
192
193 2
        foreach ($iterator as $file) {
194
            /* @var $file \DirectoryIterator */
195 2
            unlink($file->getRealPath());
196 2
        }
197
198 2
        rmdir($this->getQueueDirectory($queueName));
199 2
    }
200
201
    /**
202
     * {@inheritdoc}
203
     */
204
    public function info()
205
    {
206
        return [];
207
    }
208
209
    /**
210
     * @param string $queueName
211
     *
212
     * @return string
213
     */
214 9
    private function getQueueDirectory($queueName)
215
    {
216 9
        return $this->baseDirectory.DIRECTORY_SEPARATOR.str_replace(['\\', '.'], '-', $queueName);
217
    }
218
219
    /**
220
     * Generates a uuid.
221
     *
222
     * @param string $queueName
223
     *
224
     * @return string
225
     */
226 8
    private function getJobFilename($queueName)
227
    {
228 8
        $path = $this->baseDirectory.'/bernard.meta';
229
230 8
        if (!is_file($path)) {
231 8
            touch($path);
232 8
            chmod($path, $this->permissions);
233 8
        }
234
235 8
        $file = new \SplFileObject($path, 'r+');
236 8
        $file->flock(LOCK_EX);
237
238 8
        $meta = unserialize($file->fgets());
239
240 8
        $id = isset($meta[$queueName]) ? $meta[$queueName] : 0;
241 8
        ++$id;
242
243 8
        $filename = sprintf('%d.job', $id);
244 8
        $meta[$queueName] = $id;
245
246 8
        $content = serialize($meta);
247
248 8
        $file->fseek(0);
249 8
        $file->fwrite($content, strlen($content));
250 8
        $file->flock(LOCK_UN);
251
252 8
        return $filename;
253
    }
254
}
255