Completed
Push — develop ( 2f1dbd...8bd922 )
by Peter
02:12 queued 20s
created

ProfilingAdapterFactory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
dl 0
loc 39
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A createService() 0 16 4
A createProfiler() 0 12 2
1
<?php
2
3
namespace WebinoDev\Factory;
4
5
use BjyProfiler\Db\Adapter\ProfilingAdapter;
6
use BjyProfiler\Db\Profiler\LoggingProfiler;
7
use BjyProfiler\Db\Profiler\Profiler;
8
use Zend\Db\Adapter\Adapter;
9
use Zend\Log;
10
use Zend\ServiceManager\FactoryInterface;
11
use Zend\ServiceManager\ServiceLocatorInterface;
12
13
/**
14
 * Class ProfilingAdapterFactory
15
 */
16
class ProfilingAdapterFactory implements FactoryInterface
17
{
18
    /**
19
     * @param ServiceLocatorInterface $services
20
     * @return ProfilingAdapter|bool|Adapter
21
     */
22
    public function createService(ServiceLocatorInterface $services)
23
    {
24
        $config = $services->get('Config');
25
        if (empty($config['db'])) {
26
            return false;
27
        }
28
29
        if (!class_exists(ProfilingAdapter::class)) {
30
            return new Adapter($config['db']);
31
        }
32
33
        $adapter = new ProfilingAdapter($config['db']);
34
        $adapter->setProfiler($this->createProfiler());
35
        $adapter->injectProfilingStatementPrototype(!empty($config['db']['options']) ? $config['db'] : []);
36
        return $adapter;
37
    }
38
39
    /**
40
     * @return Profiler|LoggingProfiler
41
     */
42
    protected function createProfiler()
43
    {
44
        if ('cli' === PHP_SAPI) {
45
            // In CLI mode write queries
46
            // profiling info to stdout
47
            $logger = new Log\Logger;
48
            $logger->addWriter(new Log\Writer\Stream('php://output'), Log\Logger::DEBUG);
49
            return new LoggingProfiler($logger);
50
        }
51
52
        return new Profiler;
53
    }
54
}
55