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
Push — master ( 967239...06466a )
by Márk
02:52
created

Driver::createQueue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0625

Importance

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