Completed
Branch 2.0-dev (8e7c3a)
by Jan-Petter
02:24
created

Maintenance::__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\Cache\SQL;
3
4
use PDO;
5
use vipnytt\RobotsTxtParser\Core\RobotsTxtInterface;
6
7
/**
8
 * Class Maintenance
9
 *
10
 * @package vipnytt\RobotsTxtParser\Cache\SQL
11
 */
12
class Maintenance implements RobotsTxtInterface
13
{
14
    /**
15
     * @var PDO
16
     */
17
    protected $pdo;
18
19
    /**
20
     * Maintenance constructor.
21
     *
22
     * @param PDO $pdo
23
     */
24
    public function __construct(PDO $pdo)
25
    {
26
        $this->pdo = $pdo;
27
    }
28
29
    /**
30
     * Clean all tables automatically
31
     *
32
     * @return bool
33
     */
34
    public function autoClean()
35
    {
36
        $cache = $this->cleanCache();
37
        $delay = $this->cleanDelay();
38
        return $cache && $delay;
39
    }
40
41
    /**
42
     * Clean the cache table
43
     *
44
     * @param int $delay - in seconds
45
     * @return bool
46
     */
47 View Code Duplication
    public function cleanCache($delay = self::CACHE_TIME)
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...
48
    {
49
        $query = $this->pdo->prepare(<<<SQL
50
DELETE FROM robotstxt__cache0
51
WHERE worker = 0 AND nextUpdate < (UNIX_TIMESTAMP() - :delay);
52
SQL
53
        );
54
        $query->bindParam(':delay', $delay, PDO::PARAM_INT);
55
        return $query->execute();
56
    }
57
58
    /**
59
     * Clean the delay table
60
     *
61
     * @param int $delay - in seconds
62
     * @return bool
63
     */
64 View Code Duplication
    public function cleanDelay($delay = 60)
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...
65
    {
66
        $query = $this->pdo->prepare(<<<SQL
67
DELETE FROM robotstxt__delay0
68
WHERE microTime < ((UNIX_TIMESTAMP() - :delay) * 1000000);
69
SQL
70
        );
71
        $query->bindParam(':delay', $delay, PDO::PARAM_INT);
72
        return $query->execute();
73
    }
74
}
75