Test Failed
Push — master ( dde0ed...e2b526 )
by Francesco
03:04
created

DependencyInjection/MesCryptoExtension.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of the MesCryptoBundle package.
5
 *
6
 * (c) Francesco Cartenì <http://www.multimediaexperiencestudio.it/>
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 Mes\Security\CryptoBundle\DependencyInjection;
13
14
use Symfony\Component\Config\FileLocator;
15
use Symfony\Component\DependencyInjection\Alias;
16
use Symfony\Component\DependencyInjection\ContainerBuilder;
17
use Symfony\Component\DependencyInjection\Definition;
18
use Symfony\Component\DependencyInjection\Loader;
19
use Symfony\Component\DependencyInjection\Reference;
20
use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension;
21
22
/**
23
 * Class MesCryptoExtension.
24
 */
25
class MesCryptoExtension extends ConfigurableExtension
26
{
27
    /**
28
     * @param array            $config
29
     * @param ContainerBuilder $container
30
     *
31
     * @return Configuration
32
     */
33 5
    public function getConfiguration(array $config, ContainerBuilder $container)
34
    {
35 5
        return new Configuration();
36
    }
37
38
    /**
39
     * @codeCoverageIgnore
40
     *
41
     * @return string
42
     */
43
    public function getNamespace()
44
    {
45
        return 'http://multimediaexperiencestudio.it/schema/dic/crypto';
46
    }
47
48
    /**
49
     * @codeCoverageIgnore
50
     *
51
     * @return string|bool
52
     */
53
    public function getXsdValidationBasePath()
54
    {
55
        return __DIR__.'/../Resources/config/schema';
0 ignored issues
show
Bug Best Practice introduced by
The return type of return __DIR__ . '/../Resources/config/schema'; (string) is incompatible with the return type of the parent method Symfony\Component\Depend...etXsdValidationBasePath of type boolean.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
56
    }
57
58
    /**
59
     * Configures the passed container according to the merged configuration.
60
     *
61
     * @param array            $mergedConfig
62
     * @param ContainerBuilder $container
63
     */
64 5
    protected function loadInternal(array $mergedConfig, ContainerBuilder $container)
65
    {
66 5
        $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
67 5
        $loader->load('services.xml');
68
69 5
        $this->createKeyStorage($mergedConfig, $container);
70 5
        $this->createKeyGenerator($mergedConfig, $container);
71 5
        $this->generateKey($mergedConfig, $container);
72 5
        $this->createEncryption($mergedConfig, $container);
73 5
    }
74
75
    /**
76
     * @param $config
77
     * @param ContainerBuilder $container
78
     */
79 5
    private function createKeyStorage($config, ContainerBuilder $container)
80
    {
81
        // Key Storage
82 5
        if (null !== $config['key_storage']) {
83 1
            $container->setAlias(new Alias('mes_crypto.key_storage', false), $config['key_storage']);
84 1
        }
85 5
    }
86
87
    /**
88
     * @param $config
89
     * @param ContainerBuilder $container
90
     */
91 5
    private function createKeyGenerator($config, ContainerBuilder $container)
92
    {
93
        // Key Generator
94 5
        if (null !== $config['key_generator']) {
95 1
            $container->setAlias(new Alias('mes_crypto.key_generator', false), $config['key_generator']);
96 1
        }
97 5
    }
98
99
    /**
100
     * @param $config
101
     * @param ContainerBuilder $container
102
     */
103 5
    private function generateKey($config, ContainerBuilder $container)
104
    {
105 5
        $secret = $config['secret'];
106 5
        $ext_secret = $config['external_secret'];
107 5
        $key = $config['key']['key'];
108 5
        $path = $config['key']['path'];
109
        // Secret Factory Reference.
110 5
        $loadSecret = $this->getSecretFactoryReference();
111
        // Key Factory Reference.
112 5
        $loadKey = $this->getKeyFactoryReference();
113
114
        // Conditions.
115 5
        $createRandomKey = (null === $key) && (null === $path);
116 5
        $keyIsExternal = (true === !$createRandomKey) && (null !== $path) && (null === $key);
117 5
        $secretExists = (null !== $secret) || (true === $ext_secret);
118 5
        $secretIsExternal = (true === $secretExists) && (true === $ext_secret) && (null === $secret);
119
120 5
        $rawKeyDefinition = new Definition();
121 5
        $defuseKey = $secretExists ? 'Defuse\Crypto\KeyProtectedByPassword' : 'Defuse\Crypto\Key';
122
123
        // Creates a Key from an encoded version.
124 5
        if (!$createRandomKey) {
125 3
            if ($keyIsExternal) {
126
                // Sets the .crypto file path to CryptoLoader.
127 2
                $this->setCryptoLoaderResource($container, $path);
128 2
            }
129
130
            // Reads encoded key from configuration file if key is not external.
131 3
            $rawKeyDefinition->setClass($defuseKey)
132 3
                             ->setFactory(array(
133 3
                                 $defuseKey,
134 3
                                 'loadFromAsciiSafeString',
135 3
                             ));
136 3
            $rawKeyDefinition->setArguments(array(
137 3
                $keyIsExternal ? $loadKey : $key,
138 3
            ));
139 3
        } else {
140 2
            $rawKeyDefinition->setClass($defuseKey)
141 2
                             ->setArguments($secretExists ? array($secretIsExternal ? $loadSecret : $secret) : array())
142 2
                             ->setFactory($secretExists ? array(
143 1
                                 'Defuse\Crypto\KeyProtectedByPassword',
144 1
                                 'createRandomPasswordProtectedKey',
145 1
                             ) : array(
146 1
                                 'Defuse\Crypto\Key',
147 1
                                 'createNewRandomKey',
148 2
                             ));
149
        }
150
151 5
        if ($createRandomKey || (!$createRandomKey && !$keyIsExternal)) {
152 3
            $container->removeDefinition('mes_crypto.crypto_loader');
153 3
        }
154
155 5
        $container->setDefinition('mes_crypto.raw_key', $rawKeyDefinition)
156 5
                  ->setPublic(false);
157
158
        // Key
159 5
        $keyDefinition = new Definition('Mes\Security\CryptoBundle\Model\Key', array(
160 5
            new Reference('mes_crypto.raw_key'),
161 5
            $secretExists ? ($secretIsExternal ? $loadSecret : $secret) : null,
162 5
        ));
163 5
        $keyDefinition->setFactory(array(
164 5
            'Mes\Security\CryptoBundle\Model\Key',
165 5
            'create',
166 5
        ));
167 5
        $container->setDefinition('mes_crypto.key', $keyDefinition)
168 5
                  ->setPublic(false);
169
170 5
        $keyManagerDefinition = $container->getDefinition('mes_crypto.key_manager_wrapper');
171
172
        // Save the generated key.
173 5
        $keyManagerDefinition->addMethodCall('setKey', array(new Reference('mes_crypto.key')));
174
175
        // Save the secret.
176 5
        if ($secretExists) {
177 4
            $keyManagerDefinition->addMethodCall('setSecret', array(
178 4
                $secretExists ? ($secretIsExternal ? $loadSecret : $secret) : null,
179 4
            ));
180 4
        }
181 5
    }
182
183
    /**
184
     * @param $config
185
     * @param ContainerBuilder $container
186
     */
187 5
    private function createEncryption($config, ContainerBuilder $container)
188
    {
189 5
        if (null !== $config['encryption']) {
190 1
            $container->setAlias(new Alias('mes_crypto.encryption'), $config['encryption']);
191 1
        }
192 5
    }
193
194
    /**
195
     * @param ContainerBuilder $container
196
     * @param $resource
197
     *
198
     * @return Definition
199
     */
200 2
    private function setCryptoLoaderResource(ContainerBuilder $container, $resource)
201
    {
202 2
        $container->getDefinition('mes_crypto.crypto_loader')
203 2
                  ->replaceArgument(0, $resource);
204 2
    }
205
206
    /**
207
     * @return Reference
208
     */
209 5
    private function getSecretFactoryReference()
210
    {
211
        // Returns secret factory Reference.
212 5
        return new Reference('mes_crypto.secret_factory');
213
    }
214
215
    /**
216
     * @return Reference
217
     */
218 5
    private function getKeyFactoryReference()
219
    {
220
        // Returns key factory Reference.
221 5
        return new Reference('mes_crypto.key_factory');
222
    }
223
}
224