SqlitePdoQueue::supportsDriver()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of the Phive Queue package.
5
 *
6
 * (c) Eugene Leonovich <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Phive\Queue\Pdo;
13
14
use Phive\Queue\NoItemAvailableException;
15
16
class SqlitePdoQueue extends PdoQueue
17
{
18
    /**
19
     * {@inheritdoc}
20
     */
21 12
    public function pop()
22
    {
23 12
        $sql = sprintf(
24 12
            'SELECT id, item FROM %s WHERE eta <= %d ORDER BY eta LIMIT 1',
25 12
            $this->tableName,
26 12
            time()
27 12
        );
28
29 12
        $this->pdo->exec('BEGIN IMMEDIATE');
30
31
        try {
32 12
            $stmt = $this->pdo->query($sql);
33 11
            $row = $stmt->fetch(\PDO::FETCH_ASSOC);
34 11
            $stmt->closeCursor();
35
36 11
            if ($row) {
37 11
                $sql = sprintf('DELETE FROM %s WHERE id = %d', $this->tableName, $row['id']);
38 11
                $this->pdo->exec($sql);
39 11
            }
40
41 11
            $this->pdo->exec('COMMIT');
42 12
        } catch (\Exception $e) {
43 1
            $this->pdo->exec('ROLLBACK');
44 1
            throw $e;
45
        }
46
47 11
        if ($row) {
48 11
            return $row['item'];
49
        }
50
51 2
        throw new NoItemAvailableException($this);
52
    }
53
54 20
    protected function supportsDriver($driverName)
55
    {
56 20
        return 'sqlite' === $driverName;
57
    }
58
}
59