AbstractImporter::updateOrCreate()   B
last analyzed

Complexity

Conditions 6
Paths 7

Size

Total Lines 30
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 19
nc 7
nop 3
dl 0
loc 30
rs 8.439
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
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