Kavenegar::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 2
rs 10
1
<?php
2
3
namespace HoomanMirghasemi\Sms\Drivers;
4
5
use HoomanMirghasemi\Sms\Abstracts\Driver;
6
use Kavenegar\KavenegarApi;
7
8
class Kavenegar extends Driver
9
{
10
    protected string $from = '';
11
12
    public function __construct(protected array $settings, private KavenegarApi $kavenegarApi)
13
    {
14
    }
15
16
    public function getBalance(): string
17
    {
18
        return '';
19
    }
20
21
    public function send(): bool
22
    {
23
        try {
24
            if ($this->message->usesTemplate()) {
0 ignored issues
show
Bug introduced by
The method usesTemplate() does not exist on HoomanMirghasemi\Sms\Contracts\Message. Since it exists in all sub-types, consider adding an abstract or default implementation to HoomanMirghasemi\Sms\Contracts\Message. ( Ignorable by Annotation )

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

24
            if ($this->message->/** @scrutinizer ignore-call */ usesTemplate()) {
Loading history...
25
                $template = $this->message->getTemplate();
0 ignored issues
show
Bug introduced by
The method getTemplate() does not exist on HoomanMirghasemi\Sms\Contracts\Message. Since it exists in all sub-types, consider adding an abstract or default implementation to HoomanMirghasemi\Sms\Contracts\Message. ( Ignorable by Annotation )

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

25
                /** @scrutinizer ignore-call */ 
26
                $template = $this->message->getTemplate();
Loading history...
26
                $identifier = $template['identifier'];
27
                $params = $template['params'];
28
                $token1 = str_replace(' ', '-', $params['token1'] ?? '');
29
                $token2 = str_replace(' ', '-', $params['token2'] ?? '');
30
                $token3 = str_replace(' ', '-', $params['token3'] ?? '');
31
32
                $token10 = $this->replaceExtraSpace($params['token10'] ?? '', 4);
33
                $token20 = $this->replaceExtraSpace($params['token20'] ?? '', 8);
34
35
                $result = $this->kavenegarApi->VerifyLookup(
36
                    $this->recipient,
37
                    $token1,
38
                    $token2,
39
                    $token3,
40
                    $identifier,
41
                    null,
42
                    $token10,
43
                    $token20
44
                );
45
            } else {
46
                $result = $this->kavenegarApi->Send(null, $this->recipient, $this->message->toString());
47
            }
48
49
            $this->success = true;
50
            $this->from = $result[0]->sender;
51
            $this->message = $result[0]->message;
52
            $this->webserviceResponse = print_r($result, true);
0 ignored issues
show
Documentation Bug introduced by
It seems like print_r($result, true) can also be of type true. However, the property $webserviceResponse is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
53
        } catch (\Exception $exception) {
54
            $this->webserviceResponse = $exception->getMessage();
55
            $this->success = false;
56
        }
57
58
        return parent::send();
59
    }
60
61
    private function replaceExtraSpace($string, $maxSpace)
62
    {
63
        $spaceCount = substr_count($string, ' ');
64
        if ($spaceCount > $maxSpace) {
65
            $string = strrev(preg_replace('/ /', '-', strrev($string), $spaceCount - $maxSpace));
66
        }
67
68
        return $string;
69
    }
70
}
71