Completed
Push — master ( 5c8a6e...bd330c )
by Nikola
02:18
created

Manager::today()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 13
ccs 0
cts 11
cp 0
rs 9.4286
cc 3
eloc 7
nc 3
nop 2
crap 12
1
<?php
2
3
namespace RunOpenCode\ExchangeRate;
4
5
use Psr\Log\LoggerAwareTrait;
6
use RunOpenCode\ExchangeRate\Contract\ManagerInterface;
7
use RunOpenCode\ExchangeRate\Contract\ProcessorInterface;
8
use RunOpenCode\ExchangeRate\Contract\ProcessorsRegistryInterface;
9
use RunOpenCode\ExchangeRate\Contract\RatesConfigurationRegistryInterface;
10
use RunOpenCode\ExchangeRate\Contract\RepositoryInterface;
11
use RunOpenCode\ExchangeRate\Contract\SourceInterface;
12
use RunOpenCode\ExchangeRate\Contract\SourcesRegistryInterface;
13
use RunOpenCode\ExchangeRate\Utils\CurrencyCode;
14
15
class Manager implements ManagerInterface
16
{
17
    use LoggerAwareTrait;
18
19
    /**
20
     * @var string
21
     */
22
    protected $baseCurrency;
23
24
    /**
25
     * @var RepositoryInterface
26
     */
27
    protected $repository;
28
29
    /**
30
     * @var SourcesRegistryInterface
31
     */
32
    protected $sources;
33
34
    /**
35
     * @var ProcessorsRegistryInterface
36
     */
37
    protected $processors;
38
39
    /**
40
     * @var RatesConfigurationRegistryInterface
41
     */
42
    protected $configurations;
43
44
    public function __construct($baseCurrency, RepositoryInterface $repository, SourcesRegistryInterface $sources, ProcessorsRegistryInterface $processors, RatesConfigurationRegistryInterface $configurations)
45
    {
46
        $this->baseCurrency = CurrencyCode::validate($baseCurrency);
47
        $this->repository = $repository;
48
        $this->configurations = $configurations;
49
        $this->sources = $sources;
50
        $this->processors = $processors;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function has($currencyCode, $date = null, $rateType = 'default')
57
    {
58
        return $this->repository->has(CurrencyCode::validate($currencyCode), $date, $rateType);
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function get($currencyCode, $date = null, $rateType = 'default')
65
    {
66
        return $this->repository->get(CurrencyCode::validate($currencyCode), $date, $rateType);
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function latest($currencyCode, $rateType = 'default')
73
    {
74
        return $this->repository->latest(CurrencyCode::validate($currencyCode), $rateType);
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function today($currencyCode, $rateType = 'default')
81
    {
82
        $currencyCode = CurrencyCode::validate($currencyCode);
83
        $today = new \DateTime('now');
84
85
        if ($this->has($currencyCode, $rateType, $today)) {
0 ignored issues
show
Documentation introduced by
$rateType is of type string, but the function expects a object<DateTime>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$today is of type object<DateTime>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
86
            return $this->get($currencyCode, $rateType, $today);
0 ignored issues
show
Documentation introduced by
$rateType is of type string, but the function expects a object<DateTime>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$today is of type object<DateTime>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
87
        }
88
89
        if ((int)$today->format('N') >= 6) {
90
            $today = new \DateTime('last Friday');
0 ignored issues
show
Unused Code introduced by
$today is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
91
        }
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function fetch($sourceName = null, $date = null)
98
    {
99
        $sources = $this->sources->all();
100
101
        if (!is_null($sourceName)) {
102
            $sources = array();
103
            $sourceNames = is_array($sourceName) ? $sourceName : array($sourceName);
104
105
            foreach ($sourceNames as $sourceName) {
106
                $sources[] = $this->sources->get($sourceName);
107
            }
108
        }
109
110
        $rates = array();
111
112
        /**
113
         * @var SourceInterface $source
114
         */
115
        foreach ($sources as $source) {
116
            $configurations = $this->configurations->find($source->getName());
117
118
            /**
119
             * @var Configuration $configuration
120
             */
121
            foreach ($configurations as $configuration) {
122
                $rates[] = $source->fetch($configuration->getCurrencyCode(), $configuration->getRateType(), $date);
123
            }
124
        }
125
126
        /**
127
         * @var ProcessorInterface $processor
128
         */
129
        foreach ($this->processors as $processor) {
130
            $rates = $processor->process($this->baseCurrency, $this->configurations, $rates);
131
        }
132
133
        $this->repository->save($rates);
134
    }
135
}
136