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 (#383)
by
unknown
07:46
created

Driver::process()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.2559

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 3
cts 5
cp 0.6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 2
crap 2.2559
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->process($queueDir, $id);
110
            }
111
112
            usleep(1000);
113
        }
114
115
        return [null, null];
116
    }
117
118
    /**
119 1
     * @param string $queueDir
120
     * @param string $id
121 1
     * 
122 1
     * @return array
123
     */
124 1
    private function process($queueDir, $id) {
125
        $name = $queueDir.DIRECTORY_SEPARATOR.$id;
126
        $newName = $name.'.proceed';
127
128 1
        if (!@rename($name, $newName)) {
129 1
            throw new InsufficientPermissionsException('Unable to process file: '.$name);
130
        }
131
132
        return [file_get_contents($newName), $id];
133
    }
134 1
135
    /**
136 1
     * {@inheritdoc}
137
     */
138 1
    public function acknowledgeMessage($queueName, $receipt)
139 1
    {
140
        $queueDir = $this->getQueueDirectory($queueName);
141 1
        $path = $queueDir.DIRECTORY_SEPARATOR.$receipt.'.proceed';
142 1
143
        if (!is_file($path)) {
144 1
            return;
145
        }
146 1
147
        unlink($path);
148 1
    }
149 1
150 1
    /**
151
     * {@inheritdoc}
152 1
     */
153
    public function peekQueue($queueName, $index = 0, $limit = 20)
154
    {
155
        $queueDir = $this->getQueueDirectory($queueName);
156
157
        $it = new \GlobIterator($queueDir.DIRECTORY_SEPARATOR.'*.job', \FilesystemIterator::KEY_AS_FILENAME);
158 2
        $files = array_keys(iterator_to_array($it));
159
160 2
        natsort($files);
161 2
        $files = array_reverse($files);
162
163 2
        $files = array_slice($files, $index, $limit);
164 2
165 2
        $messages = [];
166
167 2
        foreach ($files as $file) {
168
            array_push($messages, file_get_contents($queueDir.DIRECTORY_SEPARATOR.$file));
169 2
        }
170 2
171
        return $messages;
172 2
    }
173 2
174
    /**
175
     * {@inheritdoc}
176
     */
177
    public function removeQueue($queueName)
178
    {
179
        $iterator = new \RecursiveDirectoryIterator(
180
            $this->getQueueDirectory($queueName),
181
            \FilesystemIterator::SKIP_DOTS
182
        );
183
        $iterator = new \RecursiveIteratorIterator($iterator);
184
        $iterator = new \RegexIterator($iterator, '#\.job(.proceed)?$#');
185
186
        foreach ($iterator as $file) {
187
            /* @var $file \DirectoryIterator */
188 9
            unlink($file->getRealPath());
189
        }
190 9
191
        rmdir($this->getQueueDirectory($queueName));
192
    }
193
194
    /**
195
     * {@inheritdoc}
196
     */
197
    public function info()
198
    {
199
        return [];
200 8
    }
201
202 8
    /**
203
     * @param string $queueName
204 8
     *
205 8
     * @return string
206 8
     */
207 8
    private function getQueueDirectory($queueName)
208
    {
209 8
        return $this->baseDirectory.DIRECTORY_SEPARATOR.str_replace(['\\', '.'], '-', $queueName);
210 8
    }
211
212 8
    /**
213
     * Generates a uuid.
214 8
     *
215 8
     * @param string $queueName
216
     *
217 8
     * @return string
218 8
     */
219
    private function getJobFilename($queueName)
220 8
    {
221
        $path = $this->baseDirectory.'/bernard.meta';
222 8
223 8
        if (!is_file($path)) {
224 8
            touch($path);
225
            chmod($path, $this->permissions);
226 8
        }
227
228
        $file = new \SplFileObject($path, 'r+');
229
        $file->flock(LOCK_EX);
230
231
        $meta = unserialize($file->fgets());
232
233
        $id = isset($meta[$queueName]) ? $meta[$queueName] : 0;
234
        ++$id;
235
236
        $filename = sprintf('%d.job', $id);
237
        $meta[$queueName] = $id;
238
239
        $content = serialize($meta);
240
241
        $file->fseek(0);
242
        $file->fwrite($content, strlen($content));
243
        $file->flock(LOCK_UN);
244
245
        return $filename;
246
    }
247
}
248