Lines 91.66% 11 / 12
Methods 80.00% 4 / 5
Classes 0.00% 0 / 1
Covered by tests of size
Name Lines Methods CRAP
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 getInstance 100.00% 7 / 7 100.00% 1 / 1 3
 encrypt 0.00% 0 / 1 0.00% 0 / 1 2
 error 100.00% 1 / 1 100.00% 1 / 1 2
 setSalt 100.00% 2 / 2 100.00% 1 / 1 1
25class Encryption
26{
27    /**
28     * Error constant.
29     */
30    private const string PMF_ERROR_USER_NO_ENCTYPE = 'EncryptionTypes method could not be found.';
31
32    /**
33     * Public array that contains error messages.
34     *
35     * @var string[]
36     */
37    public array $errors = [];
38
39    /**
40     * Salt.
41     */
42    protected string $salt = '';
43
44    /**
45     * Constructor.
46     */
47    private function __construct(
48        protected Configuration $configuration,
49    ) {
50    }
51
52    /**
53     * This method is called statically. The parameter encType specifies the
54     * type of encryption method for the encryption object. Supported
55     * are 'bcrypt', 'hash', and 'none'.
56     * $enc = EncryptionTypes::getInstance('hash');
57     * $enc is an instance of the class EncryptionTypes\Hash.
58     * If the given encryption-type is not supported, getInstance() will return an
59     * object without database access and with an error message. See the
60     * documentation of the error() method for further details.
61     */
62    public static function getInstance(string $encType, Configuration $configuration): Encryption
63    {
64        $self = new self($configuration);
65        $encType = ucfirst(strtolower($encType));
66
67        $encClass = 'phpMyFAQ\\EncryptionTypes\\' . $encType;
68        if (!class_exists($encClass) || !is_subclass_of($encClass, self::class)) {
69            $self->errors[] = self::PMF_ERROR_USER_NO_ENCTYPE;
70
71            return $self;
72        }
73
74        return new $encClass($configuration);
75    }
76
77    /**
78     * Encrypts the given string and returns the result.
79     *
80     * @param string $password String
81     */
82    public function encrypt(#[SensitiveParameter] string $password): string
83    {
84        return $password;
85    }
86
87    /**
88     * The string returned by error() contains messages for all errors that
89     * occurred during object processing.
90     * New lines separate messages.
91     */
92    public function error(): string
93    {
94        return $this->errors !== [] ? implode(PHP_EOL, $this->errors) . PHP_EOL : '';
95    }
96
97    /**
98     * Setter for salt.
99     */
100    public function setSalt(string $login): Encryption
101    {
102        $this->salt = (string) $this->configuration->get(item: 'security.salt') . $login;
103        return $this;
104    }
105}