Completed
Pull Request — master (#9)
by Koldo
02:13
created

ChannelChangerFactory::getZendConfigArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace WShafer\PSR11MonoLog;
4
5
use ArrayObject;
6
use Psr\Container\ContainerInterface;
7
use WShafer\PSR11MonoLog\Config\MainConfig;
8
use WShafer\PSR11MonoLog\Formatter\FormatterMapper;
9
use WShafer\PSR11MonoLog\Handler\HandlerMapper;
10
use WShafer\PSR11MonoLog\Processor\ProcessorMapper;
11
use WShafer\PSR11MonoLog\Service\FormatterManager;
12
use WShafer\PSR11MonoLog\Service\HandlerManager;
13
use WShafer\PSR11MonoLog\Service\ProcessorManager;
14
15
class ChannelChangerFactory
16
{
17
    protected $config = null;
18
19
    protected $handlerManager = null;
20
21
    protected $processManager = null;
22
23
    protected $formatterManager = null;
24
25 1
    public function __invoke(ContainerInterface $container)
26
    {
27 1
        $config = $this->getMainConfig($container);
28 1
        $handlerManager = $this->getHandlerManager($container);
29 1
        $processorManager = $this->getProcessorManager($container);
30
31 1
        return new ChannelChanger(
32 1
            $config,
33 1
            $handlerManager,
34 1
            $processorManager
35
        );
36
    }
37
38 8
    public function getMainConfig(ContainerInterface $container)
39
    {
40 8
        return new MainConfig($this->getConfigArray($container));
41
    }
42
43 8
    protected function getConfigArray(ContainerInterface $container)
44
    {
45
        // Symfony config is parameters. //
46 8
        if (method_exists($container, 'getParameter')
47 8
            && method_exists($container, 'hasParameter')
48 8
            && $container->hasParameter('monolog')
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Psr\Container\ContainerInterface as the method hasParameter() does only exist in the following implementations of said interface: Container14\ProjectServiceContainer, ProjectServiceContainer, Symfony\Component\Depend...urationContainerBuilder, Symfony\Component\DependencyInjection\Container, Symfony\Component\Depend...ection\ContainerBuilder, Symfony\Component\Depend...\NoConstructorContainer, Symfony\Component\Depend...tainers\CustomContainer, Symfony\Component\Depend...ProjectServiceContainer, Symfony\Component\Depend...ProjectServiceContainer, Symfony_DI_PhpDumper_Test_Almost_Circular_Private, Symfony_DI_PhpDumper_Test_Almost_Circular_Public, Symfony_DI_PhpDumper_Test_Base64Parameters, Symfony_DI_PhpDumper_Test_Deep_Graph, Symfony_DI_PhpDumper_Test_EnvParameters, Symfony_DI_PhpDumper_Test_Inline_Self_Ref, Symfony_DI_PhpDumper_Test_Legacy_Privates, Symfony_DI_PhpDumper_Test_Rot13Parameters, Symfony_DI_PhpDumper_Test_Uninitialized_Reference.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
49
        ) {
50 1
            return ['monolog' => $container->getParameter('monolog')];
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Psr\Container\ContainerInterface as the method getParameter() does only exist in the following implementations of said interface: Container14\ProjectServiceContainer, ProjectServiceContainer, Symfony\Component\Depend...urationContainerBuilder, Symfony\Component\DependencyInjection\Container, Symfony\Component\Depend...ection\ContainerBuilder, Symfony\Component\Depend...\NoConstructorContainer, Symfony\Component\Depend...tainers\CustomContainer, Symfony\Component\Depend...ProjectServiceContainer, Symfony\Component\Depend...ProjectServiceContainer, Symfony_DI_PhpDumper_Test_Almost_Circular_Private, Symfony_DI_PhpDumper_Test_Almost_Circular_Public, Symfony_DI_PhpDumper_Test_Base64Parameters, Symfony_DI_PhpDumper_Test_Deep_Graph, Symfony_DI_PhpDumper_Test_EnvParameters, Symfony_DI_PhpDumper_Test_Inline_Self_Ref, Symfony_DI_PhpDumper_Test_Legacy_Privates, Symfony_DI_PhpDumper_Test_Rot13Parameters, Symfony_DI_PhpDumper_Test_Uninitialized_Reference.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
51
        }
52
53
        // Zend uses config key
54 7
        if ($container->has('config')) {
55 6
            return $this->getZendConfigArray($container);
56
        }
57
58
        // Slim Config comes from "settings"
59 1
        if ($container->has('settings')) {
60 1
            return ['monolog' => $container->get('settings')['monolog']];
61
        }
62
63
        return [];
64
    }
65
66 6
    protected function getZendConfigArray(ContainerInterface $container)
67
    {
68 6
        $config = $container->get('config');
69 6
        if ($config instanceof ArrayObject) {
70 1
            $config = $config->getArrayCopy();
71
        }
72
73 6
        return $config;
74
    }
75
76 2
    public function getHandlerManager(ContainerInterface $container)
77
    {
78 2
        $config = $this->getMainConfig($container);
79 2
        $this->handlerManager = new HandlerManager(
80 2
            $config,
81 2
            new HandlerMapper(),
82 2
            $container
83
        );
84
85 2
        $this->handlerManager->setFormatterManager($this->getFormatterManager($container));
86 2
        $this->handlerManager->setProcessorManager($this->getProcessorManager($container));
87 2
        return $this->handlerManager;
88
    }
89
90 3
    public function getFormatterManager(ContainerInterface $container)
91
    {
92 3
        $config = $this->getMainConfig($container);
93 3
        $this->formatterManager = new FormatterManager(
94 3
            $config,
95 3
            new FormatterMapper(),
96 3
            $container
97
        );
98
99 3
        return $this->formatterManager;
100
    }
101
102 3
    public function getProcessorManager(ContainerInterface $container)
103
    {
104 3
        $config = $this->getMainConfig($container);
105 3
        $this->processManager = new ProcessorManager(
106 3
            $config,
107 3
            new ProcessorMapper(),
108 3
            $container
109
        );
110
111 3
        return $this->processManager;
112
    }
113
}
114