CheckLicenseAction::execute()   A
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 5
eloc 15
c 3
b 0
f 0
nc 6
nop 2
dl 0
loc 26
rs 9.4555
1
<?php
2
declare(strict_types=1);
3
4
namespace rosasurfer\bfx\controller\actions;
5
6
use rosasurfer\ministruts\config\ConfigInterface as Config;
7
use rosasurfer\ministruts\log\Logger;
8
use rosasurfer\ministruts\struts\Action;
9
use rosasurfer\ministruts\struts\Request;
10
use rosasurfer\ministruts\struts\Response;
11
12
use function rosasurfer\ministruts\strIsDigits;
13
14
use const rosasurfer\ministruts\L_INFO;
15
use const rosasurfer\ministruts\MONTH;
16
17
18
/**
19
 * CheckLicenseAction
20
 */
21
class CheckLicenseAction extends Action {
22
23
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function execute(Request $request, Response $response) {
28
        /** @var Config $config */
29
        $config = $this->di()['config'];
30
31
        // request:  GET /Paypal/TFV/index.php?id=TSR&lic={encoded-license}&rn=9&mt4={account}&ec=1
32
        // response: {account}|{plain-license}|A|{expiration}|mt4tfv|ok
33
        //       or: ERROR: {error-msg}|no
34
35
        $knownAccount = false;
36
        $input = $request->input();
37
        $account = $input->get('mt4', '');
38
39
        if (strIsDigits($account)) {
0 ignored issues
show
Bug introduced by
It seems like $account can also be of type null; however, parameter $string of rosasurfer\ministruts\strIsDigits() 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

39
        if (strIsDigits(/** @scrutinizer ignore-type */ $account)) {
Loading history...
40
            $license = $config->get('bfx.accounts.'.$account, null);
41
            $knownAccount = !is_null($license);
42
            $license = $license ?: $config->get('bfx.accounts.default');
43
            $expires = date('Ymd', time() + 1*MONTH);                       // extend license for 30 days
44
            $reply = $account.'|'.$license.'|A|'.$expires.'|mt4tfv|ok';
45
        }
46
        else {
47
            $reply = 'ERROR: '.($account=="" ? 'Missing':'Invalid').' account number.|no';
48
        }
49
        $knownAccount || Logger::log('reply: '.$reply, L_INFO);             // log requests for unknown accounts
50
51
        echo $reply;
52
        return null;
53
    }
54
}
55