BaseCore   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 2
dl 0
loc 66
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A sleep() 0 14 3
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;
10
11
use vipnytt\RobotsTxtParser\Exceptions\OutOfSyncException;
12
use vipnytt\RobotsTxtParser\Handler\DatabaseTrait;
13
use vipnytt\RobotsTxtParser\Parser\UriParser;
14
15
/**
16
 * Class BaseCore
17
 *
18
 * @see https://github.com/VIPnytt/RobotsTxtParser/blob/master/docs/methods/DelayClient.md for documentation
19
 * @package vipnytt\RobotsTxtParser\Client\Delay
20
 */
21
abstract class BaseCore implements BaseInterface
22
{
23
    use DatabaseTrait;
24
25
    /**
26
     * Database handler
27
     * @var \PDO
28
     */
29
    protected $pdo;
30
31
    /**
32
     * Base uri
33
     * @var string
34
     */
35
    protected $base;
36
37
    /**
38
     * User-agent
39
     * @var string
40
     */
41
    protected $userAgent;
42
43
    /**
44
     * Delay
45
     * @var float|int
46
     */
47
    protected $delay;
48
49
    /**
50
     * BaseCore constructor.
51
     *
52
     * @param \PDO $pdo
53
     * @param string $baseUri
54
     * @param string $userAgent
55
     * @param float|int $delay
56
     */
57
    public function __construct(\PDO $pdo, $baseUri, $userAgent, $delay)
58
    {
59
        $uriParser = new UriParser($baseUri);
60
        $this->base = $uriParser->base();
61
        $this->pdo = $pdo;
62
        $this->userAgent = $userAgent;
63
        $this->delay = $delay;
64
    }
65
66
    /**
67
     * Sleep
68
     *
69
     * @return float|int
70
     * @throws OutOfSyncException
71
     */
72
    public function sleep()
73
    {
74
        $start = microtime(true);
75
        $until = $this->getTimeSleepUntil();
76
        if (microtime(true) > $until) {
77
            return 0;
78
        }
79
        try {
80
            time_sleep_until($until);
81
        } catch (\Exception $warning) {
82
            // Timestamp already in the past
83
        }
84
        return microtime(true) - $start;
85
    }
86
}
87