|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* The MIT License (MIT) |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (c) 2014-2016 Spomky-Labs |
|
7
|
|
|
* |
|
8
|
|
|
* This software may be modified and distributed under the terms |
|
9
|
|
|
* of the MIT license. See the LICENSE file for details. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace SpomkyLabs\JoseBundle\Service; |
|
13
|
|
|
|
|
14
|
|
|
use Jose\Factory\DecrypterFactory; |
|
15
|
|
|
use Jose\Factory\EncrypterFactory; |
|
16
|
|
|
use Jose\Factory\SignerFactory; |
|
17
|
|
|
use Jose\Factory\VerifierFactory; |
|
18
|
|
|
use Psr\Log\LoggerInterface; |
|
19
|
|
|
|
|
20
|
|
|
final class ServiceFactory |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var \SpomkyLabs\JoseBundle\Service\AlgorithmManager |
|
24
|
|
|
*/ |
|
25
|
|
|
private $algorithm_manager; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var \SpomkyLabs\JoseBundle\Service\CompressionManager |
|
29
|
|
|
*/ |
|
30
|
|
|
private $compression_manager; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* ServiceFactory constructor. |
|
34
|
|
|
* |
|
35
|
|
|
* @param \SpomkyLabs\JoseBundle\Service\AlgorithmManager $algorithm_manager |
|
36
|
|
|
* @param \SpomkyLabs\JoseBundle\Service\CompressionManager $compression_manager |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct(AlgorithmManager $algorithm_manager, CompressionManager $compression_manager) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->algorithm_manager = $algorithm_manager; |
|
41
|
|
|
$this->compression_manager = $compression_manager; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param string[] $selected_algorithms |
|
46
|
|
|
* @param string[] $selected_compression_methods |
|
47
|
|
|
* @param \Psr\Log\LoggerInterface|null $logger |
|
48
|
|
|
* |
|
49
|
|
|
* @return \Jose\EncrypterInterface |
|
50
|
|
|
*/ |
|
51
|
|
View Code Duplication |
public function createEncrypter(array $selected_algorithms, array $selected_compression_methods, LoggerInterface $logger = null) |
|
|
|
|
|
|
52
|
|
|
{ |
|
53
|
|
|
$algorithms = $this->algorithm_manager->getSelectedAlgorithmMethods($selected_algorithms); |
|
54
|
|
|
$compression_methods = $this->compression_manager->getSelectedCompressionMethods($selected_compression_methods); |
|
55
|
|
|
|
|
56
|
|
|
return EncrypterFactory::createEncrypter($algorithms, $compression_methods, $logger); |
|
|
|
|
|
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param string[] $selected_algorithms |
|
61
|
|
|
* @param string[] $selected_compression_methods |
|
62
|
|
|
* @param \Psr\Log\LoggerInterface|null $logger |
|
63
|
|
|
* |
|
64
|
|
|
* @return \Jose\DecrypterInterface |
|
65
|
|
|
*/ |
|
66
|
|
View Code Duplication |
public function createDecrypter(array $selected_algorithms, array $selected_compression_methods, LoggerInterface $logger = null) |
|
|
|
|
|
|
67
|
|
|
{ |
|
68
|
|
|
$algorithms = $this->algorithm_manager->getSelectedAlgorithmMethods($selected_algorithms); |
|
69
|
|
|
$compression_methods = $this->compression_manager->getSelectedCompressionMethods($selected_compression_methods); |
|
70
|
|
|
|
|
71
|
|
|
return DecrypterFactory::createDecrypter($algorithms, $compression_methods, $logger); |
|
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param string[] $selected_algorithms |
|
76
|
|
|
* @param \Psr\Log\LoggerInterface|null $logger |
|
77
|
|
|
* |
|
78
|
|
|
* @return \Jose\SignerInterface |
|
79
|
|
|
*/ |
|
80
|
|
|
public function createSigner(array $selected_algorithms, LoggerInterface $logger = null) |
|
81
|
|
|
{ |
|
82
|
|
|
$algorithms = $this->algorithm_manager->getSelectedAlgorithmMethods($selected_algorithms); |
|
83
|
|
|
|
|
84
|
|
|
return SignerFactory::createSigner($algorithms, $logger); |
|
|
|
|
|
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param string[] $selected_algorithms |
|
89
|
|
|
* @param \Psr\Log\LoggerInterface|null $logger |
|
90
|
|
|
* |
|
91
|
|
|
* @return \Jose\VerifierInterface |
|
92
|
|
|
*/ |
|
93
|
|
|
public function createVerifier(array $selected_algorithms, LoggerInterface $logger = null) |
|
94
|
|
|
{ |
|
95
|
|
|
$algorithms = $this->algorithm_manager->getSelectedAlgorithmMethods($selected_algorithms); |
|
96
|
|
|
|
|
97
|
|
|
return VerifierFactory::createVerifier($algorithms, $logger); |
|
|
|
|
|
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
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.