Completed
Push — master ( e075b2...05acc7 )
by Anton
07:07 queued 03:27
created

KeyCommand   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
c 1
b 0
f 0
lcom 0
cbo 4
dl 0
loc 45
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B perform() 0 26 2
1
<?php
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
namespace Spiral\Commands\Spiral;
9
10
use Spiral\Console\Command;
11
use Spiral\Core\DirectoriesInterface;
12
use Spiral\Encrypter\Configs\EncrypterConfig;
13
use Spiral\Encrypter\EncrypterManager;
14
use Spiral\Files\FilesInterface;
15
16
/**
17
 * Updates encryption key in root/.env file. Same logic as in Laravel.
18
 */
19
class KeyCommand extends Command
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24
    protected $name = 'app:key';
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    protected $description = 'Update application encryption key';
30
31
    /**
32
     * @param DirectoriesInterface $directories
33
     * @param FilesInterface       $files
34
     * @param EncrypterConfig      $config
35
     * @param EncrypterManager     $encrypterManager
36
     */
37
    public function perform(
38
        DirectoriesInterface $directories,
39
        FilesInterface $files,
40
        EncrypterConfig $config,
41
        EncrypterManager $encrypterManager
42
    ) {
43
        $envFilename = $directories->directory('root') . '.env';
44
45
        if (!$files->exists($envFilename)) {
46
            $this->writeln(
47
                "<fg=red>'env.' file does not exists, unable to sek encryption key.</fg=red>"
48
            );
49
        }
50
51
        $environmentData = $files->read($envFilename);
52
53
        $environmentData = str_replace(
54
            base64_encode($config->getKey()),
55
            base64_encode($encrypterManager->generateKey()),
0 ignored issues
show
Bug introduced by
The method generateKey() does not seem to exist on object<Spiral\Encrypter\EncrypterManager>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
56
            $environmentData
57
        );
58
59
        $files->write($envFilename, $environmentData);
60
61
        $this->writeln("<info>Encryption key has been successfully updated.</info>");
62
    }
63
}