FileEncryptionTrait   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 4
eloc 10
c 2
b 0
f 0
dl 0
loc 45
ccs 10
cts 10
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A decryptFile() 0 9 2
A encryptFile() 0 9 2
1
<?php
2
3
/**
4
 * Trait implementation of file encryption/decryption for asymmetric/symmetric encryption algorithms.
5
 */
6
7
namespace CryptoManana\Core\Traits\MessageEncryption;
8
9
use CryptoManana\Core\Interfaces\MessageEncryption\FileEncryptionInterface as FileEncryptionSpecification;
10
use CryptoManana\Core\Interfaces\MessageEncryption\DataEncryptionInterface as DataEncryptionSpecification;
11
use CryptoManana\Core\Traits\CommonValidations\FileNameValidationTrait as ValidateFileNames;
12
13
/**
14
 * Trait FileEncryptionTrait - Reusable implementation of `FileEncryptionInterface`.
15
 *
16
 * @see \CryptoManana\Core\Interfaces\MessageDigestion\FileEncryptionInterface The abstract specification.
17
 *
18
 * @package CryptoManana\Core\Traits\MessageEncryption
19
 *
20
 * @mixin FileEncryptionSpecification
21
 * @mixin DataEncryptionSpecification
22
 * @mixin ValidateFileNames
23
 */
24
trait FileEncryptionTrait
25
{
26
    /**
27
     * File name and path validations.
28
     *
29
     * {@internal Reusable implementation of the common file name validation. }}
30
     */
31
    use ValidateFileNames;
32
33
    /**
34
     * Encrypts the content of a given plain file.
35
     *
36
     * @param string $filename The full path and name of the file for encryption.
37
     *
38
     * @return string The encrypted file content.
39
     * @throws \Exception Validation errors.
40
     */
41 36
    public function encryptFile($filename)
42
    {
43 36
        if (!is_string($filename)) {
44 12
            throw new \InvalidArgumentException('The file path must be of type string.');
45
        }
46
47 24
        $this->validateFileNamePath($filename);
48
49 12
        return $this->encryptData(file_get_contents($filename));
50
    }
51
52
    /**
53
     * Decrypts the content of a given encrypted file.
54
     *
55
     * @param string $filename The full path and name of the file for encryption.
56
     *
57
     * @return string The decrypted file content.
58
     * @throws \Exception Validation errors.
59
     */
60 36
    public function decryptFile($filename)
61
    {
62 36
        if (!is_string($filename)) {
63 12
            throw new \InvalidArgumentException('The file path must be of type string.');
64
        }
65
66 24
        $this->validateFileNamePath($filename);
67
68 12
        return $this->decryptData(file_get_contents($filename));
69
    }
70
}
71