Completed
Push — master ( ef4aad...65d71a )
by Nikola
06:40
created

Manager::alias()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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