EncryptedColumnLegacySupport   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 96
Duplicated Lines 40.63 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 5
dl 39
loc 96
rs 10
c 0
b 0
f 0
ccs 33
cts 33
cp 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getSQLDeclaration() 0 4 1
A requiresSQLCommentHint() 0 4 1
A getName() 0 4 1
A create() 7 7 1
A convertToPHPValue() 0 22 3
A convertToDatabaseValue() 8 8 2
C decodeJson() 24 24 8

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
namespace Carnage\EncryptedColumn\Dbal;
3
4
use Carnage\EncryptedColumn\Service\EncryptionService;
5
use Carnage\EncryptedColumn\ValueObject\ValueHolder;
6
use Doctrine\DBAL\Platforms\AbstractPlatform;
7
use Doctrine\DBAL\Types\ConversionException;
8
use Doctrine\DBAL\Types\Type;
9
use Carnage\EncryptedColumn\ValueObject\EncryptedColumn as EncryptedColumnVO;
10
11
/**
12
 * Object type for reading from legacy encrypted column extensions and converting to a better format
13
 *
14
 * This type is a drop in replacement for the EncryptedColumn class but drops some strictness to allow for
15
 * reading data that is not in an expected format. You should only use this if you have existing data in
16
 * your database you wish to convert
17
 *
18
 * Class EncryptedColumn
19
 */
20
class EncryptedColumnLegacySupport extends Type
21
{
22
    const ENCRYPTED = 'encrypted';
23
24
    /**
25
     * @var EncryptionService
26
     */
27
    private $encryptionService;
28
29 1 View Code Duplication
    public static function create(EncryptionService $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...
30
    {
31 1
        Type::addType(EncryptedColumnLegacySupport::ENCRYPTED, EncryptedColumnLegacySupport::class);
32
        /** @var EncryptedColumnLegacySupport $instance */
33 1
        $instance = Type::getType(EncryptedColumnLegacySupport::ENCRYPTED);
34 1
        $instance->encryptionService = $encryptionService;
35 1
    }
36
37 1
    public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
38
    {
39 1
        return $platform->getClobTypeDeclarationSQL($fieldDeclaration);
40
    }
41
42 1
    public function requiresSQLCommentHint(AbstractPlatform $platform)
43
    {
44 1
        return true;
45
    }
46
47 1
    public function getName()
48
    {
49 1
        return self::ENCRYPTED;
50
    }
51
52 2
    public function convertToPHPValue($value, AbstractPlatform $platform)
53
    {
54 2
        if ($value === null) {
55
            return null;
56
        }
57
58
        try {
59 2
            $decoded = $this->decodeJson($value);
60 1
        } catch (ConversionException $e) {
61
            //The data wasn't in the format we expected, assume it is legacy data which needs converting
62
            //Drop in some defaults to allow the library to handle it.
63
            $decoded = [
64 1
                'data' => $value,
65
                'classname' => ValueHolder::class,
66 1
                'serializer' => 'legacy',
67 1
                'encryptor' => 'legacy',
68
                'keyid' => 'legacy',
69
            ];
70
        }
71 2
72
        return $this->encryptionService->decryptField(EncryptedColumnVO::fromArray($decoded));
73
    }
74 1
75 View Code Duplication
    public function convertToDatabaseValue($value, AbstractPlatform $platform)
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...
76 1
    {
77
        if ($value === null) {
78
            return null;
79
        }
80 1
81
        return json_encode($this->encryptionService->encryptField($value));
82
    }
83
84
    /**
85
     * Based on: https://github.com/schmittjoh/serializer/blob/master/src/JMS/Serializer/JsonDeserializationVisitor.php
86
     *
87
     * @param $value
88
     * @return mixed
89
     * @throws ConversionException
90 2
     */
91 View Code Duplication
    private function decodeJson($value)
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...
92 2
    {
93
        $decoded = json_decode($value, true);
94 2
95 2
        switch (json_last_error()) {
96 1
            case JSON_ERROR_NONE:
97
                if (!is_array($decoded)) {
98
                    throw ConversionException::conversionFailed($value, 'Json was not an array');
99 1
                }
100 1
                return $decoded;
101
            case JSON_ERROR_DEPTH:
102 1
                throw ConversionException::conversionFailed($value, 'Could not decode JSON, maximum stack depth exceeded.');
103
            case JSON_ERROR_STATE_MISMATCH:
104 1
                throw ConversionException::conversionFailed($value, 'Could not decode JSON, underflow or the nodes mismatch.');
105
            case JSON_ERROR_CTRL_CHAR:
106 1
                throw ConversionException::conversionFailed($value, 'Could not decode JSON, unexpected control character found.');
107 1
            case JSON_ERROR_SYNTAX:
108
                throw ConversionException::conversionFailed($value, 'Could not decode JSON, syntax error - malformed JSON.');
109
            case JSON_ERROR_UTF8:
110
                throw ConversionException::conversionFailed($value, 'Could not decode JSON, malformed UTF-8 characters (incorrectly encoded?)');
111
            default:
112
                throw ConversionException::conversionFailed($value, 'Could not decode Json');
113
        }
114
    }
115
}
116