Completed
Push — master ( 512542...4d85fd )
by Jan-Petter
02:22
created

DelayHandler::__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;
3
4
use PDO;
5
use vipnytt\RobotsTxtParser\Client\Delay\DelayHandlerClient;
6
use vipnytt\RobotsTxtParser\Client\Directives\DelayInterface;
7
use vipnytt\RobotsTxtParser\Exceptions\SQLException;
8
use vipnytt\RobotsTxtParser\Parser\UrlParser;
9
use vipnytt\RobotsTxtParser\SQL\SQLInterface;
10
use vipnytt\RobotsTxtParser\SQL\SQLTrait;
11
12
/**
13
 * Class DelayHandler
14
 *
15
 * @package vipnytt\RobotsTxtParser
16
 */
17
class DelayHandler implements SQLInterface
18
{
19
    use SQLTrait;
20
    use UrlParser;
21
22
    /**
23
     * Database connection
24
     * @var PDO
25
     */
26
    private $pdo;
27
28
    /**
29
     * DelayHandler constructor.
30
     *
31
     * @param PDO $pdo
32
     */
33
    public function __construct(PDO $pdo)
34
    {
35
        $this->pdo = $this->pdoInitialize($pdo);
36
    }
37
38
    /**
39
     * Client
40
     *
41
     * @param DelayInterface $client
42
     * @return DelayHandlerClient
43
     */
44
    public function client(DelayInterface $client)
45
    {
46
        return new DelayHandlerClient($this->pdo, $client->getBaseUri(), $client->getUserAgent(), $client->getValue());
47
    }
48
49
    /**
50
     * Invalidate delay
51
     *
52
     * @param string $baseUri
53
     * @param string $userAgent
54
     * @return bool
55
     */
56
    public function invalidate($baseUri, $userAgent)
57
    {
58
        $base = $this->urlBase($this->urlEncode($baseUri));
59
        $query = $this->pdo->prepare(<<<SQL
60
DELETE FROM robotstxt__delay0
61
WHERE base = :base AND userAgent = :useragent;
62
SQL
63
        );
64
        $query->bindParam(':base', $base, PDO::PARAM_INT);
65
        $query->bindParam(':useragent', $userAgent, PDO::PARAM_INT);
66
        return $query->execute();
67
    }
68
69
    /**
70
     * List all with queue
71
     *
72
     * @param int $minSec
73
     * @param int $limit
74
     * @return array
75
     */
76
    public function listQueue($minSec = 0, $limit = 100)
77
    {
78
        $query = $this->pdo->prepare(<<<SQL
79
SELECT
80
  base,
81
  userAgent,
82
  microTime / 1000000,
83
  lastDelay
84
FROM robotstxt__delay0
85
WHERE microTime > ((UNIX_TIMESTAMP(CURTIME(6)) + :minimum) * 1000000)
86
ORDER BY microTime DESC
87
LIMIT :rowlimit;
88
SQL
89
        );
90
        $query->bindParam(':minimum', $minSec, PDO::PARAM_INT);
91
        $query->bindParam(':rowlimit', $limit, PDO::PARAM_INT);
92
        $query->execute();
93
        if ($query->rowCount() > 0) {
94
            return $query->fetchAll(PDO::FETCH_ASSOC);
95
        }
96
        return [];
97
    }
98
99
    /**
100
     * Clean the delay table
101
     *
102
     * @param int $delay - in seconds
103
     * @return bool
104
     */
105 View Code Duplication
    public function clean($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...
106
    {
107
        $query = $this->pdo->prepare(<<<SQL
108
DELETE FROM robotstxt__delay0
109
WHERE microTime < ((UNIX_TIMESTAMP() - :delay) * 1000000);
110
SQL
111
        );
112
        $query->bindParam(':delay', $delay, PDO::PARAM_INT);
113
        return $query->execute();
114
    }
115
116
    /**
117
     * Create SQL table
118
     *
119
     * @return bool
120
     * @throws SQLException
121
     */
122
    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...
123
    {
124
        if (!$this->createTable($this->pdo, self::TABLE_DELAY, file_get_contents(__DIR__ . '/SQL/delay.sql'))) {
125
            throw new SQLException('Unable to create table! Please read instructions at ' . self::README_SQL_DELAY);
126
        }
127
        return true;
128
    }
129
}
130