Completed
Push — master ( 842c53...e59924 )
by Joachim
14:33
created

ShippingMethod::populateFromApiResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 2
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Loevgaard\DandomainFoundation\Entity;
4
5
use Doctrine\ORM\Mapping as ORM;
6
use Loevgaard\DandomainFoundation;
7
use Loevgaard\DandomainFoundation\Entity\Generated\ShippingMethodInterface;
8
use Loevgaard\DandomainFoundation\Entity\Generated\ShippingMethodTraits;
9
use Money\Currency;
10
use Money\Money;
11
12
/**
13
 * @ORM\Entity()
14
 * @ORM\Table(name="loevgaard_dandomain_shipping_methods")
15
 */
16
class ShippingMethod implements ShippingMethodInterface
17
{
18
    use ShippingMethodTraits;
19
20
    /**
21
     * @var int
22
     *
23
     * @ORM\Id
24
     * @ORM\GeneratedValue
25
     * @ORM\Column(type="integer")
26
     **/
27
    protected $id;
28
29
    /**
30
     * @var int
31
     *
32
     * @ORM\Column(type="integer", unique=true)
33
     */
34
    protected $externalId;
35
36
    /**
37
     * @var string|null
38
     *
39
     * @ORM\Column(nullable=true, type="string", length=3)
40
     */
41
    protected $feeCurrency;
42
43
    /**
44
     * @var integer|null
45
     *
46
     * @ORM\Column(nullable=true, type="integer")
47
     */
48
    protected $feeAmount;
49
50
    /**
51
     * @var bool|null
52
     *
53
     * @ORM\Column(nullable=true, type="boolean")
54
     */
55
    protected $feeInclVat;
56
57
    /**
58
     * @var string|null
59
     *
60
     * @ORM\Column(nullable=true, type="string")
61
     */
62
    protected $name;
63
64
    /**
65
     * Populates a shipping method based on the response from the Dandomain API
66
     *
67
     * See the properties here:
68
     * http://4221117.shop53.dandomain.dk/admin/webapi/endpoints/v1_0/OrderService/help/operations/GetOrder
69
     *
70
     * @param \stdClass|array $data
71
     * @param string $currency
72
     * @return ShippingMethodInterface
73
     */
74
    public function populateFromApiResponse($data, $currency) : ShippingMethodInterface
75
    {
76
        $data = DandomainFoundation\objectToArray($data);
77
78
        $this
79
            ->setExternalId($data['id'])
80
            ->setFee(DandomainFoundation\createMoney((string)$currency, $data['fee']))
81
            ->setFeeInclVat($data['feeInclVat'])
82
            ->setName($data['name'])
83
        ;
84
85
        return $this;
86
    }
87
88
    /**
89
     * @return int
90
     */
91
    public function getId(): int
92
    {
93
        return $this->id;
94
    }
95
96
    /**
97
     * @param int $id
98
     * @return ShippingMethod
99
     */
100
    public function setId($id)
101
    {
102
        $this->id = $id;
103
        return $this;
104
    }
105
106
    /**
107
     * @return int
108
     */
109
    public function getExternalId(): int
110
    {
111
        return $this->externalId;
112
    }
113
114
    /**
115
     * @param int $externalId
116
     * @return ShippingMethod
117
     */
118
    public function setExternalId($externalId)
119
    {
120
        $this->externalId = $externalId;
121
        return $this;
122
    }
123
124
    /**
125
     * @return Money|null
126
     */
127
    public function getFee() : ?Money
128
    {
129
        if (!$this->feeCurrency) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->feeCurrency of type null|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
130
            return null;
131
        }
132
133
        return new Money($this->feeAmount, new Currency($this->feeCurrency));
134
    }
135
136
    /**
137
     * @param Money $money
138
     * @return ShippingMethodInterface
139
     */
140
    public function setFee(Money $money) : ShippingMethodInterface
141
    {
142
        $this->feeAmount = $money->getAmount();
0 ignored issues
show
Documentation Bug introduced by
It seems like $money->getAmount() of type string is incompatible with the declared type null|integer of property $feeAmount.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
143
        $this->feeCurrency = $money->getCurrency()->getCode();
144
145
        return $this;
146
    }
147
148
    /**
149
     * @return bool|null
150
     */
151
    public function getFeeInclVat()
152
    {
153
        return $this->feeInclVat;
154
    }
155
156
    /**
157
     * @param bool|null $feeInclVat
158
     * @return ShippingMethodInterface
159
     */
160
    public function setFeeInclVat($feeInclVat)
161
    {
162
        $this->feeInclVat = $feeInclVat;
163
        return $this;
164
    }
165
166
    /**
167
     * @return null|string
168
     */
169
    public function getName()
170
    {
171
        return $this->name;
172
    }
173
174
    /**
175
     * @param null|string $name
176
     * @return ShippingMethodInterface
177
     */
178
    public function setName($name)
179
    {
180
        $this->name = $name;
181
        return $this;
182
    }
183
}
184