Completed
Push — master ( d4f070...1ddfcf )
by Jan-Petter
05:06
created

DelayHandler::setup()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
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 microTime < ((UNIX_TIMESTAMP() - :delay) * 1000000);
62
SQL
63
        );
64
        $query->bindParam(':delay', $delay, PDO::PARAM_INT);
65
        return $query->execute();
66
    }
67
}
68