Passed
Pull Request — master (#6)
by
unknown
02:25
created

PaginationProviderChain::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace ElevenLabs\ApiServiceBundle\Pagination;
4
5
use ElevenLabs\Api\Definition\ResponseDefinition;
6
use ElevenLabs\Api\Service\Pagination\PaginationProvider;
7
use Psr\Http\Message\ResponseInterface;
8
9
class PaginationProviderChain implements PaginationProvider
10
{
11
    /**
12
     * @var PaginationProvider[]
13
     */
14
    private $providers;
15
16
    /**
17
     * @var PaginationProvider
18
     */
19
    private $matchedProvider;
20
21
    /**
22
     * @param PaginationProvider[] $providers
23
     */
24 5
    public function __construct(array $providers)
25
    {
26 5
        $this->providers = [];
27 5
        $this->matchedProvider = null;
28 5
        foreach ($providers as $provider) {
29 3
            $this->addProvider($provider);
30
        }
31 5
    }
32
33
    /**
34
     * @param array              $data
35
     * @param ResponseInterface  $response
36
     * @param ResponseDefinition $responseDefinition
37
     *
38
     * @return mixed
39
     */
40 2
    public function getPagination(array &$data, ResponseInterface $response, ResponseDefinition $responseDefinition)
41
    {
42 2
        if ($this->matchedProvider === null) {
43 1
            throw new \LogicException('No pagination provider available');
44
        }
45
46 1
        $pagination = $this->matchedProvider->getPagination($data, $response, $responseDefinition);
47
        // reset matched provider
48 1
        $this->matchedProvider = null;
49
50 1
        return $pagination;
51
    }
52
53
    /**
54
     * @param array              $data
55
     * @param ResponseInterface  $response
56
     * @param ResponseDefinition $responseDefinition
57
     *
58
     * @return bool
59
     */
60 4
    public function supportPagination(array $data, ResponseInterface $response, ResponseDefinition $responseDefinition)
61
    {
62 4
        foreach ($this->providers as $index => $provider) {
63 3
            if ($provider->supportPagination($data, $response, $responseDefinition)) {
64 2
                $this->matchedProvider = $provider;
65 2
                return true;
66
            }
67
        }
68
69 2
        return false;
70
    }
71
72
    /**
73
     * @param PaginationProvider $provider
74
     */
75 3
    private function addProvider(PaginationProvider $provider)
76
    {
77 3
        $this->providers[] = $provider;
78 3
    }
79
}
80