Passed
Branch feature/second-release (e9200f)
by Andrea Marco
13:37
created

RespectsRateLimits::respectRateLimits()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 2
nc 3
nop 0
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cerbero\LazyJsonPages\Concerns;
6
7
use Cerbero\LazyJsonPages\Services\ClientFactory;
8
9
/**
10
 * The trait to respect rate limits of APIs.
11
 */
12
trait RespectsRateLimits
13
{
14
    /**
15
     * Delay the execution to respect rate limits.
16
     */
17 39
    protected function respectRateLimits(): void
18
    {
19 39
        if (microtime(true) < $timestamp = $this->config->rateLimits?->resetAt() ?? 0.0) {
20 1
            ClientFactory::isFake() ? ClientFactory::$fakedRateLimits[] = $timestamp : time_sleep_until($timestamp);
21
        }
22
    }
23
}
24