Completed
Push — master ( 0cbd7a...258693 )
by Jan-Petter
04:51
created

Manager::debug()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 12
Ratio 100 %

Importance

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