Test Setup Failed
Push — master ( 009624...c15db0 )
by Chris
48s
created

Setup   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 115
Duplicated Lines 33.91 %

Coupling/Cohesion

Components 1
Dependencies 13

Test Coverage

Coverage 80.95%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 13
dl 39
loc 115
rs 10
c 0
b 0
f 0
ccs 34
cts 42
cp 0.8095

10 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 8 2
A __construct() 0 4 1
A enableLegacy() 11 11 1
A withKeyPath() 10 10 1
A withKey() 0 12 2
A buildEncryptionService() 0 12 1
A buildEncryptorsContainer() 0 9 2
A buildSerilaizerContainer() 0 8 2
A doRegister() 9 9 1
A doRegisterLegacy() 9 9 1

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
namespace Carnage\EncryptedColumn;
4
5
use Carnage\EncryptedColumn\Container\KeyContainer;
6
use Carnage\EncryptedColumn\Container\VersionedContainer;
7
use Carnage\EncryptedColumn\Dbal\EncryptedColumn;
8
use Carnage\EncryptedColumn\Dbal\EncryptedColumnLegacySupport;
9
use Carnage\EncryptedColumn\Encryptor\HaliteEncryptor;
10
use Carnage\EncryptedColumn\Encryptor\LegacyEncryptor;
11
use Carnage\EncryptedColumn\Serializer\LegacySerializer;
12
use Carnage\EncryptedColumn\Serializer\PhpSerializer;
13
use Carnage\EncryptedColumn\Service\EncryptionService;
14
use Carnage\EncryptedColumn\ValueObject\Key;
15
use Carnage\EncryptedColumn\ValueObject\KeyIdentity;
16
use Doctrine\ORM\EntityManagerInterface;
17
18
final class Setup
19
{
20
    private $keyPath;
21 1
    private $enableLegacy = false;
22
    private $legacyKey;
23 1
    private $keyContainer;
24 1
25
    public function __construct()
26
    {
27
        $this->keyContainer = new KeyContainer();
28 1
    }
29
30 1
    public function register(EntityManagerInterface $em)
31
    {
32 1
        if ($this->enableLegacy) {
33 1
            $this->doRegisterLegacy($em);
34 1
        } else {
35
            $this->doRegister($em);
36
        }
37 1
    }
38
39 1 View Code Duplication
    public function enableLegacy(string $legacyKey)
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...
40 1
    {
41
        $this->enableLegacy = true;
42
        $this->legacyKey = $legacyKey;
43 1
44
        $key = new Key($legacyKey);
45 1
        $this->keyContainer->addKey($key);
46 1
        $this->keyContainer->tagKey('legacy', $key->getIdentifier()->toString());
47 1
48 1
        return $this;
49 1
    }
50
51 View Code Duplication
    public function withKeyPath(string $keypath)
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...
52
    {
53
        $this->keyPath = $keypath;
54
55 1
        $key = new Key($keypath);
56
        $this->keyContainer->addKey($key);
57 1
        $this->keyContainer->tagKey('default', $key->getIdentifier()->toString());
58 1
59 1
        return $this;
60
    }
61
62 1
    public function withKey(string $key, array $tags = [])
63
    {
64
        $key = new Key($key);
65 1
        $keyId = $key->getIdentifier()->toString();
66
        $this->keyContainer->addKey($key);
67 1
68 1
        foreach ($tags as $tag) {
69 1
            $this->keyContainer->tagKey($tag, $keyId);
70
        }
71 1
72
        return $this;
73
    }
74
75
    private function buildEncryptionService(): EncryptionService
76
    {
77
        $encryptors = self::buildEncryptorsContainer();
78
        $serializers = self::buildSerilaizerContainer();
79
        return new EncryptionService(
80
            $encryptors->get(HaliteEncryptor::IDENTITY),
0 ignored issues
show
Compatibility introduced by
$encryptors->get(\Carnag...iteEncryptor::IDENTITY) of type object<Carnage\Encrypted...ner\VersionedInterface> is not a sub-type of object<Carnage\Encrypted...tor\EncryptorInterface>. It seems like you assume a child interface of the interface Carnage\EncryptedColumn\...iner\VersionedInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
81
            $serializers->get(PhpSerializer::IDENTITY),
0 ignored issues
show
Compatibility introduced by
$serializers->get(\Carna...hpSerializer::IDENTITY) of type object<Carnage\Encrypted...ner\VersionedInterface> is not a sub-type of object<Carnage\Encrypted...er\SerializerInterface>. It seems like you assume a child interface of the interface Carnage\EncryptedColumn\...iner\VersionedInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
82
            $encryptors,
83
            $serializers,
84
            $this->keyContainer
85
        );
86
    }
87
88
    private function buildEncryptorsContainer(): VersionedContainer
89
    {
90 1
        $services = [new HaliteEncryptor($this->keyPath)];
0 ignored issues
show
Unused Code introduced by
The call to HaliteEncryptor::__construct() has too many arguments starting with $this->keyPath.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
91
        if ($this->enableLegacy) {
92 1
            $services[] = new LegacyEncryptor($this->legacyKey);
0 ignored issues
show
Unused Code introduced by
The call to LegacyEncryptor::__construct() has too many arguments starting with $this->legacyKey.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
93 1
        }
94 1
        //@TODO add legacy encryptor, throw exceptions if required keys aren't specified
95 1
        return new VersionedContainer(...$services);
96 1
    }
97
98 1
    private function buildSerilaizerContainer(): VersionedContainer
99
    {
100
        $services = [new PhpSerializer()];
101
        if ($this->enableLegacy) {
102
            $services[] = new LegacySerializer();
103
        }
104
        return new VersionedContainer(...$services);
105
    }
106
107
    /**
108
     * @param EntityManagerInterface $em
109
     */
110 View Code Duplication
    private function doRegister(EntityManagerInterface $em)
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...
111
    {
112
        EncryptedColumn::create($this->buildEncryptionService());
113
        $conn = $em->getConnection();
114
        $conn->getDatabasePlatform()->registerDoctrineTypeMapping(
115
            EncryptedColumn::ENCRYPTED,
116
            EncryptedColumn::ENCRYPTED
117
        );
118
    }
119
120
    /**
121
     * @param EntityManagerInterface $em
122
     */
123 View Code Duplication
    private function doRegisterLegacy(EntityManagerInterface $em)
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...
124
    {
125
        EncryptedColumnLegacySupport::create($this->buildEncryptionService());
126
        $conn = $em->getConnection();
127
        $conn->getDatabasePlatform()->registerDoctrineTypeMapping(
128
            EncryptedColumnLegacySupport::ENCRYPTED,
129
            EncryptedColumnLegacySupport::ENCRYPTED
130
        );
131
    }
132
}