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 ( 1144b4...828920 )
by Márk
07:09 queued 04:40
created

Driver   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 218
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 23
lcom 1
cbo 0
dl 0
loc 218
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A listQueues() 0 16 3
A createQueue() 0 10 2
A countMessages() 0 11 1
A pushMessage() 0 9 1
B popMessage() 0 23 4
A acknowledgeMessage() 0 11 2
A peekQueue() 0 20 2
A removeQueue() 0 16 2
A info() 0 4 1
A getQueueDirectory() 0 4 1
B getJobFilename() 0 28 3
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
    public function __construct($baseDirectory, $permissions = 0740)
22
    {
23
        $this->baseDirectory = $baseDirectory;
24
        $this->permissions = $permissions;
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function listQueues()
31
    {
32
        $it = new \FilesystemIterator($this->baseDirectory, \FilesystemIterator::SKIP_DOTS);
33
34
        $queues = [];
35
36
        foreach ($it as $file) {
37
            if (!$file->isDir()) {
38
                continue;
39
            }
40
41
            array_push($queues, $file->getBasename());
42
        }
43
44
        return $queues;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function createQueue($queueName)
51
    {
52
        $queueDir = $this->getQueueDirectory($queueName);
53
54
        if (is_dir($queueDir)) {
55
            return;
56
        }
57
58
        mkdir($queueDir, 0755, true);
59
    }
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
    public function pushMessage($queueName, $message)
80
    {
81
        $queueDir = $this->getQueueDirectory($queueName);
82
83
        $filename = $this->getJobFilename($queueName);
84
85
        file_put_contents($queueDir.DIRECTORY_SEPARATOR.$filename, $message);
86
        chmod($queueDir . DIRECTORY_SEPARATOR . $filename, $this->permissions);
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function popMessage($queueName, $duration = 5)
93
    {
94
        $runtime = microtime(true) + $duration;
95
        $queueDir = $this->getQueueDirectory($queueName);
96
97
        $it = new \GlobIterator($queueDir.DIRECTORY_SEPARATOR.'*.job', \FilesystemIterator::KEY_AS_FILENAME);
98
        $files = array_keys(iterator_to_array($it));
99
100
        natsort($files);
101
102
        while (microtime(true) < $runtime) {
103
            if ($files) {
104
                $id = array_pop($files);
105
                if (@rename($queueDir.DIRECTORY_SEPARATOR.$id, $queueDir.DIRECTORY_SEPARATOR.$id.'.proceed')) {
106
                    return array(file_get_contents($queueDir.DIRECTORY_SEPARATOR.$id.'.proceed'), $id);
107
                }
108
            }
109
110
            usleep(1000);
111
        }
112
113
        return array(null, null);
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119
    public function acknowledgeMessage($queueName, $receipt)
120
    {
121
        $queueDir = $this->getQueueDirectory($queueName);
122
        $path = $queueDir.DIRECTORY_SEPARATOR.$receipt.'.proceed';
123
124
        if (!is_file($path)) {
125
            return;
126
        }
127
128
        unlink($path);
129
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134
    public function peekQueue($queueName, $index = 0, $limit = 20)
135
    {
136
        $queueDir = $this->getQueueDirectory($queueName);
137
138
        $it = new \GlobIterator($queueDir.DIRECTORY_SEPARATOR.'*.job', \FilesystemIterator::KEY_AS_FILENAME);
139
        $files = array_keys(iterator_to_array($it));
140
141
        natsort($files);
142
        $files = array_reverse($files);
143
144
        $files = array_slice($files, $index, $limit);
145
146
        $messages = [];
147
148
        foreach ($files as $file) {
149
            array_push($messages, file_get_contents($queueDir.DIRECTORY_SEPARATOR.$file));
150
        }
151
152
        return $messages;
153
    }
154
155
    /**
156
     * {@inheritdoc}
157
     */
158
    public function removeQueue($queueName)
159
    {
160
        $iterator = new \RecursiveDirectoryIterator(
161
            $this->getQueueDirectory($queueName),
162
            \FilesystemIterator::SKIP_DOTS
163
        );
164
        $iterator = new \RecursiveIteratorIterator($iterator);
165
        $iterator = new \RegexIterator($iterator, '#\.job(.proceed)?$#');
166
167
        foreach ($iterator as $file) {
168
            /* @var $file \DirectoryIterator */
169
            unlink($file->getRealPath());
170
        }
171
172
        rmdir($this->getQueueDirectory($queueName));
173
    }
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
    private function getQueueDirectory($queueName)
189
    {
190
        return $this->baseDirectory.DIRECTORY_SEPARATOR.str_replace(array('\\', '.'), '-', $queueName);
191
    }
192
193
    /**
194
     * Generates a uuid.
195
     *
196
     * @param string $queueName
197
     *
198
     * @return string
199
     */
200
    private function getJobFilename($queueName)
201
    {
202
        $path = $this->baseDirectory.'/bernard.meta';
203
204
        if (!is_file($path)) {
205
            touch($path);
206
            chmod($path, $this->permissions);
207
        }
208
209
        $file = new \SplFileObject($path, 'r+');
210
        $file->flock(LOCK_EX);
211
212
        $meta = unserialize($file->fgets());
213
214
        $id = isset($meta[$queueName]) ? $meta[$queueName] : 0;
215
        $id++;
216
217
        $filename = sprintf('%d.job', $id);
218
        $meta[$queueName] = $id;
219
220
        $content = serialize($meta);
221
222
        $file->fseek(0);
223
        $file->fwrite($content, strlen($content));
224
        $file->flock(LOCK_UN);
225
226
        return $filename;
227
    }
228
}
229