Handler::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
namespace GBProd\ElasticaProviderBundle\Provider;
4
5
use Elastica\Client;
6
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
7
use GBProd\ElasticaProviderBundle\Event\HasStartedHandling;
8
use GBProd\ElasticaProviderBundle\Event\HasStartedProviding;
9
use GBProd\ElasticaProviderBundle\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 4
    public function __construct(Registry $registry, EventDispatcherInterface $dispatcher = null)
33
    {
34 4
        $this->registry   = $registry;
35 4
        $this->dispatcher = $dispatcher;
36 4
    }
37
38
    /**
39
     * Handle provide command
40
     */
41 2
    public function handle(Client $client, $index, $type, $alias = null)
42
    {
43 2
        $alias = $alias ?: $index;
44
45 2
        $entries = $this->registry->get($alias, $type);
46
47 2
        $this->dispatchHandlingStartedEvent($entries);
48
49 2
        foreach ($entries as $entry) {
50 2
            $this->dispatchProvidingStartedEvent($entry);
51
52 2
            $entry->getProvider()->run(
53 2
                $client,
54 2
                $index,
55 2
                $entry->getType(),
56 2
                $this->dispatcher
57 2
            );
58
59 2
            $this->dispatchProvidingFinishedEvent($entry);
60 2
        }
61 2
    }
62
63 2
    private function dispatchHandlingStartedEvent(array $entries)
64
    {
65 2
        if ($this->dispatcher) {
66 2
            $this->dispatcher->dispatch(
67 2
                'elasticsearch.has_started_handling',
68 2
                new HasStartedHandling($entries)
69 2
            );
70 2
        }
71 2
    }
72
73 2
    private function dispatchProvidingStartedEvent(RegistryEntry $entry)
74
    {
75 2
        if ($this->dispatcher) {
76 2
            $this->dispatcher->dispatch(
77 2
                'elasticsearch.has_started_providing',
78 2
                new HasStartedProviding($entry)
79 2
            );
80 2
        }
81 2
    }
82
83 2
    private function dispatchProvidingFinishedEvent(RegistryEntry $entry)
84
    {
85 2
        if ($this->dispatcher) {
86 2
            $this->dispatcher->dispatch(
87 2
                'elasticsearch.has_finished_providing',
88 2
                new HasFinishedProviding($entry)
89 2
            );
90 2
        }
91 2
    }
92
}
93