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

ProfilingAdapterFactory::createProfiler()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
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