PaginationProviderChain   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 88.89%

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 50
ccs 16
cts 18
cp 0.8889
rs 10
c 0
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 2
A getPagination() 0 11 2
A addProvider() 0 3 1
A supportPagination() 0 10 3
1
<?php
2
namespace ElevenLabs\ApiServiceBundle\Pagination;
3
4
use ElevenLabs\Api\Definition\ResponseDefinition;
5
use ElevenLabs\Api\Service\Pagination\PaginationProvider;
6
use Psr\Http\Message\ResponseInterface;
7
8
class PaginationProviderChain implements PaginationProvider
9
{
10
    /**
11
     * @var PaginationProvider[]
12
     */
13
    private $providers;
14
15
    /**
16
     * @var PaginationProvider
17
     */
18
    private $matchedProvider;
19
20
    /**
21
     * @param PaginationProvider[] $providers
22
     */
23 4
    public function __construct(array $providers)
24
    {
25 4
        foreach ($providers as $provider) {
26 4
            $this->addProvider($provider);
27
        }
28 4
    }
29
30 1
    public function getPagination(array &$data, ResponseInterface $response, ResponseDefinition $responseDefinition)
31
    {
32 1
        if ($this->matchedProvider === null) {
33
            throw new \LogicException('No pagination provider available');
34
        }
35
36 1
        $pagination = $this->matchedProvider->getPagination($data, $response, $responseDefinition);
37
        // reset matched provider
38 1
        $this->matchedProvider = null;
39
40 1
        return $pagination;
41
    }
42
43 1
    public function supportPagination(array $data, ResponseInterface $response, ResponseDefinition $responseDefinition)
44
    {
45 1
        foreach ($this->providers as $index => $provider) {
46 1
            if ($provider->supportPagination($data, $response, $responseDefinition)) {
47 1
                $this->matchedProvider = $provider;
48 1
                return true;
49
            }
50
        }
51
52
        return false;
53
    }
54
55 4
    private function addProvider(PaginationProvider $provider)
56
    {
57 4
        $this->providers[] = $provider;
58 4
    }
59
}
60