Completed
Push — master ( 0e0f24...3f1ffc )
by Nikola
03:15
created

Manager::has()   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 6
Bugs 0 Features 0
Metric Value
c 6
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
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) 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 8
    public function __construct($baseCurrency, RepositoryInterface $repository, SourcesRegistryInterface $sources, ProcessorsRegistryInterface $processors, RatesConfigurationRegistryInterface $configurations)
61
    {
62 8
        $this->baseCurrency = CurrencyCodeUtil::clean($baseCurrency);
63 8
        $this->repository = $repository;
64 8
        $this->configurations = $configurations;
65 8
        $this->sources = $sources;
66 8
        $this->processors = $processors;
67 8
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72 6
    public function has($sourceName, $currencyCode, $date = null, $rateType = 'default')
73
    {
74 6
        return $this->repository->has($sourceName, CurrencyCodeUtil::clean($currencyCode), $date, $rateType);
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80 2
    public function get($sourceName, $currencyCode, $date = null, $rateType = 'default')
81
    {
82 2
        return $this->repository->get($sourceName, CurrencyCodeUtil::clean($currencyCode), $date, $rateType);
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88 2
    public function latest($sourceName, $currencyCode, $rateType = 'default')
89
    {
90 2
        return $this->repository->latest($sourceName, CurrencyCodeUtil::clean($currencyCode), $rateType);
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96 4
    public function today($sourceName, $currencyCode, $rateType = 'default')
97
    {
98 4
        $currencyCode = CurrencyCodeUtil::clean($currencyCode);
99 4
        $today = new \DateTime('now');
100
101 4
        if ($this->has($sourceName, $currencyCode, $today, $rateType)) {
102
            return $this->get($sourceName, $currencyCode, $today, $rateType);
103
        }
104
105 4
        if ((int)$today->format('N') >= 6) {
106
107
            $today = new \DateTime('last Friday');
108
109
            try {
110
                return $this->get($sourceName, $currencyCode, $today, $rateType);
111
            } catch (\Exception $e) {
112
                $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'));
113
                $this->getLogger()->critical($message);
114
                throw new ExchangeRateException($message, 0, $e);
115
            }
116
        }
117 4
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122 2
    public function fetch($sourceName = null, $date = null)
123
    {
124 2
        $rates = array();
125
126 2
        $sourceNames = ($sourceName === null) ? array_map(function(SourceInterface $source) {
127
            return $source->getName();
128 2
        }, $this->sources->all()) : (array) $sourceName;
129
130 2
        foreach ($sourceNames as $sourceName) {
131
132 2
            $source = $this->sources->get($sourceName);
133
134 2
            $configurations = $this->configurations->all(array(
135
                'sourceName' => $sourceName
136 2
            ));
137
138
            /**
139
             * @var Configuration $configuration
140
             */
141 2
            foreach ($configurations as $configuration) {
142 2
                $rates[] = $source->fetch($configuration->getCurrencyCode(), $configuration->getRateType(), $date);
143 2
            }
144 2
        }
145
146
        /**
147
         * @var ProcessorInterface $processor
148
         */
149 2
        foreach ($this->processors->all() as $processor) {
150 2
            $rates = $processor->process($this->baseCurrency, $this->configurations, $rates);
151 2
        }
152
153 2
        $this->repository->save($rates);
154 2
    }
155
}
156