HasModelProcessedResponse::processModelStatus()   A
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 6
eloc 10
nc 4
nop 0
dl 0
loc 16
rs 9.2222
c 1
b 0
f 1
ccs 0
cts 8
cp 0
crap 42
1
<?php
2
3
namespace ByTIC\Payments\Gateways\Providers\AbstractGateway\Message\Traits;
4
5
use ByTIC\Models\SmartProperties\RecordsTraits\HasStatus\RecordTrait;
6
use ByTIC\Payments\Models\Purchase\Traits\IsPurchasableModelTrait;
7
use ByTIC\Payments\Models\Transactions\Statuses\Active;
8
use ByTIC\Payments\Models\Transactions\Statuses\Canceled;
9
use ByTIC\Payments\Models\Transactions\Statuses\Error;
10
use ByTIC\Payments\Models\Transactions\Statuses\Pending;
11
use Nip\Records\Record;
12
13
/**
14
 * Class HasView
15
 * @package ByTIC\Payments\Gateways\Providers\AbstractGateway\Message\Traits
16
 *
17
 */
18
trait HasModelProcessedResponse
19
{
20
    protected $modelResponseStatus = null;
21
22
    /**
23
     * @return $this
24
     */
25
    public function processModel()
26
    {
27
        if ($this->canProcessModel()) {
28
            $this->processModelStatus();
29
            $this->processModelData();
30
        }
31
32
        return $this;
33
    }
34
35
    /**
36
     * @return bool
37
     */
38
    protected function canProcessModel()
39
    {
40
        return false;
41
    }
42
43
    protected function processModelStatus()
44
    {
45
        $model = $this->getModel();
46
        $modelStatus = $this->getModel()->getStatus()->getName();
0 ignored issues
show
Bug introduced by
The method getName() does not exist on Nip\Records\Collections\Collection. ( Ignorable by Annotation )

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

46
        $modelStatus = $this->getModel()->getStatus()->/** @scrutinizer ignore-call */ getName();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
47
        $newModelStatus = $this->getModelResponseStatus();
48
        if (!$newModelStatus || $modelStatus === $newModelStatus) {
49
            return;
50
        }
51
        if ($modelStatus == 'active' && $newModelStatus == 'error') {
52
            // ignore error status after active received
53
            return;
54
        }
55
        if ($newModelStatus == 'active') {
56
            $model->received = $this->getTransactionDate();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getTransactionDate() can also be of type false. However, the property $received 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...
57
        }
58
        $model->updateStatus($newModelStatus);
0 ignored issues
show
Bug introduced by
The method updateStatus() does not exist on ByTIC\Payments\Models\Pu...IsPurchasableModelTrait. Did you maybe mean update()? ( Ignorable by Annotation )

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

58
        $model->/** @scrutinizer ignore-call */ 
59
                updateStatus($newModelStatus);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
$newModelStatus of type string is incompatible with the type boolean expected by parameter $status of ByTIC\Models\SmartProper...rdTrait::updateStatus(). ( Ignorable by Annotation )

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

58
        $model->updateStatus(/** @scrutinizer ignore-type */ $newModelStatus);
Loading history...
59 4
    }
60
61 4
    /**
62
     * @return Record|RecordTrait|IsPurchasableModelTrait
63
     */
64
    public function getModel()
65
    {
66
        return $this->getDataProperty('model');
0 ignored issues
show
Bug introduced by
It seems like getDataProperty() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

66
        return $this->/** @scrutinizer ignore-call */ getDataProperty('model');
Loading history...
67 4
    }
68
69 4
    /**
70 4
     * @return null|string
71
     */
72 4
    public function getModelResponseStatus(): ?string
73
    {
74 4
        if ($this->modelResponseStatus === null) {
75 1
            if ($this->isSuccessful()) {
0 ignored issues
show
Bug introduced by
It seems like isSuccessful() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

75
            if ($this->/** @scrutinizer ignore-call */ isSuccessful()) {
Loading history...
76
                $status = Active::NAME;
77 3
            } elseif ($this->isCancelled()) {
0 ignored issues
show
Bug introduced by
It seems like isCancelled() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

77
            } elseif ($this->/** @scrutinizer ignore-call */ isCancelled()) {
Loading history...
78
                $status = Canceled::NAME;
79 4
            } elseif ($this->isPending()) {
0 ignored issues
show
Bug introduced by
It seems like isPending() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

79
            } elseif ($this->/** @scrutinizer ignore-call */ isPending()) {
Loading history...
80
                $status = Pending::NAME;
81
            } else {
82 4
                $status = Error::NAME;
83
            }
84
            $this->modelResponseStatus = $status;
85
        }
86
87
        return $this->modelResponseStatus;
88
    }
89
90
    /**
91
     * @return false|string
92
     */
93
    public function getTransactionDate()
94
    {
95
        if (is_callable('parent::getTransactionDate')) {
96
            /** @noinspection PhpUndefinedMethodInspection @phpstan-ignore-next-line */
97
            return parent::getTransactionDate();
98
        }
99
        return date('Y-m-d H:i:s');
100
    }
101
102
    public function processModelData()
103
    {
104
    }
105
106
    /**
107
     * @return bool
108
     */
109
    public function hasModel()
110
    {
111
        return is_object($this->getModel());
112
    }
113
}
114