Completed
Push — master ( 02c253...584838 )
by
unknown
12s
created

HaliteSymmetricTransformer::getBinary()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace MediaMonks\Doctrine\Transformable\Transformer;
4
5
use ParagonIE\Halite\Halite;
6
use ParagonIE\Halite\HiddenString;
7
use ParagonIE\Halite\KeyFactory;
8
use ParagonIE\Halite\Symmetric\Crypto;
9
use ParagonIE\Halite\Symmetric\EncryptionKey;
10
11
class HaliteSymmetricTransformer implements TransformerInterface
12
{
13
    const HALITE_LEGACY_VERSION = '1.0.0';
14
15
    /**
16
     * @var bool
17
     */
18
    private $binary = true;
19
20
    /**
21
     * @var EncryptionKey
22
     */
23
    private $encryptionKey = null;
24
25
    /**
26
     * @param $encryptionKey
27
     * @param array $options
28
     */
29 4
    public function __construct($encryptionKey, array $options = [])
30
    {
31 4
        $this->encryptionKey = KeyFactory::loadEncryptionKey($encryptionKey);
32
33 4
        $this->setOptions($options);
34 4
    }
35
36
    /**
37
     * @param array $options
38
     */
39 4
    protected function setOptions(array $options)
40
    {
41 4
        if (array_key_exists('binary', $options)) {
42 2
            $this->binary = $options['binary'];
43 2
        }
44 4
    }
45
46
    /**
47
     * @return bool
48
     */
49 1
    public function getBinary()
50
    {
51 1
        return $this->binary;
52
    }
53
54
    /**
55
     * @param string $value
56
     * @return string
57
     */
58 3 View Code Duplication
    public function transform($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...
59
    {
60 3
        if (empty($value)) {
61 1
            return null;
62
        }
63
64 2
        if ($this->binary) {
65 1
            $value = \Sodium\bin2hex($value);
66 1
        }
67
68 2
        if (Halite::VERSION > self::HALITE_LEGACY_VERSION) {
69
            $value = new HiddenString($value);
70
        }
71
72 2
        return Crypto::encrypt($value, $this->encryptionKey);
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type string; however, ParagonIE\Halite\Symmetric\Crypto::encrypt() does only seem to accept object<ParagonIE\Halite\HiddenString>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
73
    }
74
75
    /**
76
     * @param string $value
77
     * @return string|null
78
     */
79 3 View Code Duplication
    public function reverseTransform($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...
80
    {
81 3
        if (empty($value)) {
82 1
            return null;
83
        }
84
85 2
        $decryptedValue = Crypto::decrypt($value, $this->encryptionKey);
86
87 2
        if (Halite::VERSION > self::HALITE_LEGACY_VERSION) {
88
            $decryptedValue = $decryptedValue->getString();
89
        }
90
91 2
        if (!$this->binary) {
92 1
            return $decryptedValue;
93
        }
94
95 1
        return \Sodium\hex2bin($decryptedValue);
96
    }
97
}
98
99