Passed
Push — develop ( ef97e9...11ccd2 )
by Nikolay
04:33
created

CloudProvisioning::googleProvisioning()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 26
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 20
c 0
b 0
f 0
dl 0
loc 26
rs 9.6
cc 3
nc 2
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
use MikoPBX\Common\Models\PbxSettings;
23
use MikoPBX\Common\Models\PbxSettingsConstants;
24
use MikoPBX\Core\System\CloudProvisioning\GoogleCloud;
25
use MikoPBX\Core\System\CloudProvisioning\AzureCloud;
26
use MikoPBX\Core\System\CloudProvisioning\YandexCloud;
27
use MikoPBX\Core\System\CloudProvisioning\VKCloud;
28
29
30
/**
31
 * Class CloudProvisioning
32
 *
33
 * Handles the provisioning process for cloud solutions.
34
 *
35
 * @package MikoPBX\Core\System
36
 */
37
class CloudProvisioning
38
{
39
40
    /**
41
     * Starts the cloud provisioning process.
42
     */
43
    public static function start(): void
44
    {
45
        if (PbxSettings::findFirst('key="' . PbxSettingsConstants::CLOUD_PROVISIONING. '"')) {
46
            // Already processed before.
47
            return;
48
        }
49
50
        // Similar logic to before, but instantiate and use the new classes
51
        $providers = [
52
            new YandexCloud(),
53
            new VKCloud(),
54
            new GoogleCloud(),
55
            new AzureCloud(),
56
        ];
57
58
        foreach ($providers as $provider) {
59
            Util::sysLogMsg(__CLASS__, 'Try '.$provider::CLOUD_NAME.' provisioning... ');
60
            if ($provider->provision()) {
61
                self::afterProvisioning($provider);
62
                Util::sysLogMsg(__CLASS__, $provider::CLOUD_NAME.' provisioning completed.');
63
                // Provisioning succeeded, break out of the loop
64
                break;
65
            }
66
        }
67
    }
68
69
    /**
70
     * After provisioning, perform the following actions:
71
     * @param $provider mixed The provider object.
72
     * @return void
73
     */
74
    public static function afterProvisioning($provider): void
75
    {
76
        // Enable firewall and Fail2Ban
77
        $provider->updatePbxSettings(PbxSettingsConstants::PBX_FIREWALL_ENABLED, '1');
78
        $provider->updatePbxSettings(PbxSettingsConstants::PBX_FAIL2BAN_ENABLED, '1');
79
80
        // Connect storage
81
        $provider->checkConnectStorage();
82
83
        // Mark provisioning as completed
84
        $provider->updatePbxSettings(PbxSettingsConstants::CLOUD_PROVISIONING, '1');
85
        $provider->updatePbxSettings(PbxSettingsConstants::VIRTUAL_HARDWARE_TYPE, $provider::CLOUD_NAME);
86
    }
87
88
}