AbstractImporter   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 38
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 4

1 Method

Rating   Name   Duplication   Size   Complexity  
B updateOrCreate() 0 30 6
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
use Sylius\Component\Currency\Model\CurrencyInterface;
12
use Sylius\Component\Currency\Importer\AbstractImporter as BaseImporter;
13
14
abstract class AbstractImporter extends BaseImporter
15
{
16
    protected $baseCurrency;
17
18
    /**
19
     * {@inheritdoc}
20
     */
21
    protected function updateOrCreate(array $managedCurrencies, $code, $rate)
22
    {
23
        if (!empty($managedCurrencies)) {
24
            foreach ($managedCurrencies as $currency) {
25
                if ($currency->getCode() !== $this->baseCurrency) {
26
                    $currency->setDefault(false);
27
                } else {
28
                    $currency->setDefault(true);
29
                }
30
31
                if ($code === $currency->getCode()) {
32
                    $currency->setExchangeRate($rate);
33
                    $currency->setUpdatedAt(new \DateTime());
34
35
                    return;
36
                }
37
            }
38
        } else {
39
            /** @var $currency CurrencyInterface */
40
            $currency = $this->repository->createNew();
41
            $currency->setCode($code);
42
            $currency->setExchangeRate($rate);
43
            if ($currency->getCode() === $this->baseCurrency) {
44
                $currency->setDefault(true);
45
                $currency->setIsActive(true);
46
            }
47
48
            $this->manager->persist($currency);
49
        }
50
    }
51
}
52