Skip to content

Commit b8199b0

Browse files
committed
Fixed tests & simplified code
1 parent 48658a8 commit b8199b0

File tree

1 file changed

+10
-15
lines changed

1 file changed

+10
-15
lines changed

src/Models/Price.php

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,34 +30,29 @@ public function __construct(
3030
DateTime $activated_at = null
3131
) {
3232
if (! is_null($arguments) && $arguments !== []) {
33-
return $this->constructFromArgumentsArray($arguments);
34-
}
35-
36-
if ((! is_null($amount) && ! is_null($currency)) && is_null($minor)) {
37-
$amount = Money::of($amount, $currency)->getMinorAmount()->toInt();
33+
return parent::__construct($arguments);
3834
}
3935

4036
parent::__construct([
41-
'amount' => $minor ?? $amount,
37+
'minor' => $minor,
38+
'amount' => $amount,
4239
'currency' => $currency,
4340
'type' => $type,
4441
'activated_at' => $activated_at
4542
]);
4643
}
4744

48-
private function constructFromArgumentsArray($arguments)
45+
public function fill(array $attributes)
4946
{
50-
$amount = $arguments['amount'] ?? null;
51-
$minor = $arguments['minor'] ?? null;
52-
$currency = $arguments['currency'];
53-
54-
if (is_null($amount) && is_null($minor)) {
55-
throw new PriceValueNotDefinedException('No value provided for price object.');
47+
if ($minor = $attributes['minor'] ?? null) {
48+
$attributes['amount'] = $minor;
49+
} else if (($amount = $attributes['amount'] ?? null) && ($currency = $attributes['currency'] ?? null)) {
50+
$attributes['amount'] = Money::of($amount, $currency)->getMinorAmount()->toInt();
5651
}
5752

58-
$arguments['amount'] = $minor ?? Money::of($amount, $currency)->getMinorAmount()->toInt();
53+
unset($attributes['minor']);
5954

60-
return parent::__construct($arguments);
55+
return parent::fill($attributes);
6156
}
6257

6358
public function priceable()

0 commit comments

Comments
 (0)