BootstrapCommand::handle()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 2.0009

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 14
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 26
ccs 15
cts 16
cp 0.9375
crap 2.0009
rs 9.7998
1
<?php
2
/**
3
 * BootstrapCommand.
4
 */
5
6
namespace Bmatovu\MtnMomo\Console;
7
8
use Bmatovu\MtnMomo\Traits\CommandUtilTrait;
9
use Illuminate\Console\Command;
10
11
/**
12
 * Bootstrap integration.
13
 */
14
class BootstrapCommand extends Command
15
{
16
    use CommandUtilTrait;
17
18
    /**
19
     * The name and signature of the console command.
20
     *
21
     * @var string
22
     */
23
    protected $signature = 'mtn-momo:init
24
                                {--no-write : Don\'t write credentials to .env file.}
25
                                {--f|force : Force the operation to run when in production.}';
26
27
    /**
28
     * The console command description.
29
     *
30
     * @var string
31
     */
32
    protected $description = 'Bootstrap/setup integration.';
33
34
    /**
35
     * Create a new command instance.
36
     */
37 25
    public function __construct()
38
    {
39 25
        parent::__construct();
40
    }
41
42
    /**
43
     * Execute the console command.
44
     *
45
     * @return void
46
     */
47 7
    public function handle()
48
    {
49 7
        if (! $this->runInProduction()) {
50
            return;
51
        }
52
53 7
        $this->printLabels('MTN MOMO API integration', [
54 7
            'Please enter the values for the following settings,',
55 7
            "Or press 'Enter' to accept the given default values in square brackets.",
56 7
            'These settings will be written to your .env',
57 7
        ]);
58
59 7
        $this->setClientName();
60
61 7
        $this->setEnvironment();
62
63 7
        $this->setCurrency();
64
65 7
        $this->setProviderCallbackHost();
66
67 7
        $this->setProductName();
68
69 7
        $this->setProductSubscriptionKey();
70
71 7
        $this->info("\r\nNext: Register your client application's ID.");
72 7
        $this->line("\r\n>>> php artisan mtn-momo:register-id");
73
    }
74
75
    /**
76
     * Create/update client APP name.
77
     *
78
     * @return void
79
     */
80 7
    protected function setClientName()
81
    {
82 7
        $this->printLabels('Client APP name', [
83 7
            'May be indicated in the message sent to the payee',
84 7
        ]);
85
86 7
        $clientName = $this->laravel['config']->get('mtn-momo.app');
87 7
        $clientName = $this->ask('MOMO_APP', $clientName);
88
89 7
        $this->updateSetting('MOMO_APP', 'mtn-momo.app', $clientName);
90
    }
91
92
    /**
93
     * Create/update target environment.
94
     *
95
     * @return void
96
     */
97 7
    protected function setEnvironment()
98
    {
99 7
        $this->printLabels('Environment', [
100 7
            'Also called: <options=bold>targetEnvironment</>.',
101 7
        ]);
102
103 7
        $environment = $this->laravel['config']->get('mtn-momo.environment');
104 7
        $environments = ['sandbox', 'live'];
105 7
        $index = array_search($environment, $environments);
106 7
        $default = ($index === false) ? null : $index;
107 7
        $environment = $this->choice('MOMO_ENVIRONMENT', $environments, $default);
108
109 7
        $this->updateSetting('MOMO_ENVIRONMENT', 'mtn-momo.environment', $environment);
0 ignored issues
show
Bug introduced by
It seems like $environment can also be of type array; however, parameter $value of Bmatovu\MtnMomo\Console\...ommand::updateSetting() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

109
        $this->updateSetting('MOMO_ENVIRONMENT', 'mtn-momo.environment', /** @scrutinizer ignore-type */ $environment);
Loading history...
110
    }
111
112
    /**
113
     * Create/update currency.
114
     *
115
     * @return void
116
     */
117 7
    protected function setCurrency()
118
    {
119 7
        $this->printLabels('Currency', [
120 7
            "Use 'EUR' for sandbox environment",
121 7
        ]);
122
123 7
        $currency = $this->laravel['config']->get('mtn-momo.currency');
124 7
        $currency = $this->ask('MOMO_CURRENCY', $currency);
125
126 7
        $this->updateSetting('MOMO_CURRENCY', 'mtn-momo.currency', strtoupper($currency));
127
    }
128
129
    /**
130
     * Create/update currency.
131
     *
132
     * @return void
133
     */
134 7
    protected function setProviderCallbackHost()
135
    {
136 7
        $this->printLabels('Provider Callback Host', ['Your server hostname']);
137
138 7
        $providerCallbackHost = $this->laravel['config']->get('mtn-momo.provider_callback_host');
139 7
        $providerCallbackHost = $this->ask('MOMO_PROVIDER_CALLBACK_HOST', $providerCallbackHost);
140
141 7
        $this->updateSetting('MOMO_PROVIDER_CALLBACK_HOST', 'mtn-momo.provider_callback_host', $providerCallbackHost);
142
    }
143
144
    /**
145
     * Create/update target product subscribed too.
146
     *
147
     * @return void
148
     */
149 7
    protected function setProductName()
150
    {
151 7
        $this->printLabels('Product');
152
153 7
        $product = $this->laravel['config']->get('mtn-momo.product');
154 7
        $products = ['collection', 'disbursement', 'remittance'];
155 7
        $index = array_search($product, $products);
156 7
        $default = ($index === false) ? 0 : $index;
157
158 7
        $product = $this->choice('MOMO_PRODUCT', $products, $default);
159
160 7
        $this->updateSetting('MOMO_PRODUCT', 'mtn-momo.product', $product);
0 ignored issues
show
Bug introduced by
It seems like $product can also be of type array; however, parameter $value of Bmatovu\MtnMomo\Console\...ommand::updateSetting() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

160
        $this->updateSetting('MOMO_PRODUCT', 'mtn-momo.product', /** @scrutinizer ignore-type */ $product);
Loading history...
161
    }
162
163
    /**
164
     * Create/update product subscription key.
165
     *
166
     * @return void
167
     */
168 7
    protected function setProductSubscriptionKey()
169
    {
170 7
        $this->printLabels('Product subscription key', [
171 7
            'Also called: <options=bold>Ocp-Apim-Subscription-Key</>.',
172 7
        ]);
173
174 7
        $product = $this->laravel['config']->get('mtn-momo.product');
175
176 7
        $uProduct = strtoupper($product);
177
178 7
        $productKey = $this->laravel['config']->get("mtn-momo.products.{$product}.key");
179 7
        $productKey = $this->ask("MOMO_{$uProduct}_SUBSCRIPTION_KEY", $productKey);
180
181 7
        $this->updateSetting("MOMO_{$uProduct}_SUBSCRIPTION_KEY", "mtn-momo.products.{$product}.key", $productKey);
182
    }
183
}
184