Completed
Pull Request — master (#2)
by Jan-Petter
02:43
created

CacheMaintenanceSQL   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 50
wmc 4
lcom 1
cbo 2
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A clean() 0 10 1
A setup() 0 7 2
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\SQLMaintenanceInterface;
7
use vipnytt\RobotsTxtParser\Client\SQL\SQLTrait;
8
use vipnytt\RobotsTxtParser\Exceptions\SQLException;
9
use vipnytt\RobotsTxtParser\RobotsTxtInterface;
10
11
/**
12
 * Class CacheMaintenanceSQL
13
 *
14
 * @package vipnytt\RobotsTxtParser\Client\SQL\Cache
15
 */
16
class CacheMaintenanceSQL implements RobotsTxtInterface, SQLInterface, SQLMaintenanceInterface
17
{
18
    use SQLTrait;
19
20
    /**
21
     * @var PDO
22
     */
23
    private $pdo;
24
25
    /**
26
     * Maintenance constructor.
27
     *
28
     * @param PDO $pdo
29
     */
30
    public function __construct(PDO $pdo)
31
    {
32
        $this->pdo = $this->pdoInitialize($pdo);
33
    }
34
35
    /**
36
     * Clean the cache table
37
     *
38
     * @param int $delay - in seconds
39
     * @return bool
40
     */
41
    public function clean($delay = self::CACHE_TIME)
42
    {
43
        $query = $this->pdo->prepare(<<<SQL
44
DELETE FROM robotstxt__cache0
45
WHERE worker = 0 AND nextUpdate < (UNIX_TIMESTAMP() - :delay);
46
SQL
47
        );
48
        $query->bindParam(':delay', $delay, PDO::PARAM_INT);
49
        return $query->execute();
50
    }
51
52
    /**
53
     * Create SQL table
54
     *
55
     * @return bool
56
     * @throws SQLException
57
     */
58
    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...
59
    {
60
        if (!$this->createTable($this->pdo, self::TABLE_CACHE, file_get_contents(__DIR__ . '/cache.sql'))) {
61
            throw new SQLException('Unable to create table! Please read instructions at ' . self::README_SQL_CACHE);
62
        }
63
        return true;
64
    }
65
}
66