Completed
Branch 2.0-dev (d250b8)
by Jan-Petter
03:02
created

CacheMaintenanceSQL::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
namespace vipnytt\RobotsTxtParser\Client\SQL\Cache;
3
4
use PDO;
5
use vipnytt\RobotsTxtParser\Client\SQL\SQLInterface;
6
use vipnytt\RobotsTxtParser\Client\SQL\SQLTrait;
7
use vipnytt\RobotsTxtParser\Exceptions\SQLException;
8
use vipnytt\RobotsTxtParser\RobotsTxtInterface;
9
10
/**
11
 * Class CacheMaintenanceSQL
12
 *
13
 * @package vipnytt\RobotsTxtParser\Client\SQL\Cache
14
 */
15
class CacheMaintenanceSQL implements RobotsTxtInterface, SQLInterface
16
{
17
    use SQLTrait;
18
19
    /**
20
     * @var PDO
21
     */
22
    private $pdo;
23
24
    /**
25
     * Maintenance constructor.
26
     *
27
     * @param PDO $pdo
28
     */
29
    public function __construct(PDO $pdo)
30
    {
31
        $this->pdo = $this->pdoInitialize($pdo);
32
    }
33
34
    /**
35
     * Clean the cache table
36
     *
37
     * @param int $delay - in seconds
38
     * @return bool
39
     */
40
    public function clean($delay = self::CACHE_TIME)
41
    {
42
        $query = $this->pdo->prepare(<<<SQL
43
DELETE FROM robotstxt__cache0
44
WHERE worker = 0 AND nextUpdate < (UNIX_TIMESTAMP() - :delay);
45
SQL
46
        );
47
        $query->bindParam(':delay', $delay, PDO::PARAM_INT);
48
        return $query->execute();
49
    }
50
51
    /**
52
     * Create SQL table
53
     *
54
     * @return bool
55
     * @throws SQLException
56
     */
57 View Code Duplication
    public function setup()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
58
    {
59
        if (!$this->createTable($this->pdo, self::TABLE_CACHE, file_get_contents(__DIR__ . '/cache.sql'))) {
60
            throw new SQLException('Unable to create table! Please read instructions at ' . self::README_SQL_CACHE);
61
        }
62
        return true;
63
    }
64
}
65