ChannelChangerFactory::__invoke()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 8
cts 8
cp 1
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace WShafer\PSR11MonoLog;
4
5
use Psr\Container\ContainerInterface;
6
use WShafer\PSR11MonoLog\Config\MainConfig;
7
use WShafer\PSR11MonoLog\Formatter\FormatterMapper;
8
use WShafer\PSR11MonoLog\Handler\HandlerMapper;
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 7
    public function getMainConfig(ContainerInterface $container)
38
    {
39 7
        $config = $this->getConfigArray($container);
40 7
        return new MainConfig($config);
41
    }
42
43 7
    protected function getConfigArray(ContainerInterface $container)
44
    {
45
        // Symfony config is parameters. //
46
        if (
47 7
            method_exists($container, 'getParameter')
48 7
            && method_exists($container, 'hasParameter')
49 7
            && $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, Symfony_DI_PhpDumper_Test_Unsupported_Characters.

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...
50
        ) {
51 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, Symfony_DI_PhpDumper_Test_Unsupported_Characters.

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