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

CacheMaintenanceSQL   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 50
Duplicated Lines 14 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A clean() 0 10 1
A setup() 7 7 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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