EncryptedColumn   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 84
Duplicated Lines 46.43 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 63.16%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 5
dl 39
loc 84
ccs 24
cts 38
cp 0.6316
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getSQLDeclaration() 0 4 1
A requiresSQLCommentHint() 0 4 1
A getName() 0 4 1
A convertToPHPValue() 0 10 2
A convertToDatabaseValue() 8 8 2
C decodeJson() 24 24 8
A create() 7 7 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
namespace Carnage\EncryptedColumn\Dbal;
3
4
use Carnage\EncryptedColumn\Service\EncryptionService;
5
use Doctrine\DBAL\Platforms\AbstractPlatform;
6
use Doctrine\DBAL\Types\ConversionException;
7
use Doctrine\DBAL\Types\Type;
8
use Carnage\EncryptedColumn\ValueObject\EncryptedColumn as EncryptedColumnVO;
9
10
/**
11
 * Object type for storing an encrypted column object as json in the db
12
 *
13
 * Class EncryptedColumn
14
 */
15
class EncryptedColumn extends Type
16
{
17
    const ENCRYPTED = 'encrypted';
18
19
    /**
20
     * @var EncryptionService
21
     */
22
    private $encryptionService;
23
24 4 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...
25
    {
26 4
        Type::addType(EncryptedColumn::ENCRYPTED, EncryptedColumn::class);
27
        /** @var EncryptedColumn $instance */
28 4
        $instance = Type::getType(EncryptedColumn::ENCRYPTED);
29 4
        $instance->encryptionService = $encryptionService;
30 4
    }
31
32 4
    public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
33
    {
34 4
        return $platform->getClobTypeDeclarationSQL($fieldDeclaration);
35
    }
36
37 4
    public function requiresSQLCommentHint(AbstractPlatform $platform)
38
    {
39 4
        return true;
40
    }
41
42 4
    public function getName()
43
    {
44 4
        return self::ENCRYPTED;
45
    }
46
47 3
    public function convertToPHPValue($value, AbstractPlatform $platform)
48
    {
49 3
        if ($value === null) {
50
            return null;
51
        }
52
53 3
        $decoded = $this->decodeJson($value);
54
55 3
        return $this->encryptionService->decryptField(EncryptedColumnVO::fromArray($decoded));
56
    }
57
58 4 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...
59
    {
60 4
        if ($value === null) {
61
            return null;
62
        }
63
64 4
        return json_encode($this->encryptionService->encryptField($value));
65
    }
66
67
    /**
68
     * Based on: https://github.com/schmittjoh/serializer/blob/master/src/JMS/Serializer/JsonDeserializationVisitor.php
69
     *
70
     * @param $value
71
     * @return mixed
72
     * @throws ConversionException
73
     */
74 3 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...
75
    {
76 3
        $decoded = json_decode($value, true);
77
78 3
        switch (json_last_error()) {
79 3
            case JSON_ERROR_NONE:
80 3
                if (!is_array($decoded)) {
81
                    throw ConversionException::conversionFailed($value, 'Json was not an array');
82
                }
83 3
                return $decoded;
84
            case JSON_ERROR_DEPTH:
85
                throw ConversionException::conversionFailed($value, 'Could not decode JSON, maximum stack depth exceeded.');
86
            case JSON_ERROR_STATE_MISMATCH:
87
                throw ConversionException::conversionFailed($value, 'Could not decode JSON, underflow or the nodes mismatch.');
88
            case JSON_ERROR_CTRL_CHAR:
89
                throw ConversionException::conversionFailed($value, 'Could not decode JSON, unexpected control character found.');
90
            case JSON_ERROR_SYNTAX:
91
                throw ConversionException::conversionFailed($value, 'Could not decode JSON, syntax error - malformed JSON.');
92
            case JSON_ERROR_UTF8:
93
                throw ConversionException::conversionFailed($value, 'Could not decode JSON, malformed UTF-8 characters (incorrectly encoded?)');
94
            default:
95
                throw ConversionException::conversionFailed($value, 'Could not decode Json');
96
        }
97
    }
98
}
99