Completed
Pull Request — master (#612)
by
unknown
02:32
created

TerminateListener   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 42
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A onKernelTerminate() 0 14 4
1
<?php
2
3
/*
4
 * This file is part of the ONGR package.
5
 *
6
 * (c) NFQ Technologies UAB <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ONGR\ElasticsearchBundle\EventListener;
13
14
use ONGR\ElasticsearchBundle\Service\Manager;
15
use Symfony\Component\DependencyInjection\Container;
16
17
class TerminateListener
18
{
19
    /**
20
     * @var Container
21
     */
22
    private $container;
23
24
    /**
25
     * @var array
26
     */
27
    private $managers;
28
29
    /**
30
     * Constructor
31
     *
32
     * @param Container $container
33
     * @param array     $managers
34
     */
35
    public function __construct(Container $container, array $managers)
36
    {
37
        $this->container = $container;
38
        $this->managers = $managers;
39
    }
40
41
    /**
42
     * Forces commit to elasticsearch on kernel terminate
43
     */
44
    public function onKernelTerminate()
45
    {
46
        foreach ($this->managers as $key => $value) {
47
            if ($value['force_commit']) {
48
                try {
49
                    /** @var Manager $manager */
50
                    $manager = $this->container->get(sprintf('es.manager.%s', $key));
51
                } catch (\Exception $e) {
52
                    continue;
53
                }
54
                $manager->commit();
55
            }
56
        }
57
    }
58
}
59