Completed
Push — master ( f15022...630d96 )
by Neomerx
02:54
created

BaseAsymmetricCrypt::__destruct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php namespace Limoncello\Crypt;
2
3
/**
4
 * Copyright 2015-2016 [email protected] (www.neomerx.com)
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
use Generator;
20
21
/**
22
 * @package Limoncello\Crypt
23
 */
24
abstract class BaseAsymmetricCrypt extends BaseCrypt
25
{
26
    /**
27
     * @var resource|null
28
     */
29
    private $key = null;
30
31
    /**
32
     * @var int|null
33
     */
34
    private $keyBytes = null;
35
36
    /**
37
     * Destructor.
38
     */
39 3
    public function __destruct()
40
    {
41 3
        $this->closeKey();
42 3
    }
43
44
    /**
45
     * @return $this
46
     */
47 3
    public function closeKey()
48
    {
49 3
        if ($this->key !== null) {
50 3
            openssl_pkey_free($this->key);
51 3
            $this->key      = null;
52 3
            $this->keyBytes = null;
53
        }
54
55 3
        return $this;
56
    }
57
58
    /**
59
     * @return resource|null
60
     */
61 3
    protected function getKey()
62
    {
63 3
        return $this->key;
64
    }
65
66
    /**
67
     * @param resource $key
68
     *
69
     * @return $this
70
     */
71 3
    protected function setKey($key)
72
    {
73 3
        assert(is_resource($key) === true);
74
75 3
        $this->closeKey();
76 3
        $this->key = $key;
77
78 3
        return $this;
79
    }
80
81
    /**
82
     * @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...
83
     */
84 3
    protected function getKeyBytes()
85
    {
86 3
        if ($this->keyBytes === null && $this->getKey() !== null) {
87 3
            $this->clearErrors();
88 3
            $details = openssl_pkey_get_details($this->getKey());
89 3
            $details !== false ?: $this->throwException(new CryptException($this->getErrorMessage()));
90 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...
91
        }
92
93 3
        return $this->keyBytes;
94
    }
95
96
    /**
97
     * @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...
98
     */
99 3 View Code Duplication
    protected function getEncryptChunkSize()
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...
100
    {
101 3
        $keyBytes = $this->getKeyBytes();
102
103
        // 11 is a kind of magic number related to padding.
104 3
        $result = $keyBytes === null ? null : $keyBytes - 11;
105
106 3
        return $result;
107
    }
108
109
    /**
110
     * @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...
111
     */
112 3 View Code Duplication
    protected function getDecryptChunkSize()
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...
113
    {
114 3
        $keyBytes = $this->getKeyBytes();
115 3
        $result   = $keyBytes === null ? null : $keyBytes;
116
117 3
        return $result;
118
    }
119
120
    /**
121
     * @param string $value
122
     * @param int    $maxSize
123
     *
124
     * @return Generator
125
     */
126 3
    protected function chunkString($value, $maxSize)
127
    {
128 3
        $isValidInput = is_string($value) === true && is_int($maxSize) === true && $maxSize > 0;
129
130 3
        assert($isValidInput === true);
131
132 3
        if ($isValidInput === true) {
133 3
            $start  = 0;
134 3
            $length = strlen($value);
135 3
            if ($length === 0) {
136 1
                yield $value;
137
            }
138 3
            while ($start < $length) {
139 3
                yield substr($value, $start, $maxSize);
140 3
                $start += $maxSize;
141
            }
142
        }
143 3
    }
144
}
145