CentralAzerbaijanBank::import()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 13

Duplication

Lines 17
Ratio 73.91 %

Importance

Changes 0
Metric Value
cc 3
eloc 13
nc 3
nop 1
dl 17
loc 23
rs 9.0856
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @author Rafał Muszyński <[email protected]>
5
 * @copyright 2015 Sourcefabric z.ú.
6
 * @license http://www.gnu.org/licenses/gpl-3.0.txt
7
 */
8
9
namespace Newscoop\PaywallBundle\Importer;
10
11
/**
12
 * Central Azerbaijan Bank Importer class.
13
 */
14
class CentralAzerbaijanBank extends AbstractImporter
15
{
16
    private $url = 'http://www.cbar.az/currencies/';
17
18
    /**
19
     * {@inheritdoc}
20
     */
21 View Code Duplication
    public function configure(array $options = array())
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...
22
    {
23
        if (!isset($options['base_currency'])) {
24
            throw new \InvalidArgumentException('"base_currency" must be set in order to use CentralAzerbaijanBank.');
25
        }
26
27
        $this->baseCurrency = $options['base_currency'];
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function import(array $managedCurrencies = array())
34
    {
35
        $now = new \DateTime('now');
36
        $this->url = $this->url.$now->format('d.m.Y').'.xml';
37
        $xml = simplexml_load_file($this->url);
38 View Code Duplication
        if ($xml instanceof \SimpleXMLElement) {
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...
39
            // base currency: AZN
40
            $this->updateOrCreate($managedCurrencies, $this->baseCurrency, 1.00);
41
42
            $data = $xml->xpath('//ValCurs/*[1]');
43
            // divides 1.00 by current exchange rate to get the reverse rate,
44
            // because CBAR provides only inverse rate
45
            foreach ($data[0]->children() as $child) {
46
                $this->updateOrCreate(
47
                    $managedCurrencies,
48
                    (string) $child->attributes()->Code,
49
                    1.00 / (float) $child->children()->Value
50
                );
51
            }
52
53
            $this->manager->flush();
54
        }
55
    }
56
}
57