Completed
Push — master ( d87453...fe04c7 )
by Westin
03:28
created

ChannelChangerFactory::getConfigArray()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 22
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 10
cts 10
cp 1
rs 8.6737
c 0
b 0
f 0
cc 6
eloc 10
nc 4
nop 1
crap 6
1
<?php
2
3
namespace WShafer\PSR11MonoLog;
4
5
use Psr\Container\ContainerInterface;
6
use WShafer\PSR11MonoLog\Config\MainConfig;
7
use WShafer\PSR11MonoLog\Config\MainConfigFactory;
8
use WShafer\PSR11MonoLog\Formatter\FormatterMapper;
9
use WShafer\PSR11MonoLog\Processor\ProcessorMapper;
10
use WShafer\PSR11MonoLog\Service\FormatterManager;
11
use WShafer\PSR11MonoLog\Service\HandlerManager;
12
use WShafer\PSR11MonoLog\Service\ProcessorManager;
13
14
class ChannelChangerFactory
15
{
16
    protected $config = null;
17
18
    protected $handlerManager = null;
19
20
    protected $processManager = null;
21
22
    protected $formatterManager = null;
23
24 1
    public function __invoke(ContainerInterface $container)
25
    {
26 1
        $config = $this->getMainConfig($container);
27 1
        $handlerManager = $this->getHandlerManager($container);
28 1
        $processorManager = $this->getProcessorManager($container);
29
30 1
        return new ChannelChanger(
31 1
            $config,
32 1
            $handlerManager,
33 1
            $processorManager
34
        );
35
    }
36
37 8
    public function getMainConfig(ContainerInterface $container)
38
    {
39 8
        $config = $this->getConfigArray($container);
40 8
        return new MainConfig($config);
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')
1 ignored issue
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\DependencyInjection\Container, Symfony\Component\Depend...ection\ContainerBuilder, Symfony\Component\Depend...tainers\CustomContainer, Symfony\Component\Depend...ProjectServiceContainer, Symfony\Component\Depend...ProjectServiceContainer.

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')];
1 ignored issue
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\DependencyInjection\Container, Symfony\Component\Depend...ection\ContainerBuilder, Symfony\Component\Depend...tainers\CustomContainer, Symfony\Component\Depend...ProjectServiceContainer, Symfony\Component\Depend...ProjectServiceContainer.

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 5
            return $container->get('config');
56
        }
57
58
        // Slim Config comes from "settings"
59 2
        if ($container->has('settings')) {
60 1
            return ['monolog' => $container->get('settings')['monolog']];
61
        }
62
63 1
        return [];
64
    }
65
66 2
    public function getHandlerManager(ContainerInterface $container)
67
    {
68 2
        $config = $this->getMainConfig($container);
69 2
        $this->handlerManager = new HandlerManager(
70 2
            $config,
71 2
            new FormatterMapper(),
72 2
            $container
73
        );
74
75 2
        $this->handlerManager->setFormatterManager($this->getFormatterManager($container));
76 2
        return $this->handlerManager;
77
    }
78
79 3
    public function getFormatterManager(ContainerInterface $container)
80
    {
81 3
        $config = $this->getMainConfig($container);
82 3
        $this->formatterManager = new FormatterManager(
83 3
            $config,
84 3
            new FormatterMapper(),
85 3
            $container
86
        );
87
88 3
        return $this->formatterManager;
89
    }
90
91 2
    public function getProcessorManager(ContainerInterface $container)
92
    {
93 2
        $config = $this->getMainConfig($container);
94 2
        $this->processManager = new ProcessorManager(
95 2
            $config,
96 2
            new ProcessorMapper(),
97 2
            $container
98
        );
99
100 2
        return $this->processManager;
101
    }
102
}
103