CardRegistrationsQuery::update()   C
last analyzed

Complexity

Conditions 8
Paths 32

Size

Total Lines 36
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 25
nc 32
nop 3
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:    16:18
10
 * File:    CardQuery.php
11
 **/
12
13
namespace PartFire\MangoPayBundle\Models\Adapters;
14
15
16
use MangoPay\CardRegistration;
17
use MangoPay\CardRegistrationStatus;
18
use MangoPay\MangoPayApi;
19
use PartFire\MangoPayBundle\Models\CardRegistrationsQueryInterface;
20
use PartFire\MangoPayBundle\Models\DTOs\CardRegistration as CardRegistrationDto;
21
use PartFire\MangoPayBundle\Models\DTOs\Translators\CardTranslator;
22
use Symfony\Bridge\Monolog\Logger;
23
use PartFire\MangoPayBundle\Models\Exception as PartFireException;
24
use MangoPay\Libraries\Exception;
25
use MangoPay\Libraries\ResponseException;
26
27
class CardRegistrationsQuery extends AbstractQuery implements CardRegistrationsQueryInterface
28
{
29
    /**
30
     * @var CardTranslator
31
     */
32
    private $cardTranslator;
33
34
    public function __construct(
35
        $clientId,
36
        $clientPassword,
37
        $baseUrl,
38
        MangoPayApi $mangoPayApi,
39
        Logger $logger,
40
        CardTranslator $cardTranslator
41
    ) {
42
        parent::__construct($clientId, $clientPassword, $baseUrl, $mangoPayApi, $logger);
43
        $this->cardTranslator = $cardTranslator;
44
    }
45
46
    public function create(string $userId, string $currency, string $cardType, string $tag): CardRegistrationDto
47
    {
48
        try {
49
            $CardRegistration = new CardRegistration();
50
            $CardRegistration->Tag = $tag;
51
            $CardRegistration->UserId = $userId;
52
            $CardRegistration->Currency = $currency;
53
            $CardRegistration->CardType = $cardType;
54
55
            $cardRegistration = $this->mangoPayApi->CardRegistrations->Create($CardRegistration);
56
            return $this->cardTranslator->translateMangoCardRegistrationDataToDto($cardRegistration);
57
        } catch (ResponseException $e) {
58
            $this->logger->addCritical($e->getMessage(), ['code' => $e->getCode(), 'details' => $e->GetErrorDetails()]);
59
            throw new PartFireException($e->getMessage(), $e->getCode(), $e);
60
        } catch (Exception $e) {
61
            $this->logger->addError($e->getMessage());
62
            throw new PartFireException($e->getMessage(), $e->getCode(), $e);
63
        }
64
    }
65
66
    public function update(string $cardRegisteredId, string $registrationData, string $errorCode): CardRegistrationDto
67
    {
68
        try {
69
            $cardRegister = $this->mangoPayApi->CardRegistrations->Get($cardRegisteredId);
70
            if ($cardRegister instanceof CardRegistration) {
71
72
                if ($registrationData != '') {
73
                    $cardRegister->RegistrationData = 'data=' . $registrationData;
74
                } else {
75
                    $cardRegister->RegistrationData = 'errorCode=' . $errorCode;
76
                }
77
78
                $updatedCardRegister = $this->mangoPayApi->CardRegistrations->Update($cardRegister);
79
80
                if ($updatedCardRegister instanceof CardRegistration) {
81
                    if ($updatedCardRegister->Status != CardRegistrationStatus::Validated ||
82
                        !isset($updatedCardRegister->CardId)) {
83
                        $this->logger->addCritical("Cannot create card. Payment has not been created.");
84
                        throw new PartFireException("Cannot create card. Payment has not been created.");
85
                    }
86
                    return $this->cardTranslator->translateMangoCardRegistrationDataToDto($updatedCardRegister);
87
                }
88
                $this->logger->addCritical("Card Registration failed when trying to update via API.");
89
                throw new PartFireException("Card Registration failed when trying to update via API.");
90
            }
91
92
            $this->logger->addCritical("Card Registration ID: $cardRegisteredId not found when querying API.");
93
            throw new PartFireException("Card Registration ID: $cardRegisteredId not found when querying API.");
94
        } catch (ResponseException $e) {
95
            $this->logger->addCritical($e->getMessage(), ['code' => $e->getCode(), 'details' => $e->GetErrorDetails()]);
96
            throw new PartFireException($e->getMessage(), $e->getCode(), $e);
97
        } catch (Exception $e) {
98
            $this->logger->addError($e->getMessage());
99
            throw new PartFireException($e->getMessage(), $e->getCode(), $e);
100
        }
101
    }
102
103 View Code Duplication
    public function get(string $cardRegisteredId): CardRegistrationDto
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
104
    {
105
        try {
106
            $cardRegister = $this->mangoPayApi->CardRegistrations->Get($cardRegisteredId);
107
            if ($cardRegister instanceof CardRegistration) {
108
                return $this->cardTranslator->translateMangoCardRegistrationDataToDto($cardRegister);
109
            }
110
            $this->logger->addCritical("Card Registration ID: $cardRegisteredId not found when querying API.");
111
            throw new PartFireException("Card Registration ID: $cardRegisteredId not found when querying API.");
112
        } catch (ResponseException $e) {
113
            $this->logger->addCritical($e->getMessage(), ['code' => $e->getCode(), 'details' => $e->GetErrorDetails()]);
114
            throw new PartFireException($e->getMessage(), $e->getCode(), $e);
115
        } catch (Exception $e) {
116
            $this->logger->addError($e->getMessage());
117
            throw new PartFireException($e->getMessage(), $e->getCode(), $e);
118
        }
119
    }
120
}
121