Passed
Push — master ( 5b59e5...6891ea )
by Carl
02:32
created

CardQuery::deactivate()   A

Complexity

Conditions 4
Paths 11

Size

Total Lines 19
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.2
c 0
b 0
f 0
cc 4
eloc 15
nc 11
nop 1
1
<?php
2
/**
3
 * Created by Carl Owens ([email protected])
4
 * Company: PartFire Ltd (www.partfire.co.uk)
5
 * Copyright © 2017 PartFire Ltd. All rights reserved.
6
 *
7
 * User:    Carl Owens
8
 * Date:    18/01/2017
9
 * Time:    23:53
10
 * File:    CardQuery.php
11
 **/
12
13
namespace PartFire\MangoPayBundle\Models\Adapters;
14
15
use MangoPay\Card as mangoCard;
16
use PartFire\MangoPayBundle\Models\CardQueryInterface;
17
use PartFire\MangoPayBundle\Models\DTOs\Card;
18
use MangoPay\MangoPayApi;
19
use PartFire\MangoPayBundle\Models\DTOs\Translators\CardTranslator;
20
use Symfony\Bridge\Monolog\Logger;
21
use PartFire\MangoPayBundle\Models\Exception as PartFireException;
22
use MangoPay\Libraries\Exception;
23
use MangoPay\Libraries\ResponseException;
24
25
class CardQuery extends AbstractQuery implements CardQueryInterface
26
{
27
    /**
28
     * @var CardTranslator
29
     */
30
    private $cardTranslator;
31
32
    /**
33
     * CardQuery constructor.
34
     * @param $clientId
35
     * @param $clientPassword
36
     * @param $baseUrl
37
     * @param MangoPayApi $mangoPayApi
38
     * @param Logger $logger
39
     * @param CardTranslator $cardTranslator
40
     */
41
    public function __construct(
42
        $clientId,
43
        $clientPassword,
44
        $baseUrl,
45
        MangoPayApi $mangoPayApi,
46
        Logger $logger,
47
        CardTranslator $cardTranslator
48
    ) {
49
        parent::__construct($clientId, $clientPassword, $baseUrl, $mangoPayApi, $logger);
50
        $this->cardTranslator = $cardTranslator;
51
    }
52
53
    /**
54
     * @param string $cardId
55
     * @return null|Card
56
     * @throws PartFireException
57
     */
58
    public function get(string $cardId): ? Card
59
    {
60
        try {
61
            $card = $this->mangoPayApi->Cards->Get($cardId);
62
            if ($card instanceof mangoCard) {
63
                return $this->cardTranslator->translateMangoCardToDto($card);
64
            }
65
            $this->logger->addCritical("Card ID: $cardId not found when querying API.");
66
            throw new PartFireException("Card ID: $cardId not found when querying API.");
67
        } catch (ResponseException $e) {
68
            $this->logger->addCritical($e->getMessage(), ['code' => $e->getCode(), 'details' => $e->GetErrorDetails()]);
69
            throw new PartFireException($e->getMessage(), $e->getCode(), $e);
70
        } catch (Exception $e) {
71
            $this->logger->addError($e->getMessage());
72
            throw new PartFireException($e->getMessage(), $e->getCode(), $e);
73
        }
74
    }
75
76
    public function deactivate(string $cardId): ? Card
77
    {
78
        try {
79
            $card = $this->mangoPayApi->Cards->Get($cardId);
80
            if ($card instanceof mangoCard) {
81
                $card->Active = false;
82
                $card = $this->mangoPayApi->Cards->Update($card);
83
                return $this->cardTranslator->translateMangoCardToDto($card);
84
            }
85
            $this->logger->addCritical("Card ID: $cardId not found when querying API.");
86
            throw new PartFireException("Card ID: $cardId not found when querying API.");
87
        } catch (ResponseException $e) {
88
            $this->logger->addCritical($e->getMessage(), ['code' => $e->getCode(), 'details' => $e->GetErrorDetails()]);
89
            throw new PartFireException($e->getMessage(), $e->getCode(), $e);
90
        } catch (Exception $e) {
91
            $this->logger->addError($e->getMessage());
92
            throw new PartFireException($e->getMessage(), $e->getCode(), $e);
93
        }
94
    }
95
}
96