Completed
Push — master ( 17fbce...9c0ecb )
by Nikola
02:12
created

Manager::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 8
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 8
loc 8
ccs 0
cts 7
cp 0
rs 9.4286
cc 2
eloc 4
nc 2
nop 3
crap 6
1
<?php
2
3
namespace RunOpenCode\ExchangeRate;
4
5
use Psr\Log\LoggerAwareTrait;
6
use RunOpenCode\ExchangeRate\Contract\ManagerInterface;
7
use RunOpenCode\ExchangeRate\Contract\ProcessorInterface;
8
use RunOpenCode\ExchangeRate\Contract\ProcessorsRegistryInterface;
9
use RunOpenCode\ExchangeRate\Contract\RatesConfigurationRegistryInterface;
10
use RunOpenCode\ExchangeRate\Contract\RepositoryInterface;
11
use RunOpenCode\ExchangeRate\Contract\SourceInterface;
12
use RunOpenCode\ExchangeRate\Contract\SourcesRegistryInterface;
13
use RunOpenCode\ExchangeRate\Exception\UnknownCurrencyCodeException;
14
use RunOpenCode\ExchangeRate\Utils\CurrencyCode;
15
16
class Manager implements ManagerInterface
17
{
18
    use LoggerAwareTrait;
19
20
    /**
21
     * @var string
22
     */
23
    protected $baseCurrency;
24
25
    /**
26
     * @var RepositoryInterface
27
     */
28
    protected $repository;
29
30
    /**
31
     * @var SourcesRegistryInterface
32
     */
33
    protected $sources;
34
35
    /**
36
     * @var ProcessorsRegistryInterface
37
     */
38
    protected $processors;
39
40
    /**
41
     * @var RatesConfigurationRegistryInterface
42
     */
43
    protected $configurations;
44
45
    public function __construct($baseCurrency, RepositoryInterface $repository, SourcesRegistryInterface $sources, ProcessorsRegistryInterface $processors, RatesConfigurationRegistryInterface $configurations)
46
    {
47
        $this->baseCurrency = $baseCurrency;
48
        $this->repository = $repository;
49
        $this->configurations = $configurations;
50
        $this->sources = $sources;
51
        $this->processors = $processors;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 View Code Duplication
    public function has($currencyCode, $date = null, $rateType = 'default')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
58
    {
59
        if (!CurrencyCode::exists($currencyCode)) {
60
            throw new UnknownCurrencyCodeException(sprintf('Unknown currency code "%s".', $currencyCode));
61
        }
62
63
        return $this->repository->has($currencyCode, $date, $rateType);
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69 View Code Duplication
    public function get($currencyCode, $date = null, $rateType = 'default')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
70
    {
71
        if (!CurrencyCode::exists($currencyCode)) {
72
            throw new UnknownCurrencyCodeException(sprintf('Unknown currency code "%s".', $currencyCode));
73
        }
74
75
        return $this->repository->get($currencyCode, $date, $rateType);
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81 View Code Duplication
    public function latest($currencyCode, $rateType = 'default')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
82
    {
83
        if (!CurrencyCode::exists($currencyCode)) {
84
            throw new UnknownCurrencyCodeException(sprintf('Unknown currency code "%s".', $currencyCode));
85
        }
86
87
        return $this->repository->latest($currencyCode, $rateType);
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function fetch($sourceName = null, $date = null)
94
    {
95
        $sources = $this->sources;
96
97
        if (!is_null($sourceName)) {
98
            $sources = array();
99
            $sourceNames = is_array($sourceName) ? $sourceName : array($sourceName);
100
101
            foreach ($sourceNames as $sourceName) {
102
                $sources[] = $this->sources->get($sourceName);
103
            }
104
        }
105
106
        $rates = array();
107
108
        /**
109
         * @var SourceInterface $source
110
         */
111
        foreach ($sources as $source) {
112
            $configurations = $this->configurations->find($source->getName());
113
114
            /**
115
             * @var Configuration $configuration
116
             */
117
            foreach ($configurations as $configuration) {
118
                $rates[] = $source->fetch($configuration->getCurrencyCode(), $configuration->getRateType(), $date);
119
            }
120
        }
121
122
        /**
123
         * @var ProcessorInterface $processor
124
         */
125
        foreach ($this->processors as $processor) {
126
            $rates = $processor->process($this->baseCurrency, $this->configurations, $rates);
127
        }
128
129
        $this->repository->save($rates);
130
    }
131
}
132