Completed
Push — master ( 7c1eaa...80d841 )
by Joachim
15:53
created

TerminalSynchronizer::syncAll()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
c 0
b 0
f 0
rs 9.0856
cc 3
eloc 16
nc 3
nop 0
1
<?php
2
3
namespace Loevgaard\DandomainAltapayBundle\Synchronizer;
4
5
use Loevgaard\AltaPay\Client as AltapayClient;
6
use Loevgaard\DandomainAltapayBundle\Manager\TerminalManager;
7
8
class TerminalSynchronizer
9
{
10
    /**
11
     * @var TerminalManager
12
     */
13
    protected $terminalManager;
14
15
    /**
16
     * @var AltapayClient
17
     */
18
    protected $altapayClient;
19
20
    public function __construct(TerminalManager $terminalManager, AltapayClient $altapayClient)
21
    {
22
        $this->terminalManager = $terminalManager;
23
        $this->altapayClient = $altapayClient;
24
    }
25
26
    public function syncAll()
27
    {
28
        $response = $this->altapayClient->getTerminals();
29
30
        foreach ($response->getTerminals() as $terminal) {
31
            $entity = $this->terminalManager->findTerminalByTitle($terminal->getTitle());
32
            if (!$entity) {
33
                $entity = $this->terminalManager->create();
34
            }
35
            $entity
36
                ->setTitle($terminal->getTitle())
37
                ->setCountry($terminal->getCountry())
38
                ->setNatures(array_map(function ($val) {
39
                    return (string) $val;
40
                }, $terminal->getNatures()))
41
                ->setCurrencies(array_map(function ($val) {
42
                    return (string) $val;
43
                }, $terminal->getCurrencies()))
44
            ;
45
46
            $this->terminalManager->update($entity);
47
        }
48
    }
49
}
50