Completed
Push — master ( 1b6743...e69112 )
by Jan-Petter
9s
created

SQL::maintenance()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
namespace vipnytt\RobotsTxtParser;
3
4
use PDO;
5
use vipnytt\RobotsTxtParser\Client\SQL\Cache\CacheCoreSQL;
6
use vipnytt\RobotsTxtParser\Client\SQL\SQLMaintenance;
7
use vipnytt\RobotsTxtParser\Parser\UrlParser;
8
9
/**
10
 * Class SQL
11
 *
12
 * @package vipnytt\RobotsTxtParser
13
 */
14
class SQL extends CacheCoreSQL
15
{
16
    use UrlParser;
17
18
    /**
19
     * Client nextUpdate margin in seconds
20
     * @var int
21
     */
22
    private $clientUpdateMargin = 300;
23
24
    /**
25
     * Cache constructor.
26
     *
27
     * @param PDO $pdo
28
     * @param array $guzzleConfig
29
     * @param int|null $byteLimit
30
     */
31
    public function __construct(PDO $pdo, array $guzzleConfig = [], $byteLimit = self::BYTE_LIMIT)
32
    {
33
        parent::__construct($pdo, $guzzleConfig, $byteLimit);
34
    }
35
36
    /**
37
     * Parser client
38
     *
39
     * @param string $baseUri
40
     * @return Basic
41
     */
42
    public function client($baseUri)
43
    {
44
        $base = $this->urlBase($this->urlEncode($baseUri));
45
        $query = $this->pdo->prepare(<<<SQL
46
SELECT
47
  content,
48
  statusCode,
49
  nextUpdate,
50
  worker
51
FROM robotstxt__cache0
52
WHERE base = :base;
53
SQL
54
        );
55
        $query->bindParam(':base', $base, PDO::PARAM_STR);
56
        $query->execute();
57
        if ($query->rowCount() > 0) {
58
            $row = $query->fetch(PDO::FETCH_ASSOC);
59
            if ($row['nextUpdate'] >= (time() - $this->clientUpdateMargin)) {
60
                $this->markAsActive($base, $row['worker']);
61
                return new Basic($base, $row['code'], $row['content'], self::ENCODING, $this->byteLimit);
62
            }
63
        }
64
        $request = new URI($base, $this->guzzleConfig, $this->byteLimit);
65
        $this->push($request);
66
        $this->markAsActive($base);
67
        return new Basic($base, $request->getStatusCode(), $request->getContents(), self::ENCODING, $this->byteLimit);
68
    }
69
70
    /**
71
     * Mark robots.txt as active
72
     *
73
     * @param string $base
74
     * @param int|null $workerID
75
     * @return bool
76
     */
77
    private function markAsActive($base, $workerID = 0)
78
    {
79
        if ($workerID == 0) {
80
            $query = $this->pdo->prepare(<<<SQL
81
UPDATE robotstxt__cache0
82
SET worker = NULL
83
WHERE base = :base AND worker = 0;
84
SQL
85
            );
86
            $query->bindParam(':base', $base, PDO::PARAM_STR);
87
            return $query->execute();
88
        }
89
        return true;
90
    }
91
92
    /**
93
     * Maintenance
94
     *
95
     * @return SQLMaintenance
96
     */
97
    public function maintenance()
98
    {
99
        return new SQLMaintenance($this->pdo);
100
    }
101
}
102