BaseAsymmetricCrypt   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 2
dl 0
loc 135
ccs 45
cts 45
cp 1
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __destruct() 0 4 1
A closeKey() 0 10 2
A getKey() 0 4 1
A setKey() 0 9 1
A getKeyBytes() 0 11 4
A getEncryptChunkSize() 0 9 2
A getDecryptChunkSize() 0 7 2
A chunkString() 0 18 4
A checkIfPathToFileCheckPrefix() 0 8 2
1
<?php declare(strict_types=1);
2
3
namespace Limoncello\Crypt;
4
5
/**
6
 * Copyright 2015-2019 [email protected]
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 * http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
use Generator;
22
use Limoncello\Crypt\Exceptions\CryptException;
23
use function assert;
24
use function file_exists;
25
use function openssl_pkey_free;
26
use function openssl_pkey_get_details;
27
use function strlen;
28
use function substr;
29
30
/**
31
 * @package Limoncello\Crypt
32
 */
33
abstract class BaseAsymmetricCrypt extends BaseCrypt
34
{
35
    /**
36
     * @var resource|null
37
     */
38
    private $key = null;
39
40
    /**
41
     * @var int|null
42
     */
43
    private $keyBytes = null;
44
45
    /**
46
     * Destructor.
47
     */
48 5
    public function __destruct()
49
    {
50 5
        $this->closeKey();
51
    }
52
53
    /**
54
     * @return self
55
     */
56 5
    public function closeKey(): self
57
    {
58 5
        if ($this->key !== null) {
59 5
            openssl_pkey_free($this->key);
60 5
            $this->key      = null;
61 5
            $this->keyBytes = null;
62
        }
63
64 5
        return $this;
65
    }
66
67
    /**
68
     * @return resource|null
69
     */
70 3
    protected function getKey()
71
    {
72 3
        return $this->key;
73
    }
74
75
    /**
76
     * @param resource $key
77
     *
78
     * @return self
79
     */
80 5
    protected function setKey($key): self
81
    {
82 5
        assert(is_resource($key) === true);
83
84 5
        $this->closeKey();
85 5
        $this->key = $key;
86
87 5
        return $this;
88
    }
89
90
    /**
91
     * @return int|null
0 ignored issues
show
Documentation introduced by
Should the return type not be integer|double|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
92
     */
93 3
    protected function getKeyBytes(): ?int
94
    {
95 3
        if ($this->keyBytes === null && $this->getKey() !== null) {
96 3
            $this->clearErrors();
97 3
            $details = openssl_pkey_get_details($this->getKey());
98 3
            $details !== false ?: $this->throwException(new CryptException($this->getErrorMessage()));
99 3
            $this->keyBytes = $details['bits'] / 8;
0 ignored issues
show
Documentation Bug introduced by
It seems like $details['bits'] / 8 can also be of type double. However, the property $keyBytes is declared as type integer|null. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
100
        }
101
102 3
        return $this->keyBytes;
103
    }
104
105
    /**
106
     * @return int|null
0 ignored issues
show
Documentation introduced by
Should the return type not be integer|double?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
107
     */
108 3
    protected function getEncryptChunkSize(): ?int
109
    {
110 3
        $keyBytes = $this->getKeyBytes();
111
112
        // 11 is a kind of magic number related to padding.
113 3
        $result = $keyBytes === null ? null : $keyBytes - 11;
114
115 3
        return $result;
116
    }
117
118
    /**
119
     * @return int|null
0 ignored issues
show
Documentation introduced by
Should the return type not be integer|double?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
120
     */
121 3
    protected function getDecryptChunkSize(): ?int
122
    {
123 3
        $keyBytes = $this->getKeyBytes();
124 3
        $result   = $keyBytes === null ? null : $keyBytes;
125
126 3
        return $result;
127
    }
128
129
    /**
130
     * @param string $value
131
     * @param int    $maxSize
132
     *
133
     * @return Generator
134
     */
135 3
    protected function chunkString(string $value, int $maxSize): Generator
136
    {
137 3
        $isValidInput = $maxSize > 0;
138
139 3
        assert($isValidInput === true);
140
141 3
        if ($isValidInput === true) {
142 3
            $start  = 0;
143 3
            $length = strlen($value);
144 3
            if ($length === 0) {
145 1
                yield $value;
146
            }
147 3
            while ($start < $length) {
148 3
                yield substr($value, $start, $maxSize);
149 3
                $start += $maxSize;
150
            }
151
        }
152
    }
153
154
    /**
155
     * @param string $path
156
     *
157
     * @return bool
158
     */
159 5
    protected function checkIfPathToFileCheckPrefix(string $path): bool
160
    {
161 5
        if (file_exists($path) === true) {
162 5
            return substr($path, 0, 7) === 'file://';
163
        }
164
165 3
        return true;
166
    }
167
}
168