ComposerScripts   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 75
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A postInstall() 0 6 1
A postUpdate() 0 6 1
A generateKey() 0 8 1
A setKeyInConfigFile() 0 6 1
A generateRandomKey() 0 4 1
1
<?php
2
3
namespace Magister\Services\Foundation;
4
5
use Composer\Script\Event;
6
use Magister\Magister;
7
8
class ComposerScripts
9
{
10
    /**
11
     * The Magister application instance.
12
     *
13
     * @var \Magister\Magister
14
     */
15
    protected static $magister;
16
17
    /**
18
     * Handle the post-install Composer event.
19
     *
20
     * @param \Composer\Script\Event $event
21
     *
22
     * @return void
23
     */
24
    public static function postInstall(Event $event)
25
    {
26
        require_once $event->getComposer()->getConfig()->get('vendor-dir').'/autoload.php';
27
28
        static::generateKey();
29
    }
30
31
    /**
32
     * Handle the post-update Composer event.
33
     *
34
     * @param \Composer\Script\Event $event
35
     *
36
     * @return void
37
     */
38
    public static function postUpdate(Event $event)
39
    {
40
        require_once $event->getComposer()->getConfig()->get('vendor-dir').'/autoload.php';
41
42
        static::generateKey();
43
    }
44
45
    /**
46
     * Generate a safe key for session encryption.
47
     *
48
     * @return void
49
     */
50
    protected static function generateKey()
51
    {
52
        static::$magister = new Magister('foo');
53
54
        $key = static::generateRandomKey();
55
56
        static::setKeyInConfigFile($key);
57
    }
58
59
    /**
60
     * Set the application key in the config file.
61
     *
62
     * @param string $key
63
     *
64
     * @return void
65
     */
66
    protected static function setKeyInConfigFile($key)
67
    {
68
        file_put_contents(static::$magister->configPath().'/app.php', str_replace(
69
            static::$magister['config']['app.key'], $key, file_get_contents(static::$magister->configPath().'/app.php')
70
        ));
71
    }
72
73
    /**
74
     * Generate a random key for the application.
75
     *
76
     * @return string
77
     */
78
    protected static function generateRandomKey()
79
    {
80
        return str_random(32);
81
    }
82
}
83