Completed
Pull Request — master (#5)
by Chris
08:31
created

FlySystemManagerFactory::getAdaptorMapper()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

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
nc 1
nop 1
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace WShafer\PSR11FlySystem;
5
6
use Psr\Container\ContainerInterface;
7
use WShafer\PSR11FlySystem\Adaptor\AdaptorMapper;
8
use WShafer\PSR11FlySystem\Cache\CacheMapper;
9
use WShafer\PSR11FlySystem\Config\MainConfig;
10
use WShafer\PSR11FlySystem\Service\AdaptorManager;
11
use WShafer\PSR11FlySystem\Service\CacheManager;
12
13
class FlySystemManagerFactory
14
{
15
    protected $config;
16
17 1
    public function __invoke(ContainerInterface $container)
18
    {
19
        /** @var MainConfig $config */
20 1
        $config = $this->getConfig($container);
21
22
        /** @var AdaptorManager $adaptorManager */
23 1
        $adaptorManager = $this->getAdaptorManager($container);
24
25
        /** @var CacheManager $cacheManager */
26 1
        $cacheManager = $this->getCacheManager($container);
27
28 1
        return new FlySystemManager($config, $adaptorManager, $cacheManager, $container);
29
    }
30
31 7
    public function getConfig(ContainerInterface $container)
32
    {
33 7
        if (!$this->config) {
34 7
            $config = $this->getConfigArray($container);
35 7
            $this->config = new MainConfig($config);
36
        }
37
38 6
        return $this->config;
39
    }
40
41 7
    protected function getConfigArray(ContainerInterface $container)
42
    {
43
        // Symfony config is parameters. //
44 7
        if (method_exists($container, 'getParameter')
45 7
            && method_exists($container, 'hasParameter')
46 7
            && $container->hasParameter('flysystem')
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...
47
        ) {
48 1
            return ['flysystem' => $container->getParameter('flysystem')];
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...
49
        }
50
51
        // Zend uses config key
52 6
        if ($container->has('config')) {
53 4
            $config = $container->get('config');
54
            
55 4
            if ($config instanceof \ArrayObject) {
56
                return $config->getArrayCopy();
57
            }
58
            
59 4
            return $config;
60
        }
61
62
        // Slim Config comes from "settings"
63 2
        if ($container->has('settings')) {
64 1
            return ['flysystem' => $container->get('settings')['flysystem']];
65
        }
66
67 1
        return [];
68
    }
69
70 2
    public function getAdaptorManager(ContainerInterface $container)
71
    {
72
        /** @var MainConfig $config */
73 2
        $config = $this->getConfig($container);
74
75
        /** @var AdaptorMapper $adaptorMapper */
76 2
        $adaptorMapper = $this->getAdaptorMapper($container);
77
78 2
        return new AdaptorManager($config, $adaptorMapper, $container);
79
    }
80
81 3
    public function getAdaptorMapper(ContainerInterface $container)
82
    {
83 3
        return new AdaptorMapper($container);
84
    }
85
86 2
    public function getCacheManager(ContainerInterface $container)
87
    {
88
        /** @var MainConfig $config */
89 2
        $config = $this->getConfig($container);
90
91
        /** @var CacheMapper $cacheMapper */
92 2
        $cacheMapper = $this->getCacheMapper($container);
93
94 2
        return new CacheManager($config, $cacheMapper, $container);
95
    }
96
97 3
    public function getCacheMapper(ContainerInterface $container)
98
    {
99 3
        return new CacheMapper($container);
100
    }
101
}
102