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

Manager   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 85
Duplicated Lines 14.12 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 5
Bugs 3 Features 2
Metric Value
wmc 5
c 5
b 3
f 2
lcom 1
cbo 0
dl 12
loc 85
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A clean() 0 10 1
A getTopWaitTimes() 0 22 2
A debug() 12 12 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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