Passed
Pull Request — master (#5)
by Chris
02:42
created

Setup   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 85
Duplicated Lines 34.12 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 80.95%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 11
dl 29
loc 85
ccs 34
cts 42
cp 0.8095
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 8 2
A enableLegacy() 0 6 1
A withKeyPath() 0 5 1
A buildEncryptionService() 11 11 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\VersionedContainer;
6
use Carnage\EncryptedColumn\Dbal\EncryptedColumn;
7
use Carnage\EncryptedColumn\Dbal\EncryptedColumnLegacySupport;
8
use Carnage\EncryptedColumn\Encryptor\HaliteEncryptor;
9
use Carnage\EncryptedColumn\Encryptor\LegacyEncryptor;
10
use Carnage\EncryptedColumn\Serializer\LegacySerializer;
11
use Carnage\EncryptedColumn\Serializer\PhpSerializer;
12
use Carnage\EncryptedColumn\Service\EncryptionService;
13
use Doctrine\ORM\EntityManagerInterface;
14
15
final class Setup
16
{
17
    private $keyPath;
18
    private $enableLegacy = false;
19
    private $legacyKey;
20
21 1
    public function register(EntityManagerInterface $em)
22
    {
23 1
        if ($this->enableLegacy) {
24 1
            $this->doRegisterLegacy($em);
25
        } else {
26
            $this->doRegister($em);
27
        }
28 1
    }
29
30 1
    public function enableLegacy(string $legacyKey)
31
    {
32 1
        $this->enableLegacy = true;
33 1
        $this->legacyKey = $legacyKey;
34 1
        return $this;
35
    }
36
37 1
    public function withKeyPath(string $keypath)
38
    {
39 1
        $this->keyPath = $keypath;
40 1
        return $this;
41
    }
42
43 1 View Code Duplication
    private function buildEncryptionService(): EncryptionService
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...
44
    {
45 1
        $encryptors = self::buildEncryptorsContainer();
46 1
        $serializers = self::buildSerilaizerContainer();
47 1
        return new EncryptionService(
48 1
            $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...
49 1
            $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...
50
            $encryptors,
51
            $serializers
52
        );
53
    }
54
55 1
    private function buildEncryptorsContainer(): VersionedContainer
56
    {
57 1
        $services = [new HaliteEncryptor($this->keyPath)];
58 1
        if ($this->enableLegacy) {
59 1
            $services[] = new LegacyEncryptor($this->legacyKey);
60
        }
61
        //@TODO add legacy encryptor, throw exceptions if required keys aren't specified
62 1
        return new VersionedContainer(...$services);
63
    }
64
65 1
    private function buildSerilaizerContainer(): VersionedContainer
66
    {
67 1
        $services = [new PhpSerializer()];
68 1
        if ($this->enableLegacy) {
69 1
            $services[] = new LegacySerializer();
70
        }
71 1
        return new VersionedContainer(...$services);
72
    }
73
74
    /**
75
     * @param EntityManagerInterface $em
76
     */
77 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...
78
    {
79
        EncryptedColumn::create($this->buildEncryptionService());
80
        $conn = $em->getConnection();
81
        $conn->getDatabasePlatform()->registerDoctrineTypeMapping(
82
            EncryptedColumn::ENCRYPTED,
83
            EncryptedColumn::ENCRYPTED
84
        );
85
    }
86
87
    /**
88
     * @param EntityManagerInterface $em
89
     */
90 1 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...
91
    {
92 1
        EncryptedColumnLegacySupport::create($this->buildEncryptionService());
93 1
        $conn = $em->getConnection();
94 1
        $conn->getDatabasePlatform()->registerDoctrineTypeMapping(
95 1
            EncryptedColumnLegacySupport::ENCRYPTED,
96 1
            EncryptedColumnLegacySupport::ENCRYPTED
97
        );
98
    }
99
}