Completed
Push — master ( 862be6...6b5941 )
by Nikola
50:19 queued 47:05
created

Manager::today()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 4
Bugs 2 Features 0
Metric Value
c 4
b 2
f 0
dl 0
loc 22
ccs 0
cts 15
cp 0
rs 8.9197
cc 4
eloc 13
nc 4
nop 3
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($sourceName, $currencyCode, $date = null, $rateType = 'default')
73
    {
74
        return $this->repository->has($sourceName, CurrencyCodeUtil::clean($currencyCode), $date, $rateType);
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function get($sourceName, $currencyCode, $date = null, $rateType = 'default')
81
    {
82
        return $this->repository->get($sourceName, CurrencyCodeUtil::clean($currencyCode), $date, $rateType);
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function latest($sourceName, $currencyCode, $rateType = 'default')
89
    {
90
        return $this->repository->latest($sourceName, CurrencyCodeUtil::clean($currencyCode), $rateType);
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public function today($sourceName, $currencyCode, $rateType = 'default')
97
    {
98
        $currencyCode = CurrencyCodeUtil::clean($currencyCode);
99
        $today = new \DateTime('now');
100
101
        if ($this->has($sourceName, $currencyCode, $today, $rateType)) {
102
            return $this->get($sourceName, $currencyCode, $today, $rateType);
103
        }
104
105
        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, $e);
115
            }
116
        }
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122
    public function fetch($sourceName = null, $date = null)
123
    {
124
        $rates = array();
125
126
        /**
127
         * @var SourceInterface $source
128
         */
129
        foreach ($this->sources->all((is_string($sourceName) ? array($sourceName) : $sourceName)) as $source) {
0 ignored issues
show
Bug introduced by
It seems like is_string($sourceName) ?...urceName) : $sourceName can also be of type null; however, RunOpenCode\ExchangeRate...egistryInterface::all() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
130
131
            $configurations = $this->configurations->all(array(
132
                'sourceName' => $source->getName()
133
            ));
134
135
            /**
136
             * @var Configuration $configuration
137
             */
138
            foreach ($configurations as $configuration) {
139
                $rates[] = $source->fetch($configuration->getCurrencyCode(), $configuration->getRateType(), $date);
140
            }
141
        }
142
143
        /**
144
         * @var ProcessorInterface $processor
145
         */
146
        foreach ($this->processors as $processor) {
147
            $rates = $processor->process($this->baseCurrency, $this->configurations, $rates);
148
        }
149
150
        $this->repository->save($rates);
151
    }
152
}
153