Passed
Push — master ( 99a1a7...209f8f )
by Gabriel
06:24
created

RecordTrait   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Test Coverage

Coverage 9.3%

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 19
eloc 37
c 4
b 1
f 0
dl 0
loc 140
ccs 4
cts 43
cp 0.093
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getEntryDescription() 0 7 1
A writeData() 0 7 2
A getFileModelName() 0 6 2
A getDescription() 0 3 1
A getName() 0 6 2
A checkConfirmRedirect() 0 7 2
A getGateway() 0 7 2
A canDelete() 0 7 2
A getPaymentGatewayOptions() 0 5 1
A setType() 0 8 2
A setPaymentGatewayOptions() 0 5 2
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 writeData($data = [])
40
    {
41
        if (!$data['type']) {
42
            $data['type'] = 'bank-transfer';
43
        }
44
45
        return parent::writeData($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->internal_name;
56
        }
57
        return $this->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->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()->getName() == 'credit-cards') {
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...
Bug introduced by
The method getGateway() does not exist on ByTIC\Payments\Models\Methods\Types\AbstractType. It seems like you code against a sub-type of ByTIC\Payments\Models\Methods\Types\AbstractType such as ByTIC\Payments\Models\Methods\Types\CreditCards. ( Ignorable by Annotation )

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

115
            return $this->getType()->/** @scrutinizer ignore-call */ getGateway();
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::getCollection()->keys();
127 24
        if (in_array($type, $paymentGatewaysNames)) {
128
            $this->setOption('payment_gateway', $type);
129
            $type = 'credit-cards';
130
        }
131 24
        return $this->setTypeTrait($type);
132
    }
133
134
    /**
135
     * @return mixed
136
     */
137
    public function getPaymentGatewayOptions()
138
    {
139
        $gatewayName = $this->getOption('payment_gateway');
140
141
        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
    public function setPaymentGatewayOptions($options, $gatewayName = null)
150
    {
151
        $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
        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