Passed
Push — master ( dc471b...c13d2f )
by Julien
21:34
created

ServiceProvider   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 8.33%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
c 1
b 0
f 0
dl 0
loc 35
ccs 2
cts 24
cp 0.0833
rs 10
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A register() 0 31 1
1
<?php
2
3
/**
4
 * This file is part of the Zemit Framework.
5
 *
6
 * (c) Zemit Team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.txt
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zemit\Provider\Crypt;
13
14
use Phalcon\Di\DiInterface;
15
use Phalcon\Encryption\Crypt;
16
use Zemit\Config\ConfigInterface;
17
use Zemit\Provider\AbstractServiceProvider;
18
19
class ServiceProvider extends AbstractServiceProvider
20
{
21
    protected string $serviceName = 'crypt';
22
    
23 112
    public function register(DiInterface $di): void
24
    {
25 112
        $di->setShared($this->getName(), function (?string $cipher = null, ?bool $useSigning = null) use ($di) {
26
            
27
            $config = $di->get('config');
28
            assert($config instanceof ConfigInterface);
29
            $options = $config->pathToArray('crypt', []);
30
            
31
            $cipher ??= $options['cipher'] ?? 'aes-256-cfb';
32
            $useSigning ??= $options['useSigning'] ?? true;
33
            
34
            $hash = $options['hash'] ?? 'sha256';
35
            $key = $options['key'] ?? 'T4\xb1\x8d\xa9\x98\x05\\x8c\xbe\x1d\x07&[\x99\x18\xa4~Lc1\xbeW\xb3';
36
            $paddingScheme = $options['paddingScheme'] ?? Crypt::PADDING_DEFAULT;
37
            $padFactoryClass = $options['padFactory'] ?? \Phalcon\Encryption\Crypt\PadFactory::class;
38
            $authData = $options['authData'] ?? '';
39
            $authTag = $options['authTag'] ?? '';
40
            $authTagLength = $options['authTagLength'] ?? 16;
41
            
42
            $padFactory = new $padFactoryClass();
43
            assert($padFactory instanceof \Phalcon\Encryption\Crypt\PadFactory);
44
            
45
            $crypt = new Crypt($cipher, $useSigning, $padFactory);
46
            $crypt->setHashAlgorithm($hash);
47
            $crypt->setPadding($paddingScheme);
48
            $crypt->setKey($key);
49
            $crypt->setAuthData($authData);
50
            $crypt->setAuthTag($authTag);
51
            $crypt->setAuthTagLength($authTagLength);
52
            
53
            return $crypt;
54 112
        });
55
    }
56
}
57