1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Limoncello\Crypt\Package; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Copyright 2015-2019 [email protected] |
7
|
|
|
* |
8
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
9
|
|
|
* you may not use this file except in compliance with the License. |
10
|
|
|
* You may obtain a copy of the License at |
11
|
|
|
* |
12
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
13
|
|
|
* |
14
|
|
|
* Unless required by applicable law or agreed to in writing, software |
15
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
16
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
17
|
|
|
* See the License for the specific language governing permissions and |
18
|
|
|
* limitations under the License. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
use Limoncello\Contracts\Application\ContainerConfiguratorInterface; |
22
|
|
|
use Limoncello\Contracts\Container\ContainerInterface as LimoncelloContainerInterface; |
23
|
|
|
use Limoncello\Contracts\Settings\SettingsProviderInterface; |
24
|
|
|
use Limoncello\Crypt\Contracts\DecryptInterface; |
25
|
|
|
use Limoncello\Crypt\Contracts\EncryptInterface; |
26
|
|
|
use Limoncello\Crypt\Exceptions\CryptConfigurationException; |
27
|
|
|
use Limoncello\Crypt\Package\AsymmetricCryptSettings as C; |
28
|
|
|
use Limoncello\Crypt\PrivateKeyAsymmetricEncrypt; |
29
|
|
|
use Limoncello\Crypt\PublicKeyAsymmetricDecrypt; |
30
|
|
|
use Psr\Container\ContainerInterface as PsrContainerInterface; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @package Limoncello\Crypt |
34
|
|
|
*/ |
35
|
|
View Code Duplication |
class AsymmetricPrivateEncryptPublicDecryptContainerConfigurator implements ContainerConfiguratorInterface |
|
|
|
|
36
|
|
|
{ |
37
|
|
|
/** @var callable */ |
38
|
|
|
const CONFIGURATOR = [self::class, self::CONTAINER_METHOD_NAME]; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @inheritdoc |
42
|
|
|
*/ |
43
|
2 |
|
public static function configureContainer(LimoncelloContainerInterface $container): void |
44
|
|
|
{ |
45
|
|
|
$container[EncryptInterface::class] = function (PsrContainerInterface $container): EncryptInterface { |
46
|
2 |
|
$settings = $container->get(SettingsProviderInterface::class)->get(C::class); |
47
|
2 |
|
$keyOrPath = $settings[C::KEY_PRIVATE_PATH_OR_KEY_VALUE] ?? null; |
48
|
2 |
|
if (empty($keyOrPath) === true) { |
49
|
1 |
|
throw new CryptConfigurationException(); |
50
|
|
|
} |
51
|
|
|
|
52
|
1 |
|
$crypt = new PrivateKeyAsymmetricEncrypt($keyOrPath); |
53
|
|
|
|
54
|
1 |
|
return $crypt; |
55
|
|
|
}; |
56
|
|
|
|
57
|
|
|
$container[DecryptInterface::class] = function (PsrContainerInterface $container): DecryptInterface { |
58
|
2 |
|
$settings = $container->get(SettingsProviderInterface::class)->get(C::class); |
59
|
2 |
|
$keyOrPath = $settings[C::KEY_PUBLIC_PATH_OR_KEY_VALUE] ?? null; |
60
|
2 |
|
if (empty($keyOrPath) === true) { |
61
|
1 |
|
throw new CryptConfigurationException(); |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
$crypt = new PublicKeyAsymmetricDecrypt($keyOrPath); |
65
|
|
|
|
66
|
1 |
|
return $crypt; |
67
|
|
|
}; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.