Completed
Push — master ( 0aa154...6fc129 )
by Maksim
05:50
created

RegisterListenersService::register()   C

Complexity

Conditions 7
Paths 16

Size

Total Lines 46
Code Lines 26

Duplication

Lines 24
Ratio 52.17 %

Code Coverage

Tests 21
CRAP Score 8.645

Importance

Changes 0
Metric Value
dl 24
loc 46
ccs 21
cts 31
cp 0.6774
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 26
nc 16
nop 3
crap 8.645
1
<?php
2
namespace FOS\ElasticaBundle\Doctrine;
3
4
use Doctrine\Common\Persistence\ObjectManager;
5
use Doctrine\ODM\MongoDB\DocumentManager;
6
use Doctrine\ORM\EntityManagerInterface;
7
use FOS\ElasticaBundle\Persister\Event\Events;
8
use FOS\ElasticaBundle\Persister\Event\PersistEvent;
9
use FOS\ElasticaBundle\Provider\PagerInterface;
10
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
11
12
class RegisterListenersService
13
{
14
    /**
15
     * @var EventDispatcherInterface
16
     */
17
    private $dispatcher;
18
19 9
    public function __construct(EventDispatcherInterface $dispatcher)
20
    {
21 9
        $this->dispatcher = $dispatcher;
22 9
    }
23
24 8
    public function register(ObjectManager $manager, PagerInterface $pager, array $options)
25
    {
26 8
        $options = array_replace([
27 8
            'clear_object_manager' => true,
28
            'debug_logging'        => false,
29
            'sleep'                => 0,
30 8
        ], $options);
31
32 8
        if ($options['clear_object_manager']) {
33 2
            $this->addListener($pager, Events::POST_INSERT_OBJECTS, function() use ($manager) {
34 1
                $manager->clear();
35 2
            });
36
        }
37
38 8
        if ($options['sleep']) {
39 2
            $this->addListener($pager, Events::POST_INSERT_OBJECTS, function() use ($options) {
40 1
                usleep($options['sleep']);
41 2
            });
42
        }
43
44 8 View Code Duplication
        if (false == $options['debug_logging'] && $manager instanceof EntityManagerInterface) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
45 1
            $configuration = $manager->getConnection()->getConfiguration();
46 1
            $logger = $configuration->getSQLLogger();
47
            
48 1
            $this->addListener($pager, Events::PRE_FETCH_OBJECTS, function() use ($configuration) {
49
                $configuration->setSQLLogger(null);
50 1
            });
51
52 1
            $this->addListener($pager, Events::PRE_INSERT_OBJECTS, function() use ($configuration, $logger) {
53
                $configuration->setSQLLogger($logger);
54 1
            });
55
        }
56
57 8 View Code Duplication
        if (false == $options['debug_logging'] && $manager instanceof DocumentManager) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
Bug introduced by
The class Doctrine\ODM\MongoDB\DocumentManager does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
58
            $configuration = $manager->getConnection()->getConfiguration();
59
            $logger = $configuration->getLoggerCallable();
60
61
            $this->addListener($pager, Events::PRE_FETCH_OBJECTS, function() use ($configuration) {
62
                $configuration->setLoggerCallable(null);
63
            });
64
65
            $this->addListener($pager, Events::PRE_INSERT_OBJECTS, function() use ($configuration, $logger) {
66
                $configuration->setLoggerCallable($logger);
67
            });
68
        }
69 8
    }
70
71
    /**
72
     * @param PagerInterface $pager
73
     * @param string $eventName
74
     * @param \Closure $callable
75
     */
76
    private function addListener(PagerInterface $pager, $eventName, \Closure $callable)
77
    {
78 5
        $this->dispatcher->addListener($eventName, function(PersistEvent $event) use ($pager, $callable) {
79 4
            if ($event->getPager() !== $pager) {
80 2
                return;
81
            }
82
83 2
            call_user_func_array($callable, func_get_args());
84 5
        });
85
    }
86
}
87