Completed
Push — master ( 6eb58a...ff20ae )
by Jan-Petter
03:32
created

Manager::getTopWaitTimes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 22
rs 9.2
cc 2
eloc 10
nc 2
nop 2
1
<?php
2
namespace vipnytt\RobotsTxtParser\Client\Delay\MySQL;
3
4
use PDO;
5
use vipnytt\RobotsTxtParser\Client\Delay\ManagerInterface;
6
use vipnytt\RobotsTxtParser\Exceptions\SQLException;
7
use vipnytt\RobotsTxtParser\Parser\UriParser;
8
9
/**
10
 * Class Manager
11
 *
12
 * @see https://github.com/VIPnytt/RobotsTxtParser/blob/master/docs/methods/DelayManager.md for documentation
13
 * @package vipnytt\RobotsTxtParser\Handler\Delay\MySQL
14
 */
15
class Manager implements ManagerInterface
16
{
17
    use UriParser;
18
19
    /**
20
     * Database handler
21
     * @var PDO
22
     */
23
    private $pdo;
24
25
    /**
26
     * Manager constructor.
27
     *
28
     * @param PDO $pdo
29
     * @throws SQLException
30
     */
31
    public function __construct(PDO $pdo)
32
    {
33
        $this->pdo = $pdo;
34
    }
35
36
    /**
37
     * Clean the delay table
38
     *
39
     * @param int $delay - in seconds
40
     * @return bool
41
     */
42 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...
43
    {
44
        $query = $this->pdo->prepare(<<<SQL
45
DELETE FROM robotstxt__delay0
46
WHERE delayUntil < ((UNIX_TIMESTAMP() - :delay) * 1000000);
47
SQL
48
        );
49
        $query->bindParam(':delay', $delay, PDO::PARAM_INT);
50
        return $query->execute();
51
    }
52
53
    /**
54
     * Top X wait time
55
     *
56
     * @param int $limit
57
     * @param int $min
58
     * @return array
59
     */
60
    public function getTopWaitTimes($limit = 100, $min = 0)
61
    {
62
        $query = $this->pdo->prepare(<<<SQL
63
SELECT
64
  base,
65
  userAgent,
66
  delayUntil / 1000000,
67
  lastDelay / 1000000
68
FROM robotstxt__delay0
69
WHERE delayUntil > ((UNIX_TIMESTAMP(CURTIME(6)) + :minDelay) * 1000000)
70
ORDER BY delayUntil DESC
71
LIMIT :maxCount;
72
SQL
73
        );
74
        $query->bindParam(':minDelay', $min, PDO::PARAM_INT);
75
        $query->bindParam(':maxCount', $limit, PDO::PARAM_INT);
76
        $query->execute();
77
        if ($query->rowCount() > 0) {
78
            return $query->fetchAll(PDO::FETCH_ASSOC);
79
        }
80
        return [];
81
    }
82
}