Completed
Push — master ( 3b88e7...c64881 )
by GBProd
02:12
created

Handler::dispatchHandlingStartedEvent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.9765

Importance

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