Completed
Push — master ( 5badca...c88774 )
by GBProd
02:27
created

Handler::dispatchProvidingFinishedEvent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 9
ccs 8
cts 8
cp 1
rs 9.6666
cc 2
eloc 5
nc 2
nop 1
crap 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
use GBProd\ElasticsearchDataProviderBundle\Event\HasFinishedProviding;
10
11
/**
12
 * Handle data providing
13
 * 
14
 * @author gbprod <[email protected]>
15
 */
16
class Handler
17
{
18
    /**
19
     * @var Registry
20
     */
21
    private $registry;
22
    
23
    /**
24
     * @var EventDispatcherInterface
25
     */
26
    private $dispatcher;
27
    
28
    /**
29
     * @param Registry                      $registry
30
     * @param EventDispatcherInterface|null $dispatcher
31
     */
32 2
    public function __construct(
33
        Registry $registry, 
34
        EventDispatcherInterface $dispatcher = null
35
    ) {
36 2
        $this->registry   = $registry;
37 2
        $this->dispatcher = $dispatcher;
38 2
    }
39
    
40
    /**
41
     * Handle provide command
42
     */
43 1
    public function handle(Client $client, $index, $type)
44
    {
45 1
        $entries = $this->registry->get($index, $type);
46
        
47 1
        $this->dispatchHandlingStartedEvent($entries);
48
        
49 1
        foreach($entries as $entry) {
50 1
            $this->dispatchProvidingStartedEvent($entry);
51
    
52 1
            $entry->getProvider()->run(
53 1
                $client, 
54 1
                $entry->getIndex(), 
55 1
                $entry->getType(),
56 1
                $this->dispatcher
57 1
            );
58
59 1
            $this->dispatchProvidingFinishedEvent($entry);
60 1
        }
61 1
    }
62
    
63 1
    private function dispatchHandlingStartedEvent(array $entries)
64
    {
65 1
        if ($this->dispatcher) {
66 1
            $this->dispatcher->dispatch(
67 1
                'elasticsearch.has_started_handling',
68 1
                new HasStartedHandling($entries)
69 1
            );
70 1
        }
71 1
    }
72
    
73 1
    private function dispatchProvidingStartedEvent(RegistryEntry $entry)
74
    {
75 1
        if ($this->dispatcher) {
76 1
            $this->dispatcher->dispatch(
77 1
                'elasticsearch.has_started_providing',
78 1
                new HasStartedProviding($entry)
79 1
            );
80 1
        }
81 1
    }
82
    
83 1
    private function dispatchProvidingFinishedEvent(RegistryEntry $entry)
84
    {
85 1
        if ($this->dispatcher) {
86 1
            $this->dispatcher->dispatch(
87 1
                'elasticsearch.has_finished_providing',
88 1
                new HasFinishedProviding($entry)
89 1
            );
90 1
        }
91 1
    }
92
}
93