InstallController::prepare()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 35
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
cc 3
eloc 22
c 3
b 1
f 0
nc 4
nop 1
dl 0
loc 35
rs 9.568
1
<?php
2
3
/**
4
 *  * Install controller
5
 *  *
6
 * The file contains every necessary functions for installation.
7
 *
8
 *  * @category   Controllers
9
 *  * @package    SuperHive
10
 *  * @author     Florent Kosmala <[email protected]>
11
 *  * @license    https://www.gnu.org/licenses/gpl-3.0.txt GPL-3.0
12
 *  */
13
14
declare(strict_types=1);
15
16
namespace App\Controllers;
17
18
use Psr\Container\ContainerInterface;
19
use Psr\Http\Message\ResponseInterface as Response;
20
use Psr\Http\Message\ServerRequestInterface as Request;
21
22
final class InstallController
23
{
24
    private ContainerInterface $app;
25
26
    public function __construct(ContainerInterface $app)
27
    {
28
        $this->app = $app;
29
    }
30
31
    /**
32
     *  * Prepare function
33
     *  *
34
     * This function called after Composer installation.
35
     * It will display the page with requirements check, account creation and
36
     * the form to create password file with encreypted key
37
     *
38
     * @param Response $response
39
     */
40
    public function prepare(Response $response): Response
41
    {
42
        $requirements = [];
43
44
        /* Check if PHP version is ok tu run SuperHive */
45
        if (version_compare(PHP_VERSION, '8.1.0', '>=')) {
46
            $req = [
47
                'status' => 'success',
48
                'message' => 'Your PHP version can run SuperHive. PHP version: ' . PHP_VERSION,
49
            ];
50
        } else {
51
            $req = [
52
                'status' => 'error',
53
                'message' => 'Please, update your PHP version to run SuperHive. (PHP 8.1 minimum)',
54
            ];
55
        }
56
        $requirements[] = $req;
57
58
        /* Check if data folder is writeable */
59
        $datadir = $this->app->get('datadir');
60
        if (is_writable($datadir)) {
61
            $req = [
62
                'status' => 'success',
63
                'message' => 'The data folder is writable to store blockchain data.',
64
            ];
65
        } else {
66
            $req = [
67
                'status' => 'error',
68
                'message' => 'Make the data folder writable.',
69
            ];
70
        }
71
        $requirements[] = $req;
72
73
        return $this->app->get('view')->render($response, '/setup.html', [
74
            'requirements' => $requirements,
75
        ]);
76
    }
77
78
    /**
79
     *  * Install function
80
     *  *
81
     * This function dis called after the prepare function to generate the file and store it in ocnfig folder.
82
     *
83
     * @param Request $request
84
     * @param Response $response
85
     */
86
    public function install(Request $request, Response $response): Response
87
    {
88
        if (!file_exists($this->app->get('password'))) {
89
            $data = $request->getParsedBody();
90
            // Create password file with username and password (signed msg))
91
            if ((isset($data['username'])) && (isset($data['passwd']))) {
92
                $cred = [$data['username'] => $data['passwd']];
93
                file_put_contents($this->app->get('password'), serialize($cred));
94
95
                // Change account name in config file
96
                $settings = $this->app->get('settings');
97
                $settings['author'] = $data['username'];
98
                $file = json_encode($settings, JSON_PRETTY_PRINT);
99
                file_put_contents($this->app->get('configfile'), $file);
100
101
                $response->getBody()->write('ok');
102
            }
103
        } else {
104
            $response->getBody()->write('notok');
105
        }
106
107
        return $response;
108
    }
109
}
110