Passed
Push — develop ( 49e3f5...56ecd0 )
by Nikolay
04:35
created

PBXRecovery::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
dl 0
loc 6
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/*
3
 * MikoPBX - free phone system for small business
4
 * Copyright © 2017-2023 Alexey Portnov and Nikolay Beketov
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with this program.
17
 * If not, see <https://www.gnu.org/licenses/>.
18
 */
19
20
namespace MikoPBX\Core\System;
21
22
/**
23
 * Class PBXRecovery
24
 *
25
 * Class responsible for managing the recovery process of a PBX system.
26
 *
27
 * @package MikoPBX\Core\System
28
 */
29
class PBXRecovery
30
{
31
    // ASCII codes for colored output
32
    const REDON    = "\033[31;1m";
33
    const REDOFF   = "\033[0m";
34
    const GREENON  = "\033[32;1m";
35
    const GREENOFF = "\033[0m";
36
37
    // Storage instance
38
    private Storage $storage;
39
40
    // Recover disk name
41
    private string $DEVICE;
42
43
    // Version info
44
    private string $VERSION;
45
46
    // File pointer
47
    private $fp;
48
49
    /**
50
     * PBXRecovery constructor.
51
     *
52
     * Initializes file pointer, storage, disk name and version
53
     */
54
    public function __construct()
55
    {
56
        $this->fp      = fopen('php://stdin', 'r');
57
        $this->storage = new Storage();
58
        $this->DEVICE  = $this->storage->getRecoverDiskName();
59
        $this->VERSION = trim(file_get_contents('/offload/version'));
60
    }
61
62
    /**
63
     * Main runner method
64
     *
65
     * Clears the terminal and provides the user with recovery options.
66
     * Handles the user's selection input.
67
     */
68
    public function run()
69
    {
70
        system('clear');
71
        echo "\n";
72
        echo Util::translate("Install or recovery")."\n";
73
        echo "*******************************\n";
74
        echo "1) ".Util::translate('Install').".   ".self::REDON.Util::translate('All settings will be lost!').self::REDOFF."\n";
75
        echo "2) ".Util::translate('Reinstall to')." ".$this->VERSION. ". ".self::GREENON.Util::translate('All settings will be kept!').self::GREENOFF."\n";
76
        echo "3) ".Util::translate('Cancel')."\n\n";
77
        echo Util::translate('Enter a number').": ";
78
79
        $input = trim(fgets($this->fp));
80
        $this->handleInput($input);
0 ignored issues
show
Bug introduced by
$input of type string is incompatible with the type integer expected by parameter $input of MikoPBX\Core\System\PBXRecovery::handleInput(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

80
        $this->handleInput(/** @scrutinizer ignore-type */ $input);
Loading history...
81
    }
82
83
    /**
84
     * Handle user input
85
     *
86
     * Executes the appropriate recovery process based on the user's selection.
87
     *
88
     * @param int $input User's selection input
89
     */
90
    private function handleInput(int $input)
91
    {
92
        switch ($input) {
93
            case 1:
94
                // Prepare for installation
95
                file_put_contents('/tmp/ejectcd', '');
96
                $installer = new PBXInstaller();
97
                $installer->run();
98
                sleep(300);
99
                break;
100
            case 2:
101
                // Prepare for reinstalling
102
                file_put_contents('/tmp/ejectcd', '');
103
                $pbx_firmwarePath = Util::which('pbx_firmware');
104
                Processes::mwExecBg("{$pbx_firmwarePath} /offload/firmware.img.gz {$this->DEVICE}");
105
                sleep(600);
106
                break;
107
            case 3:
108
                // Cancel operation
109
                sleep(2);
110
                break;
111
        }
112
    }
113
}