Completed
Push — master ( 565929...2a3309 )
by Nikola
03:02
created

Manager::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 4
crap 1
1
<?php
2
/*
3
 * This file is part of the Exchange Rate package, an RunOpenCode project.
4
 *
5
 * (c) 2017 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\Enum\RateType;
20
use RunOpenCode\ExchangeRate\Exception\ExchangeRateException;
21
use RunOpenCode\ExchangeRate\Log\LoggerAwareTrait;
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
    /**
61
     * Manager constructor.
62
     *
63
     * @param string $baseCurrency
64
     * @param RepositoryInterface $repository
65
     * @param SourcesRegistryInterface $sources
66
     * @param ProcessorsRegistryInterface $processors
67
     * @param RatesConfigurationRegistryInterface $configurations
68
     *
69
     * @throws \RunOpenCode\ExchangeRate\Exception\UnknownCurrencyCodeException
70
     */
71 14
    public function __construct($baseCurrency, RepositoryInterface $repository, SourcesRegistryInterface $sources, ProcessorsRegistryInterface $processors, RatesConfigurationRegistryInterface $configurations)
72
    {
73 14
        $this->baseCurrency = CurrencyCodeUtil::clean($baseCurrency);
74 14
        $this->repository = $repository;
75 14
        $this->configurations = $configurations;
76 14
        $this->sources = $sources;
77 14
        $this->processors = $processors;
78 14
    }
79
80
    /**
81
     * Get base currency
82
     *
83
     * @return string
84
     */
85 1
    public function getBaseCurrency()
86
    {
87 1
        return $this->baseCurrency;
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     *
93
     * @throws \RunOpenCode\ExchangeRate\Exception\UnknownCurrencyCodeException
94
     */
95 8
    public function has($sourceName, $currencyCode, \DateTime $date = null, $rateType = RateType::MEDIAN)
96
    {
97 8
        return $this->repository->has($sourceName, CurrencyCodeUtil::clean($currencyCode), $date, $rateType);
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     *
103
     * @throws \RunOpenCode\ExchangeRate\Exception\UnknownCurrencyCodeException
104
     */
105 7
    public function get($sourceName, $currencyCode, \DateTime $date = null, $rateType = RateType::MEDIAN)
106
    {
107 7
        return $this->repository->get($sourceName, CurrencyCodeUtil::clean($currencyCode), $date, $rateType);
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113 2
    public function latest($sourceName, $currencyCode, $rateType = RateType::MEDIAN)
114
    {
115 2
        return $this->repository->latest($sourceName, CurrencyCodeUtil::clean($currencyCode), $rateType);
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     *
121
     * @throws \RunOpenCode\ExchangeRate\Exception\UnknownCurrencyCodeException
122
     */
123 4
    public function today($sourceName, $currencyCode, $rateType = RateType::MEDIAN)
124
    {
125 4
        $currencyCode = CurrencyCodeUtil::clean($currencyCode);
126 4
        $today = new \DateTime('@'.time());
127
128 4
        if ($this->has($sourceName, $currencyCode, $today, $rateType)) {
129 1
            return $this->get($sourceName, $currencyCode, $today, $rateType);
130
        }
131
132 3 View Code Duplication
        if ((int) $today->format('N') >= 6 && $this->has($sourceName, $currencyCode, $lastFriday = new \DateTime('last Friday'), $rateType)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
133 2
            return $this->get($sourceName, $currencyCode, $lastFriday, $rateType);
134
        }
135
136 1
        $message = sprintf('Rate for currency code "%s" of type "%s" from source "%s" is not available for today "%s".', $currencyCode, $rateType, $sourceName, date('Y-m-d'));
137 1
        $this->getLogger()->critical($message);
138 1
        throw new ExchangeRateException($message);
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     *
144
     * @throws \RunOpenCode\ExchangeRate\Exception\UnknownCurrencyCodeException
145
     */
146 3
    public function historical($sourceName, $currencyCode, \DateTime $date, $rateType = RateType::MEDIAN)
147
    {
148 3
        $currencyCode = CurrencyCodeUtil::clean($currencyCode);
149
150 3
        if ($this->has($sourceName, $currencyCode, $date, $rateType)) {
151 1
            return $this->get($sourceName, $currencyCode, $date, $rateType);
152
        }
153
154 2 View Code Duplication
        if ((int) $date->format('N') === 6 && $this->has($sourceName, $currencyCode, $lastFriday = $date->sub(new \DateInterval('P1D')), $rateType)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
155 1
            return $this->get($sourceName, $currencyCode, $lastFriday, $rateType);
156
        }
157
158 2 View Code Duplication
        if ((int) $date->format('N') === 7 && $this->has($sourceName, $currencyCode, $lastFriday = $date->sub(new \DateInterval('P2D')), $rateType)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
159 1
            return $this->get($sourceName, $currencyCode, $lastFriday, $rateType);
160
        }
161
162 1
        $message = sprintf('Rate for currency code "%s" of type "%s" from source "%s" is not available for historical date "%s".', $currencyCode, $rateType, $sourceName, $date->format('Y-m-d'));
163 1
        $this->getLogger()->critical($message);
164 1
        throw new ExchangeRateException($message);
165
    }
166
167
    /**
168
     * {@inheritdoc}
169
     */
170 1
    public function fetch($sourceName = null, \DateTime $date = null)
171
    {
172 1
        $rates = array();
173
174 1
        $filteredSourceNames = ($sourceName === null) ? array_map(function (SourceInterface $source) {
175
            return $source->getName();
176 1
        }, $this->sources->all()) : (array) $sourceName;
177
178 1
        foreach ($filteredSourceNames as $name) {
179
180 1
            $source = $this->sources->get($name);
181
182 1
            $filteredConfigurations = $this->configurations->all(array(
183 1
                'sourceName' => $name,
184
            ));
185
186
            /**
187
             * @var Configuration $configuration
188
             */
189 1
            foreach ($filteredConfigurations as $configuration) {
190 1
                $rates[] = $source->fetch($configuration->getCurrencyCode(), $configuration->getRateType(), $date);
191
            }
192
        }
193
194
        /**
195
         * @var ProcessorInterface $processor
196
         */
197 1
        foreach ($this->processors->all() as $processor) {
198 1
            $rates = $processor->process($this->baseCurrency, $this->configurations, $rates);
199
        }
200
201 1
        $this->repository->save($rates);
202
203 1
        return $rates;
204
    }
205
}
206