RecordTrait::getDescription()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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

53
            return $this->/** @scrutinizer ignore-call */ getAttributeFromArray('internal_name');
Loading history...
54
        }
55
        return $this->getAttributeFromArray('name');
56
    }
57
58
    /**
59
     * @return bool
60
     */
61
    public function checkConfirmRedirect()
62
    {
63
        if ($this->getType()->checkConfirmRedirect()) {
64
            return true;
65
        }
66
67
        return false;
68
    }
69
70
    /**
71
     * @return bool|string
72
     */
73
    public function getEntryDescription()
74
    {
75
        $return = $this->getType()->getEntryDescription();
76
        $return .= $this->getDescription();
77
        $return .= $this->__notes;
78
79
        return $return;
80
    }
81
82
    /**
83
     * @return string
84
     */
85
    public function getDescription()
86
    {
87
        return $this->getAttributeFromArray('description');
88
    }
89
90
    /**
91
     * @return bool
92
     */
93
    public function canDelete()
94
    {
95
        if ($this->getPurchasesCount() > 0) {
96
            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...
97
        }
98
99
        return true;
100
    }
101
102
    /**
103
     * @return int
104
     */
105
    abstract public function getPurchasesCount();
106
107
    /**
108
     * @return bool|GatewayTrait|null
109
     */
110
    public function getGateway()
111
    {
112
        if ($this->getType() instanceof CreditCards) {
113
            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...
114
        }
115
116
        return false;
117
    }
118
119
    /**
120
     * @inheritDoc
121
     */
122
    public function setType($type = null)
123
    {
124 24
        $paymentGatewaysNames = \ByTIC\Payments\Gateways\Manager::getAll()->keys();
125
        if (in_array($type, $paymentGatewaysNames)) {
126 24
            $this->setOption('payment_gateway', $type);
127 24
            $type = 'credit-cards';
128 2
        }
129 2
        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...
130
    }
131 24
132
    /**
133
     * @return mixed
134
     */
135
    public function getPaymentGatewayOptions()
136
    {
137 1
        $gatewayName = $this->getOption('payment_gateway');
138
139 1
        return $this->getOption($gatewayName);
140
    }
141 1
142
    /**
143
     * @param array $options
144
     * @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...
145
     * @return mixed
146
     */
147
    public function setPaymentGatewayOptions($options, $gatewayName = null)
148
    {
149 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...
150
151 1
        return $this->setOption($gatewayName, $options);
152
    }
153
}
154