PriceFormatter   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
lcom 1
cbo 0
dl 0
loc 52
ccs 0
cts 34
cp 0
rs 10
c 2
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B format() 0 23 4
A initializeCurrenciesFile() 0 8 2
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
    {
26
        foreach ($this->currencies as $currency) {
27
            if ($currency['Code'] == $currencyCode) {
28
                if ($currency['SymbolOnLeft'] == 'true') {
29
                    return $currency['Symbol'] .
30
                        number_format(
31
                            $price,
32
                            2,
33
                            $currency['DecimalSeparator'],
34
                            $currency['ThousandsSeparator']
35
                        );
36
                }
37
38
                return number_format(
39
                    $price,
40
                    2,
41
                    $currency['DecimalSeparator'],
42
                    $currency['ThousandsSeparator']
43
                ) . $currency['Symbol'];
44
            }
45
        }
46
    }
47
48
    /**
49
     * @param $resourcesDir
50
     */
51
    private function initializeCurrenciesFile($resourcesDir)
52
    {
53
        if (!$this->currencies) {
54
            $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...
55
            $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...
56
            $this->currencies = $currenciesArray['ReferenceServiceResponseDto']['Currencies']['CurrencyDto'];
57
        }
58
    }
59
}
60