SaxulumWebProfilerProvider::register()   C
last analyzed

Complexity

Conditions 8
Paths 2

Size

Total Lines 96
Code Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 96
rs 5.3987
c 0
b 0
f 0
cc 8
eloc 52
nc 2
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Saxulum\SaxulumWebProfiler\Provider;
4
5
use Pimple\Container;
6
use Pimple\ServiceProviderInterface;
7
use Saxulum\DoctrineMongodbOdmManagerRegistry\Doctrine\ManagerRegistry;
8
use Saxulum\SaxulumWebProfiler\DataCollector\DoctrineDataCollector;
9
use Saxulum\SaxulumWebProfiler\DataCollector\DoctrineMongoDbDataCollector;
10
use Saxulum\SaxulumWebProfiler\Logger\DbalLogger;
11
use Saxulum\SaxulumWebProfiler\Logger\DoctrineMongoDbAggregateLogger;
12
use Saxulum\SaxulumWebProfiler\Logger\DoctrineMongoDbLogger;
13
use Saxulum\SaxulumWebProfiler\Twig\DoctrineExtension;
14
15
class SaxulumWebProfilerProvider implements ServiceProviderInterface
16
{
17
    /**
18
     * {@inheritdoc}
19
     */
20
    public function register(Container $app)
21
    {
22
        if (isset($app['profiler'])) {
23
24
            $app['twig'] = $app->extend('twig', function (\Twig_Environment $twig) {
25
                $twig->addExtension(new DoctrineExtension());
26
27
                return $twig;
28
            });
29
30
            $app['twig.loader.filesystem'] = $app->extend('twig.loader.filesystem',
31
                function (\Twig_Loader_Filesystem $twigLoaderFilesystem) {
32
                    $twigLoaderFilesystem->addPath(dirname(__DIR__). '/Resources/views', 'SaxulumWebProfilerProvider');
33
34
                    return $twigLoaderFilesystem;
35
                }
36
            );
37
38
            $app['data_collectors'] = $app->extend('data_collectors',
39
                function(array $collectors) use ($app) {
40
                    if(isset($app['doctrine'])) {
41
                        $app['saxulum.orm.logger'] = $app->factory(function ($app) {
42
                            return new DbalLogger($app['monolog'], $app['stopwatch']);
43
                        });
44
45
                        $collectors['db'] = function ($app) {
46
                            $dataCollector = new DoctrineDataCollector($app['doctrine']);
47
                            foreach ($app['doctrine']->getConnectionNames() as $name) {
48
                                $logger = $app['saxulum.orm.logger'];
49
                                $app['doctrine']->getConnection($name)->getConfiguration()->setSQLLogger($logger);
50
                                $dataCollector->addLogger($name, $logger);
51
                            }
52
53
                            return $dataCollector;
54
                        };
55
                    }
56
57
                    if(isset($app['doctrine_mongodb'])) {
58
                        $app['saxulum.webprofiler.mongodb.odm.logger.batchthreshold'] = 4;
59
60
                        $app['saxulum.mongodb.odm.logger'] = function ($app) {
61
                            $logger = new DoctrineMongoDbLogger($app['monolog']);
62
                            $logger->setBatchInsertThreshold($app['saxulum.mongodb.odm.logger.batchthreshold']);
63
64
                            return $logger;
65
                        };
66
67
                        $app['saxulum.mongodb.odm.loggers'] = function ($app) {
68
                            $loggers = array();
69
                            $loggers[] = $app['saxulum.mongodb.odm.logger'];
70
                            $loggers[] = $app['saxulum.mongodb.odm.datacolletor'];
71
72
                            return $loggers;
73
                        };
74
75
                        $app['saxulum.mongodb.odm.aggregatelogger'] = function ($app) {
76
                            $logger = new DoctrineMongoDbAggregateLogger($app['saxulum.mongodb.odm.loggers']);
77
78
                            return $logger;
79
                        };
80
81
                        $aggregatedLogger = $app['saxulum.mongodb.odm.aggregatelogger'];
82
                        $app['doctrine_mongodb'] = $app->extend('doctrine_mongodb',
83
                            function(ManagerRegistry $registry) use($aggregatedLogger) {
84
                                foreach ($registry->getConnectionNames() as $name) {
85
                                    $registry->getConnection($name)->getConfiguration()->setLoggerCallable(array($aggregatedLogger, 'logQuery'));
86
                                }
87
                            }
88
                        );
89
90
                        $collectors['mongodb'] = function ($app) {
91
                            $dataCollector = new DoctrineMongoDbDataCollector();
92
                            $dataCollector->setBatchInsertThreshold($app['saxulum.mongodb.odm.logger.batchthreshold']);
93
94
                            return $dataCollector;
95
                        };
96
                    }
97
98
                    return $collectors;
99
                }
100
            );
101
102
            $app['data_collector.templates'] = $app->extend('data_collector.templates',
103
                function(array $dataCollectorTemplates) use ($app) {
104
                    if(isset($app['doctrine'])) {
105
                        $dataCollectorTemplates[] = array('db', '@SaxulumWebProfilerProvider/Collector/db.html.twig');
106
                    }
107
                    if(isset($app['doctrine_mongodb'])) {
108
                        $dataCollectorTemplates[] = array('mongodb', '@SaxulumWebProfilerProvider/Collector/mongodb.html.twig');
109
                    }
110
111
                    return $dataCollectorTemplates;
112
                }
113
            );
114
        }
115
    }
116
}
117