GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

EncryptedType::requiresToPhpCast()   A
last analyzed

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 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
namespace BryanCrowe\EncryptedType\Database\Type;
3
4
use Cake\Core\Configure;
5
use Cake\Database\Driver;
6
use Cake\Database\Type;
7
use Cake\Database\TypeInterface;
8
use Cake\Database\Type\OptionalConvertInterface;
9
use Cake\Utility\Security;
10
use InvalidArgumentException;
11
use PDO;
12
13
/**
14
 * Encrypted BLOB converter. Used to encrypt and decrypt stored data.
15
 */
16
class EncryptedType extends Type implements OptionalConvertInterface, TypeInterface
17
{
18
19
    /**
20
     * Key used for encryption.
21
     *
22
     * @var string|null
23
     */
24
    protected $key = null;
25
26
    /**
27
     * Constructor
28
     *
29
     * @param string|null $name The name identifying this type.
30
     */
31 3
    public function __construct($name = null)
32
    {
33 3
        parent::__construct($name);
34 3
        $this->key = Configure::readOrFail('Encryption.key');
35 3
    }
36
37
    /**
38
     * Convert encrypted values to PHP strings or null.
39
     *
40
     * @param mixed $value The value to convert.
41
     * @param \Cake\Database\Driver $driver The driver instance to convert with.
42
     * @return mixed
43
     */
44 3
    public function toPHP($value, Driver $driver)
45
    {
46 3
        if ($value === null) {
47 3
            return null;
48
        }
49
50 3
        return (string)Security::decrypt($value, $this->key);
51
    }
52
53
    /**
54
     * Marshalls request data.
55
     *
56
     * @param mixed $value The value to convert.
57
     * @return mixed Converted value.
58
     */
59 3
    public function marshal($value)
60
    {
61 3
        if ($value === null) {
62 3
            return null;
63
        }
64
65 3
        if (is_array($value)) {
66 3
            return '';
67
        }
68
69 3
        return (string)$value;
70
    }
71
72
    /**
73
     * Convert PHP values into the database format.
74
     *
75
     * @param mixed $value The value to convert.
76
     * @param \Cake\Database\Driver $driver The driver instance to convert with.
77
     * @return string
78
     */
79 9
    public function toDatabase($value, Driver $driver)
80
    {
81 9
        if ($value === null) {
82 3
            return null;
83
        };
84
85 9
        if (is_string($value)) {
86 6
            return Security::encrypt($value, $this->key);
87
        }
88
89 9
        if (is_object($value) && method_exists($value, '__toString')) {
90 3
            return Security::encrypt($value->__toString(), $this->key);
91
        }
92
93 9
        if (is_scalar($value)) {
94 6
            return Security::encrypt((string)$value, $this->key);
95
        }
96
97 3
        throw new InvalidArgumentException('Cannot convert value to an encrypted string.');
98
    }
99
100
    /**
101
     * Get the correct PDO binding type for string data.
102
     *
103
     * @param mixed $value The value being bound.
104
     * @param \Cake\Database\Driver $driver The driver.
105
     * @return int
106
     */
107 3
    public function toStatement($value, Driver $driver)
108
    {
109 3
        if ($value === null) {
110
            return PDO::PARAM_NULL;
111
        }
112
113 3
        return PDO::PARAM_STR;
114
    }
115
116
    /**
117
     * {@inheritDoc}
118
     *
119
     * @return boolean True as database results are returned as encrypted strings.
120
     */
121 3
    public function requiresToPhpCast()
122
    {
123 3
        return true;
124
    }
125
}
126