Completed
Push — master ( 948998...4f0b2f )
by Jaap
13s
created

canCreateServiceWithName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 3
crap 1
1
<?php
2
/**
3
 * Polder Knowledge / log-module (https://polderknowledge.com)
4
 *
5
 * @link https://github.com/polderknowledge/log-module for the canonical source repository
6
 * @copyright Copyright (c) 2016-2017 Polder Knowledge (https://polderknowledge.com)
7
 * @license https://github.com/polderknowledge/log-module/blob/master/LICENSE.md MIT
8
 */
9
10
namespace PolderKnowledge\LogModule\Factory;
11
12
use Interop\Container\ContainerInterface;
13
use WShafer\PSR11MonoLog\ChannelChanger;
14
use Zend\Log\Logger as ZendLogger;
15
use Zend\Log\Writer\Psr;
16
use Zend\ServiceManager\AbstractFactoryInterface;
17
use Zend\ServiceManager\ServiceLocatorInterface;
18
19
final class LoggerAbstractServiceFactory implements AbstractFactoryInterface
0 ignored issues
show
Deprecated Code introduced by
The interface Zend\ServiceManager\AbstractFactoryInterface has been deprecated with message: Use Zend\ServiceManager\Factory\AbstractFactoryInterface instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
20
{
21 12
    public function canCreate(ContainerInterface $container, $requestedName)
22
    {
23 12
        $channelChanger = $container->get(ChannelChanger::class);
24
25 12
        return $channelChanger->has($requestedName);
26
    }
27
28 21
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
29
    {
30 21
        $channelChanger = $container->get(ChannelChanger::class);
31
32 21
        $channel = $channelChanger->get($requestedName);
33
34 21
        if (!$channel) {
35 3
            return null;
36
        }
37
38
        /** @var array $config */
39 18
        $config = $container->get('config');
40
41 18
        if ($this->isZendLogger($config['monolog']['channels'], $requestedName)) {
42 6
            $zendLogger = new ZendLogger();
43 6
            $zendLogger->addWriter(new Psr($channel));
44
45 6
            $channel = $zendLogger;
46
        }
47
48 18
        return $channel;
49
    }
50
51 18
    private function isZendLogger(array $channels, $requestedName)
52
    {
53 18
        if (!array_key_exists('zend-log', $channels[$requestedName])) {
54 6
            return false;
55
        }
56
57 12
        return $channels[$requestedName]['zend-log'] === true;
58
    }
59
60
    /**
61
     * Determine if we can create a service with name
62
     *
63
     * @param ServiceLocatorInterface $serviceLocator
64
     * @param $name
65
     * @param $requestedName
66
     * @return bool
67
     */
68 3
    public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
69
    {
70 3
        return $this->canCreate($serviceLocator, $requestedName);
71
    }
72
73
    /**
74
     * Create service with name
75
     *
76
     * @param ServiceLocatorInterface $serviceLocator
77
     * @param $name
78
     * @param $requestedName
79
     * @return mixed
80
     */
81 9
    public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
82
    {
83 9
        return $this($serviceLocator, $requestedName);
84
    }
85
}
86