Handler   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 5
dl 0
loc 77
ccs 42
cts 42
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A handle() 0 19 2
A dispatchHandlingStartedEvent() 0 9 2
A dispatchProvidingStartedEvent() 0 9 2
A dispatchProvidingFinishedEvent() 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
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