Passed
Pull Request — master (#259)
by Arnaud
08:22
created

CompositeDataHandler   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 7
eloc 11
c 1
b 1
f 0
dl 0
loc 32
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 9 3
A __construct() 0 3 1
A supports() 0 9 3
1
<?php
2
3
namespace LAG\AdminBundle\DataProvider\DataSourceHandler;
4
5
use LAG\AdminBundle\DataProvider\DataSourceInterface;
6
use LAG\AdminBundle\Exception\DataHandler\ClassNotSupportedException;
7
8
class CompositeDataHandler implements DataHandlerInterface
9
{
10
    /**
11
     * @var iterable<DataHandlerInterface>
12
     */
13
    private iterable $dataHandlers;
14
15
    public function __construct(iterable $dataHandlers)
16
    {
17
        $this->dataHandlers = $dataHandlers;
18
    }
19
20
    public function supports(DataSourceInterface $dataSource): bool
21
    {
22
        foreach ($this->dataHandlers as $dataHandler) {
23
            if ($dataHandler->supports($dataSource)) {
24
                return true;
25
            }
26
        }
27
28
        return false;
29
    }
30
31
    public function handle(DataSourceInterface $dataSource)
32
    {
33
        foreach ($this->dataHandlers as $dataHandler) {
34
            if ($dataHandler->supports($dataSource)) {
35
                return $dataHandler->handle($dataSource);
36
            }
37
        }
38
39
        throw new ClassNotSupportedException($dataSource);
40
    }
41
}
42