Completed
Push — master ( f17301...aeaf79 )
by Jan-Petter
02:43
created

DelayHandler::getTopWaitTimes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 10

Duplication

Lines 22
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 22
loc 22
rs 9.2
cc 2
eloc 10
nc 2
nop 2
1
<?php
2
namespace vipnytt\RobotsTxtParser;
3
4
use PDO;
5
use vipnytt\RobotsTxtParser\Client\Directives\DelayHandlerClient;
6
use vipnytt\RobotsTxtParser\Client\Directives\DelayInterface;
7
use vipnytt\RobotsTxtParser\Parser\UrlParser;
8
use vipnytt\RobotsTxtParser\SQL\SQLInterface;
9
10
/**
11
 * Class DelayHandler
12
 *
13
 * @package vipnytt\RobotsTxtParser
14
 */
15
class DelayHandler implements SQLInterface
16
{
17
    use UrlParser;
18
19
    /**
20
     * Database connection
21
     * @var PDO
22
     */
23
    private $pdo;
24
25
    /**
26
     * DelayHandler constructor.
27
     *
28
     * @param PDO $pdo
29
     */
30
    public function __construct(PDO $pdo)
31
    {
32
        $this->pdo = $pdo;
33
        if ($this->pdo->getAttribute(PDO::ATTR_ERRMODE) === PDO::ERRMODE_SILENT) {
34
            $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
35
        }
36
        $this->pdo->setAttribute(PDO::ATTR_CASE, PDO::CASE_NATURAL);
37
        $this->pdo->exec('SET NAMES ' . self::SQL_ENCODING);
38
    }
39
40
    /**
41
     * Client
42
     *
43
     * @param DelayInterface $client
44
     * @return DelayHandlerClient
45
     */
46
    public function client(DelayInterface $client)
47
    {
48
        return new DelayHandlerClient($this->pdo, $client->getBaseUri(), $client->getUserAgent(), $client->getValue());
49
    }
50
51
    /**
52
     * Clean the delay table
53
     *
54
     * @param int $delay - in seconds
55
     * @return bool
56
     */
57 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...
58
    {
59
        $query = $this->pdo->prepare(<<<SQL
60
DELETE FROM robotstxt__delay0
61
WHERE delayUntil < ((UNIX_TIMESTAMP() - :delay) * 1000000);
62
SQL
63
        );
64
        $query->bindParam(':delay', $delay, PDO::PARAM_INT);
65
        return $query->execute();
66
    }
67
68
    /**
69
     * Top X delays
70
     *
71
     * @param int $limit
72
     * @param int $min
73
     * @return array
74
     */
75 View Code Duplication
    public function getTopDelays($limit = 100, $min = 0)
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...
76
    {
77
        $query = $this->pdo->prepare(<<<SQL
78
SELECT
79
  base,
80
  userAgent,
81
  delayUntil / 1000000,
82
  lastDelay / 1000000
83
FROM robotstxt__delay0
84
WHERE lastDelay > (:minDelay * 1000000)
85
ORDER BY lastDelay DESC
86
LIMIT :maxCount;
87
SQL
88
        );
89
        $query->bindParam(':minDelay', $min, PDO::PARAM_INT);
90
        $query->bindParam(':maxCount', $limit, PDO::PARAM_INT);
91
        $query->execute();
92
        if ($query->rowCount() > 0) {
93
            return $query->fetchAll(PDO::FETCH_ASSOC);
94
        }
95
        return [];
96
    }
97
98
    /**
99
     * Top X wait time
100
     *
101
     * @param int $limit
102
     * @param int $min
103
     * @return array
104
     */
105 View Code Duplication
    public function getTopWaitTimes($limit = 100, $min = 0)
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
SELECT
109
  base,
110
  userAgent,
111
  delayUntil / 1000000,
112
  lastDelay / 1000000
113
FROM robotstxt__delay0
114
WHERE delayUntil > ((UNIX_TIMESTAMP(CURTIME(6)) + :minDelay) * 1000000)
115
ORDER BY delayUntil DESC
116
LIMIT :maxCount;
117
SQL
118
        );
119
        $query->bindParam(':minDelay', $min, PDO::PARAM_INT);
120
        $query->bindParam(':maxCount', $limit, PDO::PARAM_INT);
121
        $query->execute();
122
        if ($query->rowCount() > 0) {
123
            return $query->fetchAll(PDO::FETCH_ASSOC);
124
        }
125
        return [];
126
    }
127
}
128