Passed
Push — master ( ff7474...c002dd )
by Gabriel
05:50
created

HasModelProcessedResponse   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Test Coverage

Coverage 28.21%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 33
c 1
b 0
f 1
dl 0
loc 92
ccs 11
cts 39
cp 0.2821
rs 10
wmc 19

8 Methods

Rating   Name   Duplication   Size   Complexity  
A processModelStatus() 0 13 6
A canProcessModel() 0 3 1
A getModel() 0 3 1
A processModel() 0 8 2
A hasModel() 0 3 1
A processModelData() 0 2 1
A getTransactionDate() 0 6 2
A getModelResponseStatus() 0 16 5
1
<?php
2
3
namespace ByTIC\Payments\Gateways\Providers\AbstractGateway\Message\Traits;
4
5
use ByTIC\Common\Payments\Models\Purchase\Traits\IsPurchasableModelTrait;
6
use ByTIC\Common\Records\Traits\HasStatus\RecordTrait;
7
use Nip\Records\Record;
8
9
/**
10
 * Class HasView
11
 * @package ByTIC\Payments\Gateways\Providers\AbstractGateway\Message\Traits
12
 *
13
 */
14
trait HasModelProcessedResponse
15
{
16
    protected $modelResponseStatus = null;
17
18
    /**
19
     * @return $this
20
     */
21
    public function processModel()
22
    {
23
        if ($this->canProcessModel()) {
24
            $this->processModelStatus();
25
            $this->processModelData();
26
        }
27
28
        return $this;
29
    }
30
31
    /**
32
     * @return bool
33
     */
34
    protected function canProcessModel()
35
    {
36
        return false;
37
    }
38
39
    protected function processModelStatus()
40
    {
41
        $model = $this->getModel();
42
        $modelStatus = $this->getModel()->getStatus()->getName();
0 ignored issues
show
Bug introduced by
It seems like getStatus() 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

42
        $modelStatus = $this->getModel()->/** @scrutinizer ignore-call */ getStatus()->getName();
Loading history...
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

42
        $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...
43
        $newModelStatus = $this->getModelResponseStatus();
44
        if ($newModelStatus && $modelStatus !== $newModelStatus) {
45
            if ($newModelStatus == 'active') {
46
                $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...
47
                $model->updateStatus($newModelStatus);
0 ignored issues
show
Bug introduced by
It seems like updateStatus() 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

47
                $model->/** @scrutinizer ignore-call */ 
48
                        updateStatus($newModelStatus);
Loading history...
Bug introduced by
$newModelStatus of type string is incompatible with the type boolean expected by parameter $status of ByTIC\Common\Records\Tra...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

47
                $model->updateStatus(/** @scrutinizer ignore-type */ $newModelStatus);
Loading history...
48
            } elseif ($modelStatus == 'active' && $newModelStatus = 'error') {
49
                // ignore error status after active received
50
            } else {
51
                $model->updateStatus($newModelStatus);
52
            }
53
        }
54
    }
55
56
    /**
57
     * @return Record|RecordTrait|IsPurchasableModelTrait
58
     */
59 4
    public function getModel()
60
    {
61 4
        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

61
        return $this->/** @scrutinizer ignore-call */ getDataProperty('model');
Loading history...
62
    }
63
64
    /**
65
     * @return null|string
66
     */
67 4
    public function getModelResponseStatus()
68
    {
69 4
        if ($this->modelResponseStatus === null) {
70 4
            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

70
            if ($this->/** @scrutinizer ignore-call */ isSuccessful()) {
Loading history...
71
                $status = 'active';
72 4
            } 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

72
            } elseif ($this->/** @scrutinizer ignore-call */ isCancelled()) {
Loading history...
73
                $status = 'canceled';
74 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

74
            } elseif ($this->/** @scrutinizer ignore-call */ isPending()) {
Loading history...
75 1
                $status = 'pending';
76
            } else {
77 3
                $status = 'error';
78
            }
79 4
            $this->modelResponseStatus = $status;
80
        }
81
82 4
        return $this->modelResponseStatus;
83
    }
84
85
    /**
86
     * @return false|string
87
     */
88
    public function getTransactionDate()
89
    {
90
        if (is_callable('parent::getTransactionDate')) {
91
            return parent::getTransactionDate();
92
        }
93
        return date('Y-m-d H:i:s');
94
    }
95
96
    public function processModelData()
97
    {
98
    }
99
100
    /**
101
     * @return bool
102
     */
103
    public function hasModel()
104
    {
105
        return is_object($this->getModel());
106
    }
107
}
108