CardRegistrationsQuery   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 94
Duplicated Lines 18.09 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 8
dl 17
loc 94
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A create() 0 19 3
C update() 0 36 8
A get() 17 17 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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