PdoQueue::push()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 8
cts 8
cp 1
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 2
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\Queue;
15
use Phive\Queue\QueueUtils;
16
17
abstract class PdoQueue implements Queue
18
{
19
    /**
20
     * @var \PDO
21
     */
22
    protected $pdo;
23
24
    /**
25
     * @var string
26
     */
27
    protected $tableName;
28
29 60
    public function __construct(\PDO $pdo, $tableName)
30
    {
31 60
        $driverName = $pdo->getAttribute(\PDO::ATTR_DRIVER_NAME);
32
33 60
        if (!$this->supportsDriver($driverName)) {
34 3
            throw new \InvalidArgumentException(sprintf(
35 3
                'PDO driver "%s" is unsupported by "%s".',
36 3
                $driverName,
37 3
                get_class($this)
38 3
            ));
39
        }
40
41 60
        $this->pdo = $pdo;
42 60
        $this->tableName = $tableName;
43 60
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 45
    public function push($item, $eta = null)
49
    {
50 45
        $sql = sprintf('INSERT INTO %s (eta, item) VALUES (%d, %s)',
51 45
            $this->tableName,
52 45
            QueueUtils::normalizeEta($eta),
53 45
            $this->pdo->quote($item)
54 39
        );
55
56 39
        $this->pdo->exec($sql);
57 36
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 6
    public function count()
63
    {
64 6
        $stmt = $this->pdo->query('SELECT COUNT(*) FROM '.$this->tableName);
65 3
        $result = $stmt->fetchColumn();
66 3
        $stmt->closeCursor();
67
68 3
        return $result;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74 6
    public function clear()
75
    {
76 6
        $this->pdo->exec('DELETE FROM '.$this->tableName);
77 3
    }
78
79
    /**
80
     * @param string $driverName
81
     *
82
     * @return bool
83
     */
84
    abstract protected function supportsDriver($driverName);
85
}
86