Completed
Push — master ( 7edb4c...18c0ae )
by Nikola
03:09
created

Manager::fetch()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 27
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 6
Bugs 1 Features 0
Metric Value
c 6
b 1
f 0
dl 0
loc 27
ccs 0
cts 14
cp 0
rs 8.5806
cc 4
eloc 9
nc 6
nop 2
crap 20
1
<?php
2
/*
3
 * This file is part of the Exchange Rate package, an RunOpenCode project.
4
 *
5
 * (c) 2016 RunOpenCode
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace RunOpenCode\ExchangeRate;
11
12
use RunOpenCode\ExchangeRate\Contract\ManagerInterface;
13
use RunOpenCode\ExchangeRate\Contract\ProcessorInterface;
14
use RunOpenCode\ExchangeRate\Contract\ProcessorsRegistryInterface;
15
use RunOpenCode\ExchangeRate\Contract\RatesConfigurationRegistryInterface;
16
use RunOpenCode\ExchangeRate\Contract\RepositoryInterface;
17
use RunOpenCode\ExchangeRate\Contract\SourceInterface;
18
use RunOpenCode\ExchangeRate\Contract\SourcesRegistryInterface;
19
use RunOpenCode\ExchangeRate\Exception\ExchangeRateException;
20
use RunOpenCode\ExchangeRate\Log\LoggerAwareTrait;
21
use RunOpenCode\ExchangeRate\Registry\SourcesRegistry;
22
use RunOpenCode\ExchangeRate\Utils\CurrencyCodeUtil;
23
24
/**
25
 * Class Manager
26
 *
27
 * Default implementation of manager.
28
 *
29
 * @package RunOpenCode\ExchangeRate
30
 */
31
class Manager implements ManagerInterface
32
{
33
    use LoggerAwareTrait;
34
35
    /**
36
     * @var string
37
     */
38
    protected $baseCurrency;
39
40
    /**
41
     * @var RepositoryInterface
42
     */
43
    protected $repository;
44
45
    /**
46
     * @var SourcesRegistryInterface
47
     */
48
    protected $sources;
49
50
    /**
51
     * @var ProcessorsRegistryInterface
52
     */
53
    protected $processors;
54
55
    /**
56
     * @var RatesConfigurationRegistryInterface
57
     */
58
    protected $configurations;
59
60
    public function __construct($baseCurrency, RepositoryInterface $repository, SourcesRegistryInterface $sources, ProcessorsRegistryInterface $processors, RatesConfigurationRegistryInterface $configurations)
61
    {
62
        $this->baseCurrency = CurrencyCodeUtil::clean($baseCurrency);
63
        $this->repository = $repository;
64
        $this->configurations = $configurations;
65
        $this->sources = $sources;
66
        $this->processors = $processors;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function has($currencyCode, $date = null, $rateType = 'default')
73
    {
74
        return $this->repository->has(CurrencyCodeUtil::clean($currencyCode), $date, $rateType);
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function get($currencyCode, $date = null, $rateType = 'default')
81
    {
82
        return $this->repository->get(CurrencyCodeUtil::clean($currencyCode), $date, $rateType);
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function latest($currencyCode, $rateType = 'default')
89
    {
90
        return $this->repository->latest(CurrencyCodeUtil::clean($currencyCode), $rateType);
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public function today($currencyCode, $rateType = 'default')
97
    {
98
        $currencyCode = CurrencyCodeUtil::clean($currencyCode);
99
        $today = new \DateTime('now');
100
101
        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...
102
            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...
103
        }
104
105
        if ((int)$today->format('N') >= 6) {
106
            $today = new \DateTime('last Friday');
107
            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...
108
        }
109
110
        $message = sprintf('Rate for currency code "%s" of type "%s" is not available for today "%s".', $currencyCode, $rateType, date('Y-m-d'));
111
        $this->getLogger()->critical($message);
112
        throw new ExchangeRateException($message);
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118
    public function fetch($sourceName = null, $date = null)
119
    {
120
        $rates = array();
121
122
        /**
123
         * @var SourceInterface $source
124
         */
125
        foreach (SourcesRegistry::filter($this->sources, $sourceName) as $source) {
126
            $configurations = $this->configurations->find($source->getName());
127
128
            /**
129
             * @var Configuration $configuration
130
             */
131
            foreach ($configurations as $configuration) {
132
                $rates[] = $source->fetch($configuration->getCurrencyCode(), $configuration->getRateType(), $date);
133
            }
134
        }
135
136
        /**
137
         * @var ProcessorInterface $processor
138
         */
139
        foreach ($this->processors as $processor) {
140
            $rates = $processor->process($this->baseCurrency, $this->configurations, $rates);
141
        }
142
143
        $this->repository->save($rates);
144
    }
145
}
146