Completed
Push — master ( c64881...c0b61c )
by GBProd
02:14
created

Handler   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 1 Features 1
Metric Value
wmc 7
c 5
b 1
f 1
lcom 1
cbo 4
dl 0
loc 65
ccs 33
cts 33
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A handle() 0 17 2
A dispatchHandlingStartedEvent() 0 9 2
A dispatchProvidingStartedEvent() 0 9 2
1
<?php
2
3
namespace GBProd\ElasticsearchDataProviderBundle\DataProvider;
4
5
use Elasticsearch\Client;
6
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
7
use GBProd\ElasticsearchDataProviderBundle\Event\HasStartedHandling;
8
use GBProd\ElasticsearchDataProviderBundle\Event\HasStartedProviding;
9
10
/**
11
 * Handle data providing
12
 * 
13
 * @author gbprod <[email protected]>
14
 */
15
class Handler
16
{
17
    /**
18
     * @var Registry
19
     */
20
    private $registry;
21
    
22
    /**
23
     * @var EventDispatcherInterface
24
     */
25
    private $dispatcher;
26
    
27
    /**
28
     * @param Registry                      $registry
29
     * @param EventDispatcherInterface|null $dispatcher
30
     */
31 2
    public function __construct(
32
        Registry $registry, 
33
        EventDispatcherInterface $dispatcher = null
34
    ) {
35 2
        $this->registry   = $registry;
36 2
        $this->dispatcher = $dispatcher;
37 2
    }
38
    
39
    /**
40
     * Handle provide command
41
     */
42 1
    public function handle(Client $client, $index, $type)
43
    {
44 1
        $entries = $this->registry->get($index, $type);
45
        
46 1
        $this->dispatchHandlingStartedEvent($entries);
47
        
48 1
        foreach($entries as $entry) {
49 1
            $this->dispatchProvidingStartedEvent($entry);
50
    
51 1
            $entry->getProvider()->run(
52 1
                $client, 
53 1
                $entry->getIndex(), 
54 1
                $entry->getType(),
55 1
                $this->dispatcher
56 1
            );
57 1
        }
58 1
    }
59
    
60 1
    private function dispatchHandlingStartedEvent(array $entries)
61
    {
62 1
        if ($this->dispatcher) {
63 1
            $this->dispatcher->dispatch(
64 1
                'elasticsearch.has_started_handling',
65 1
                new HasStartedHandling($entries)
66 1
            );
67 1
        }
68 1
    }
69
    
70 1
    private function dispatchProvidingStartedEvent(RegistryEntry $entry)
71
    {
72 1
        if ($this->dispatcher) {
73 1
            $this->dispatcher->dispatch(
74 1
                'elasticsearch.has_started_providing',
75 1
                new HasStartedProviding($entry)
76 1
            );
77 1
        }
78 1
    }
79
}
80