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

RespectsRateLimits   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 9
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
dl 0
loc 9
ccs 3
cts 3
cp 1
rs 10
c 1
b 0
f 0
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A respectRateLimits() 0 4 3
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