Completed
Push — master ( 5018c3...5108e7 )
by GBProd
07:07
created

Registry::filter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 5
nc 1
nop 2
1
<?php
2
3
namespace GBProd\ElasticsearchDataProviderBundle\DataProvider;
4
5
/**
6
 * Registry for DataProviders
7
 * 
8
 * @author gbprod <[email protected]>
9
 */
10
class Registry
11
{
12
    /**
13
     * @var array<DataProvider>
14
     */
15
    private $providers = [];
16
    
17
    /**
18
     * Add a provider to the registry
19
     * 
20
     * @param DataProvider $provider
21
     * @param string       $index
22
     * @param string       $type
23
     */
24
    public function addProvider(DataProvider $provider, $index, $type)
25
    {
26
        $this->providers[] = [
27
            'provider' => $provider,
28
            'index'    => $index,
29
            'type'     => $type,
30
        ];
31
        
32
        return $this;    
33
    }
34
    
35
    /**
36
     * Get providers for index and type
37
     * 
38
     * @param string $index
0 ignored issues
show
Documentation introduced by
Should the type for parameter $index not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
39
     * @param string $type
0 ignored issues
show
Documentation introduced by
Should the type for parameter $type not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
40
     * 
41
     * @return array<DataProvider>
42
     */
43
    public function getProviders($index = null, $type = null)
44
    {
45
        $filteredProviders = $this->filter($index, $type);
46
        
47
        return $this->mapProviders($filteredProviders);
48
    }
49
    
50
    private function filter($index, $type)
51
    {
52
        return array_filter(
53
            $this->providers,
54
            function ($providerData) use ($index, $type) {
55
                return $this->match($providerData, $index, $type);
56
            }
57
        );
58
    }
59
60
    private function match($providerData, $index, $type)
61
    {
62
        return (null === $index && $type === null)
63
            || ($providerData['index'] == $index && $type === null)
64
            || ($providerData['index'] == $index && $providerData['type'] == $type)
65
        ;
66
    
67
    }
68
    
69
    private function mapProviders(array $providers)
70
    {
71
        return array_map(
72
            function ($providerData) {
73
                return $providerData['provider'];    
74
            },
75
            $providers
76
        );
77
    }
78
}