Completed
Push — develop ( 17d645...b57b7f )
by Florent
02:19
created

ServiceFactory   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 138
Duplicated Lines 11.59 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 3
Bugs 0 Features 2
Metric Value
wmc 10
c 3
b 0
f 2
lcom 2
cbo 4
dl 16
loc 138
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A createEncrypter() 8 8 1
A createDecrypter() 8 8 1
A createSigner() 0 6 1
A createVerifier() 0 6 1
A createChecker() 0 7 1
A createJWTLoader() 0 9 2
A createJWTCreator() 0 9 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Checker\CheckerManagerInterface;
15
use Jose\DecrypterInterface;
16
use Jose\EncrypterInterface;
17
use Jose\Factory\CheckerManagerFactory;
18
use Jose\Decrypter;
19
use Jose\Encrypter;
20
use Jose\JWTCreator;
21
use Jose\JWTLoader;
22
use Jose\Signer;
23
use Jose\SignerInterface;
24
use Jose\Verifier;
25
use Jose\VerifierInterface;
26
use Psr\Log\LoggerInterface;
27
28
final class ServiceFactory
29
{
30
    /**
31
     * @var \SpomkyLabs\JoseBundle\Service\AlgorithmManager
32
     */
33
    private $algorithm_manager;
34
35
    /**
36
     * @var \SpomkyLabs\JoseBundle\Service\CompressionManager
37
     */
38
    private $compression_manager;
39
40
    /**
41
     * @var \SpomkyLabs\JoseBundle\Service\CheckerManager
42
     */
43
    private $checker_manager;
44
45
    /**
46
     * ServiceFactory constructor.
47
     *
48
     * @param \SpomkyLabs\JoseBundle\Service\AlgorithmManager   $algorithm_manager
49
     * @param \SpomkyLabs\JoseBundle\Service\CompressionManager $compression_manager
50
     * @param \SpomkyLabs\JoseBundle\Service\CheckerManager     $checker_manager
51
     */
52
    public function __construct(AlgorithmManager $algorithm_manager, CompressionManager $compression_manager, CheckerManager $checker_manager)
53
    {
54
        $this->algorithm_manager = $algorithm_manager;
55
        $this->compression_manager = $compression_manager;
56
        $this->checker_manager = $checker_manager;
57
    }
58
59
    /**
60
     * @param string[]                      $selected_key_encryption_algorithms
61
     * @param string[]                      $selected_content_encryption_algorithms
62
     * @param string[]                      $selected_compression_methods
63
     * @param \Psr\Log\LoggerInterface|null $logger
64
     *
65
     * @return \Jose\EncrypterInterface
66
     */
67 View Code Duplication
    public function createEncrypter(array $selected_key_encryption_algorithms, array $selected_content_encryption_algorithms, array $selected_compression_methods, LoggerInterface $logger = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
68
    {
69
        $key_encryption_algorithms = $this->algorithm_manager->getSelectedAlgorithmMethods($selected_key_encryption_algorithms);
70
        $content_encryption_algorithms = $this->algorithm_manager->getSelectedAlgorithmMethods($selected_content_encryption_algorithms);
71
        $compression_methods = $this->compression_manager->getSelectedCompressionMethods($selected_compression_methods);
72
73
        return Encrypter::createEncrypter($key_encryption_algorithms, $content_encryption_algorithms, $compression_methods, $logger);
0 ignored issues
show
Bug introduced by
The method createEncrypter() does not exist on Jose\Encrypter. Did you maybe mean encrypt()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
74
    }
75
76
    /**
77
     * @param string[]                      $selected_key_encryption_algorithms
78
     * @param string[]                      $selected_content_encryption_algorithms
79
     * @param \Psr\Log\LoggerInterface|null $logger
80
     *
81
     * @return \Jose\DecrypterInterface
82
     */
83 View Code Duplication
    public function createDecrypter(array $selected_key_encryption_algorithms, array $selected_content_encryption_algorithms, array $selected_compression_methods, LoggerInterface $logger = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
84
    {
85
        $key_encryption_algorithms = $this->algorithm_manager->getSelectedAlgorithmMethods($selected_key_encryption_algorithms);
86
        $content_encryption_algorithms = $this->algorithm_manager->getSelectedAlgorithmMethods($selected_content_encryption_algorithms);
87
        $compression_methods = $this->compression_manager->getSelectedCompressionMethods($selected_compression_methods);
88
89
        return Decrypter::createDecrypter($key_encryption_algorithms, $content_encryption_algorithms, $compression_methods, $logger);
0 ignored issues
show
Bug introduced by
The method createDecrypter() does not seem to exist on object<Jose\Decrypter>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
90
    }
91
92
    /**
93
     * @param string[]                      $selected_algorithms
94
     * @param \Psr\Log\LoggerInterface|null $logger
95
     *
96
     * @return \Jose\SignerInterface
97
     */
98
    public function createSigner(array $selected_algorithms, LoggerInterface $logger = null)
99
    {
100
        $algorithms = $this->algorithm_manager->getSelectedAlgorithmMethods($selected_algorithms);
101
102
        return Signer::createSigner($algorithms, $logger);
0 ignored issues
show
Bug introduced by
The method createSigner() does not seem to exist on object<Jose\Signer>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
103
    }
104
105
    /**
106
     * @param string[]                      $selected_algorithms
107
     * @param \Psr\Log\LoggerInterface|null $logger
108
     *
109
     * @return \Jose\VerifierInterface
110
     */
111
    public function createVerifier(array $selected_algorithms, LoggerInterface $logger = null)
112
    {
113
        $algorithms = $this->algorithm_manager->getSelectedAlgorithmMethods($selected_algorithms);
114
        
115
        return Verifier::createVerifier($algorithms, $logger);
0 ignored issues
show
Bug introduced by
The method createVerifier() does not seem to exist on object<Jose\Verifier>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
116
    }
117
118
    /**
119
     * @param string[] $selected_claim_checkers
120
     * @param string[] $selected_header_checkers
121
     *
122
     * @return \Jose\Checker\CheckerManagerInterface
123
     */
124
    public function createChecker(array $selected_claim_checkers, array $selected_header_checkers)
125
    {
126
        $claim_checkers = $this->checker_manager->getSelectedClaimChecker($selected_claim_checkers);
127
        $header_checkers = $this->checker_manager->getSelectedHeaderChecker($selected_header_checkers);
128
        
129
        return CheckerManagerFactory::createClaimCheckerManager($claim_checkers, $header_checkers);
0 ignored issues
show
Documentation introduced by
$claim_checkers is of type array<integer,object<Jos...ClaimCheckerInterface>>, but the function expects a array<integer,string>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$header_checkers is of type array<integer,object<Jos...eaderCheckerInterface>>, but the function expects a array<integer,string>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
130
    }
131
132
    /**
133
     * @param \Jose\Checker\CheckerManagerInterface $checker_manager
134
     * @param \Jose\VerifierInterface               $verifier
135
     * @param \Jose\DecrypterInterface|null         $decrypter
136
     * @param \Psr\Log\LoggerInterface|null         $logger
137
     *
138
     * @return \Jose\JWTLoader
139
     */
140
    public function createJWTLoader(CheckerManagerInterface $checker_manager, VerifierInterface $verifier, DecrypterInterface $decrypter = null, LoggerInterface $logger = null)
141
    {
142
        $jwt_loader = new JWTLoader($checker_manager, $verifier, $logger);
143
        if (null !== $decrypter) {
144
            $jwt_loader->enableEncryptionSupport($decrypter);
145
        }
146
        
147
        return $jwt_loader;
148
    }
149
150
    /**
151
     * @param \Jose\SignerInterface         $signer
152
     * @param \Jose\EncrypterInterface|null $encrypter
153
     *
154
     * @return \Jose\JWTCreator
155
     */
156
    public function createJWTCreator(SignerInterface $signer, EncrypterInterface $encrypter = null)
157
    {
158
        $jwt_creator = new JWTCreator($signer);
159
        if (null !== $encrypter) {
160
            $jwt_creator->enableEncryptionSupport($encrypter);
161
        }
162
        
163
        return $jwt_creator;
164
    }
165
}
166