Passed
Pull Request — master (#5)
by Chris
03:25
created

Setup::enableLegacy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 1
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
}