Passed
Push — master ( 9051a7...af40e2 )
by Gabriel
11:43 queued 18s
created

RecordTrait::canDelete()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 7
ccs 0
cts 4
cp 0
rs 10
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
namespace ByTIC\Payments\Models\Methods\Traits;
4
5
use ByTIC\Common\Payments\Models\Methods\Files\MobilpayFile;
6
use \ByTIC\Models\SmartProperties\RecordsTraits\HasTypes\RecordTrait as HasTypesRecordTrait;
7
use ByTIC\MediaLibrary\HasMedia\HasMediaTrait;
8
use ByTIC\MediaLibrary\HasMedia\Traits\AddMediaTrait;
9
use ByTIC\Payments\Gateways\Providers\AbstractGateway\Traits\GatewayTrait;
10
use ByTIC\Payments\Models\Methods\Types\AbstractType;
11
use ByTIC\Payments\Models\Methods\Types\CreditCards;
12
use Nip\Records\RecordManager;
13
14
/**
15
 * Class MethodTrait
16
 * @package ByTIC\Payments\Models\Methods\Traits
17
 *
18
 * @property string $name
19
 * @property string $internal_name
20
 * @property string $description
21
 * @property string $__notes
22
 *
23
 * @method AbstractType|CreditCards getType
24
 * @method RecordsTrait|RecordManager getManager()
25
 */
26
trait RecordTrait
27
{
28
    use HasTypesRecordTrait {
29
        setType as setTypeTrait;
30
    }
31
    use \ByTIC\Common\Records\Traits\HasSerializedOptions\RecordTrait;
32
33
    use HasMediaTrait;
34
35
    /**
36
     * @param array $data
37
     * @return mixed
38
     */
39
    public function fill($data = [])
40
    {
41
        if (!isset($data['type'])) {
42
            $data['type'] = 'bank-transfer';
43
        }
44
45
        return parent::fill($data);
46
    }
47
48
    /**
49
     * @param bool $type
50
     * @return string
51
     */
52
    public function getName($type = false)
53
    {
54
        if ($type == 'internal') {
55
            return $this->getAttributeFromArray('internal_name');
0 ignored issues
show
Bug introduced by
It seems like getAttributeFromArray() 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

55
            return $this->/** @scrutinizer ignore-call */ getAttributeFromArray('internal_name');
Loading history...
56
        }
57
        return $this->getAttributeFromArray('name');
58
    }
59
60
    /**
61
     * @return bool
62
     */
63
    public function checkConfirmRedirect()
64
    {
65
        if ($this->getType()->checkConfirmRedirect()) {
66
            return true;
67
        }
68
69
        return false;
70
    }
71
72
    /**
73
     * @return bool|string
74
     */
75
    public function getEntryDescription()
76
    {
77
        $return = $this->getType()->getEntryDescription();
78
        $return .= $this->getDescription();
79
        $return .= $this->__notes;
80
81
        return $return;
82
    }
83
84
    /**
85
     * @return string
86
     */
87
    public function getDescription()
88
    {
89
        return $this->getAttributeFromArray('description');
90
    }
91
92
    /**
93
     * @return bool
94
     */
95
    public function canDelete()
96
    {
97
        if ($this->getPurchasesCount() > 0) {
98
            return $this->getManager()->getMessage('delete.denied.has-purchases');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getManager....denied.has-purchases') also could return the type ByTIC\Payments\Models\Me...p\Records\RecordManager which is incompatible with the documented return type boolean.
Loading history...
99
        }
100
101
        return true;
102
    }
103
104
    /**
105
     * @return int
106
     */
107
    abstract public function getPurchasesCount();
108
109
    /**
110
     * @return bool|GatewayTrait|null
111
     */
112
    public function getGateway()
113
    {
114
        if ($this->getType() instanceof CreditCards) {
115
            return $this->getType()->getGateway();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getType()->getGateway() also could return the type Omnipay\Common\GatewayInterface which is incompatible with the documented return type ByTIC\Payments\Gateways\...tewayTrait|boolean|null.
Loading history...
116
        }
117
118
        return false;
119
    }
120
121
    /**
122
     * @inheritDoc
123
     */
124 24
    public function setType($type = null)
125
    {
126 24
        $paymentGatewaysNames = \ByTIC\Payments\Gateways\Manager::getAll()->keys();
127 24
        if (in_array($type, $paymentGatewaysNames)) {
128 2
            $this->setOption('payment_gateway', $type);
129 2
            $type = 'credit-cards';
130
        }
131 24
        return $this->setTypeTrait($type);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->setTypeTrait($type) targeting ByTIC\Payments\Models\Me...\RecordTrait::setType() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
132
    }
133
134
    /**
135
     * @return mixed
136
     */
137 1
    public function getPaymentGatewayOptions()
138
    {
139 1
        $gatewayName = $this->getOption('payment_gateway');
140
141 1
        return $this->getOption($gatewayName);
142
    }
143
144
    /**
145
     * @param array $options
146
     * @param null $gatewayName
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $gatewayName is correct as it would always require null to be passed?
Loading history...
147
     * @return mixed
148
     */
149 1
    public function setPaymentGatewayOptions($options, $gatewayName = null)
150
    {
151 1
        $gatewayName = $gatewayName? $gatewayName : $this->getOption('payment_gateway');
0 ignored issues
show
introduced by
$gatewayName is of type null, thus it always evaluated to false.
Loading history...
152
153 1
        return $this->setOption($gatewayName, $options);
154
    }
155
156
    /**
157
     * @param null $type
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $type is correct as it would always require null to be passed?
Loading history...
158
     * @return string
159
     */
160
    public function getFileModelName($type = null)
161
    {
162
        if ($type == 'Mobilpay') {
163
            return MobilpayFile::class;
164
        }
165
        return $this->getFileModelNameAbstract($type);
0 ignored issues
show
Bug introduced by
The method getFileModelNameAbstract() does not exist on ByTIC\Payments\Models\Methods\Traits\RecordTrait. Did you maybe mean getFileModelName()? ( Ignorable by Annotation )

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

165
        return $this->/** @scrutinizer ignore-call */ getFileModelNameAbstract($type);

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...
166
    }
167
}
168