Price::currency()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace GNAHotelSolutions\CurrencyConverter;
4
5
class Price
6
{
7
    /** @var float */
8
    protected $amount;
9
10
    /** @var string */
11
    protected $currency;
12
13
    const CURRENCY_SYMBOLS = [
14
        '€' => 'EUR',
15
        '$' => 'USD',
16
        '£' => 'GBP',
17
        // TODO: Add more currency symbols and their codes
18
    ];
19
20
    public function __construct($amount, string $currency)
21
    {
22
        $this->currency = $this->parseCurrency($currency);
23
24
        $this->amount = $this->parseAmount($amount);
25
    }
26
27
    public function amount(): float
28
    {
29
        return $this->amount;
30
    }
31
32
    public function currency(): string
33
    {
34
        return $this->currency;
35
    }
36
37
    private function parseCurrency($currency): string
38
    {
39
        if (isset(self::CURRENCY_SYMBOLS[$currency])) {
40
            return self::CURRENCY_SYMBOLS[$currency];
41
        }
42
43
        return strtoupper($currency);
44
    }
45
46
    protected function parseAmount($amount): float
47
    {
48
        return (float) $amount;
49
    }
50
51
    public function formattedWith(string $currency): string
52
    {
53
        if ($this->getCurrency() !== $currency) {
0 ignored issues
show
Bug introduced by
The method getCurrency() does not exist on GNAHotelSolutions\CurrencyConverter\Price. Did you maybe mean currency()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
54
            return "{$this} ({$this->convertTo($currency)})";
0 ignored issues
show
Bug introduced by
The method convertTo() does not seem to exist on object<GNAHotelSolutions\CurrencyConverter\Price>.

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...
55
        }
56
57
        return "{$this}";
58
    }
59
60
    public function __toString()
61
    {
62
        return "{$this->getAmount()} {$this->getCurrency()}";
0 ignored issues
show
Bug introduced by
The method getAmount() does not exist on GNAHotelSolutions\CurrencyConverter\Price. Did you maybe mean amount()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
Bug introduced by
The method getCurrency() does not exist on GNAHotelSolutions\CurrencyConverter\Price. Did you maybe mean currency()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
63
    }
64
}
65