FileRegister::registerConfigCache()   B
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 19
cts 19
cp 1
rs 8.9713
c 0
b 0
f 0
cc 3
eloc 17
nc 3
nop 1
crap 3
1
<?php
2
3
/*
4
 * This file is part of the ConfigCacheBundle package.
5
 *
6
 * Copyright (c) 2015-2016 Yahoo Japan Corporation
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace YahooJapan\ConfigCacheBundle\ConfigCache\Register;
13
14
use Symfony\Component\Config\Resource\FileResource as BaseFileResource;
15
use Symfony\Component\DependencyInjection\Reference;
16
use YahooJapan\ConfigCacheBundle\ConfigCache\Resource\FileResource;
17
use YahooJapan\ConfigCacheBundle\ConfigCache\Resource\ResourceInterface;
18
19
/**
20
 * FileRegister registers the ConfigCache services with FileResource.
21
 */
22
class FileRegister
23
{
24
    protected $register;
25
    protected $resources = array();
26
27
    /**
28
     * Constructor.
29
     *
30
     * @param ServiceRegister $register
31
     */
32 73
    public function __construct(ServiceRegister $register)
33
    {
34 73
        $this->register = $register;
35 73
    }
36
37
    /**
38
     * Registers ConfigCache or Configuration services
39
     */
40 19
    public function register()
41
    {
42 19
        foreach ($this->resources as $resource) {
43 17
            if ($resource->hasAlias()) {
44 14
                $this->registerConfigCache($resource);
45 14
            } else {
46 14
                $this->registerConfiguration($resource);
47
            }
48 19
        }
49 18
    }
50
51
    /**
52
     * Adds a FileResource.
53
     *
54
     * @param FileResource $resource
55
     *
56
     * @return FileRegister
57
     */
58 28
    public function add(FileResource $resource)
59
    {
60 28
        $this->resources[] = $resource;
61
62 28
        return $this;
63
    }
64
65
    /**
66
     * Whether the resource is enabled or not.
67
     *
68
     * @param ResourceInterface $resource
69
     *
70
     * @return bool
71
     */
72 26
    public function enabled(ResourceInterface $resource)
73
    {
74 26
        return $resource->exists() && $this->isFileResource($resource);
75
    }
76
77
    /**
78
     * Whether the resource is FileResource and has Alias or not.
79
     *
80
     * @param ResourceInterface $resource
81
     *
82
     * @return bool
83
     */
84 20
    public function hasAlias(ResourceInterface $resource)
85
    {
86 20
        return $this->isFileResource($resource) && $resource->hasAlias();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface YahooJapan\ConfigCacheBu...ource\ResourceInterface as the method hasAlias() does only exist in the following implementations of said interface: YahooJapan\ConfigCacheBu...e\Resource\FileResource.

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...
87
    }
88
89
    /**
90
     * Whether the Register has a FileResource without alias or not.
91
     *
92
     * @return bool
93
     */
94 39
    public function hasNoAlias()
95
    {
96 39
        foreach ($this->resources as $resource) {
97 28
            if (!$resource->hasAlias()) {
98 21
                return true;
99
            }
100 29
        }
101
102 28
        return false;
103
    }
104
105
    /**
106
     * Whether the resource is FileResource or not.
107
     *
108
     * @param ResourceInterface $resource
109
     *
110
     * @return bool
111
     */
112 34
    protected function isFileResource(ResourceInterface $resource)
113
    {
114 34
        return $resource instanceof FileResource;
115
    }
116
117
    /**
118
     * Registers a ConfigCache service.
119
     *
120
     * @param FileResource $resource
121
     *
122
     * @throws \RuntimeException throws if the ConfigCache service ID already exists
123
     */
124 14
    protected function registerConfigCache(FileResource $resource)
125
    {
126 14
        $alias = $resource->getAlias();
127 14
        $path  = $resource->getResource();
128 14
        $standaloneCacheId = $this->register->getIdBuilder()->buildCacheId(array($alias));
129 14
        $container = $this->register->getContainer();
130 14
        if ($container->hasDefinition($standaloneCacheId)) {
131 1
            throw new \RuntimeException(
132 1
                "{$standaloneCacheId} is already registered. Maybe FileResource alias[{$alias}] is duplicated."
133 1
            );
134
        }
135
136 14
        $container->addResource(new BaseFileResource($path));
137 14
        if ($resource->isRestorable()) {
138 1
            $this->register->registerRestorableConfigCache($alias);
139 1
        } else {
140 13
            $this->register->registerConfigCacheByAlias($alias);
141
        }
142 14
        $container->findDefinition($standaloneCacheId)
143 14
            ->addMethodCall('addResource', array((string) $path))
144 14
            ->addMethodCall('setStrict', array(false))
145 14
            ->addMethodCall('setId', array($alias))
146
            ;
147 14
    }
148
149
    /**
150
     * Registers a Configuration service.
151
     *
152
     * @param FileResource $resource
153
     */
154 14
    protected function registerConfiguration(FileResource $resource)
155
    {
156 14
        $container = $this->register->getContainer();
157 14
        $container->addResource(new BaseFileResource($resource->getResource()));
158
159
        // private configuration definition, finally discarded because of private service
160 14
        $idBuilder     = $this->register->getIdBuilder();
161 14
        $configuration = $this->register->getConfiguration();
162 14
        $privateId     = $idBuilder->buildConfigurationId($configuration->find($resource));
163 14
        $this->register->registerConfiguration($privateId, $configuration->find($resource));
164
165 14
        $container->findDefinition($idBuilder->buildCacheId())
166 14
            ->addMethodCall(
167 14
                'addResource',
168 14
                array((string) $resource->getResource(), new Reference($privateId))
169 14
            )
170
            ;
171 14
    }
172
}
173