Completed
Push — master ( 636a3e...0d9cfe )
by Joachim
12:23
created

SyncTerminalsCommand::execute()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 18
nc 3
nop 2
1
<?php
2
namespace Loevgaard\DandomainAltapayBundle\Command;
3
4
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
5
use Symfony\Component\Console\Input\InputInterface;
6
use Symfony\Component\Console\Output\OutputInterface;
7
8
class SyncTerminalsCommand extends ContainerAwareCommand
9
{
10
    protected function configure()
11
    {
12
        $this->setName('loevgaard_dandomain_altapay:sync-terminals')
13
            ->setDescription('Synchronizes terminals from Altapay to the local database (one way sync)')
14
        ;
15
    }
16
17
    protected function execute(InputInterface $input, OutputInterface $output)
18
    {
19
        $terminalManager = $this->getContainer()->get('loevgaard_dandomain_altapay.terminal_manager');
20
        $altapayClient = $this->getContainer()->get('loevgaard_dandomain_altapay.altapay_client');
21
        $response = $altapayClient->getTerminals();
22
23
        foreach ($response->getTerminals() as $terminal) {
24
            $entity = $terminalManager->findTerminalByTitle($terminal->getTitle());
25
            if(!$entity) {
26
                $entity = $terminalManager->createTerminal();
27
            }
28
            $entity
29
                ->setTitle($terminal->getTitle())
30
                ->setCountry($terminal->getCountry())
31
                ->setNatures(array_map(function ($val) {
32
                    return (string)$val;
33
                }, $terminal->getNatures()))
34
                ->setCurrencies(array_map(function ($val) {
35
                    return (string)$val;
36
                }, $terminal->getCurrencies()))
37
            ;
38
39
            $terminalManager->updateTerminal($entity);
40
        }
41
    }
42
}