Completed
Push — master ( a2bd00...ff1505 )
by Jean
02:44
created

PriceFormatter::format()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 0
cts 21
cp 0
rs 8.9197
c 0
b 0
f 0
cc 4
eloc 16
nc 4
nop 2
crap 20
1
<?php
2
/**
3
 * @author Jean Silva <[email protected]>
4
 * @license MIT
5
 */
6
namespace Jeancsil\FlightSpy\Service\Currency;
7
8
class PriceFormatter
9
{
10
    const CURRENCIES_FILE = 'currencies.json';
11
12
    private $currencies;
13
14
    public function __construct($resourcesDir)
15
    {
16
        $this->initializeCurrenciesFile($resourcesDir);
17
    }
18
19
    /**
20
     * @param string $price
21
     * @param string $currencyCode
22
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
23
     */
24
    public function format($price, $currencyCode) {
25
        foreach ($this->currencies as $currency) {
26
            if ($currency['Code'] == $currencyCode) {
27
                if ($currency['SymbolOnLeft'] == 'true') {
28
                    return $currency['Symbol'] .
29
                        number_format(
30
                            $price,
31
                            2,
32
                            $currency['DecimalSeparator'],
33
                            $currency['ThousandsSeparator']
34
                        );
35
                }
36
37
                return number_format(
38
                    $price,
39
                    2,
40
                    $currency['DecimalSeparator'],
41
                    $currency['ThousandsSeparator']
42
                ) . $currency['Symbol'];
43
            }
44
        }
45
    }
46
47
    /**
48
     * @param $resourcesDir
49
     */
50
    private function initializeCurrenciesFile($resourcesDir)
51
    {
52
        if (!$this->currencies) {
53
            $currenciesFile = getcwd() . '/' . $resourcesDir . '/' . static::CURRENCIES_FILE;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
54
            $currenciesArray = json_decode(file_get_contents($currenciesFile), true);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
55
            $this->currencies = $currenciesArray['ReferenceServiceResponseDto']['Currencies']['CurrencyDto'];
56
        }
57
    }
58
}
59