Passed
Push — master ( 6c1a60...321d9a )
by Carl
02:50
created

PayInQuery   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 94
Duplicated Lines 76.6 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
B createPayInCardDirect() 27 27 5
B createBankWireDirectPayIn() 28 28 5
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:    19/01/2017
9
 * Time:    12:47
10
 * File:    PayInQuery.php
11
 **/
12
13
namespace PartFire\MangoPayBundle\Models\Adapters;
14
15
use MangoPay\PayIn;
16
use MangoPay\PayInStatus;
17
use PartFire\MangoPayBundle\Models\DTOs\BankwireDirectPayIn;
18
use PartFire\MangoPayBundle\Models\DTOs\CardDirectPayIn;
19
use PartFire\MangoPayBundle\Models\DTOs\Translators\PayInTranslator;
20
use PartFire\MangoPayBundle\Models\PayInQueryInterface;
21
use Symfony\Bridge\Monolog\Logger;
22
use PartFire\MangoPayBundle\Models\Exception as PartFireException;
23
use MangoPay\Libraries\Exception;
24
use MangoPay\Libraries\ResponseException;
25
use MangoPay\MangoPayApi;
26
27
class PayInQuery extends AbstractQuery implements PayInQueryInterface
28
{
29
    /**
30
     * @var PayInTranslator
31
     */
32
    private $payInTranslator;
33
34
    public function __construct(
35
        $clientId,
36
        $clientPassword,
37
        $baseUrl,
38
        MangoPayApi $mangoPayApi,
39
        Logger $logger,
40
        PayInTranslator $payInTranslator
41
    ) {
42
        parent::__construct($clientId, $clientPassword, $baseUrl, $mangoPayApi, $logger);
43
        $this->payInTranslator = $payInTranslator;
44
    }
45
46 View Code Duplication
    public function createPayInCardDirect(CardDirectPayIn $cardDirectPayIn) : CardDirectPayIn
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...
47
    {
48
        try {
49
            $payIn = $this->payInTranslator->translateDtoToMangoPayInForDirectPayIn($cardDirectPayIn);
50
            $createdPayIn = $this->mangoPayApi->PayIns->Create($payIn);
51
52
            if ($createdPayIn instanceof PayIn) {
53
                if ($createdPayIn->Status == PayInStatus::Succeeded) {
54
                    return $this->payInTranslator->translateMangoPayDirectPayInToDto($createdPayIn);
55
                }
56
                $this->logger->addCritical(
57
                    "Pay-In has been created with status: ".$createdPayIn->Status . ' (result code: ' . $createdPayIn->ResultCode . ')'
58
                );
59
                throw new PartFireException(
60
                    "Pay-In has been created with status: ".$createdPayIn->Status . ' (result code: ' . $createdPayIn->ResultCode . ')'
61
                );
62
            }
63
            $this->logger->addCritical("Failed to create PayIn");
64
            throw new PartFireException("Failed to create PayIn");
65
        } catch (ResponseException $e) {
66
            $this->logger->addCritical($e->getMessage(), ['code' => $e->getCode(), 'details' => $e->GetErrorDetails()]);
67
            throw new PartFireException($e->getMessage(), $e->getCode(), $e);
68
        } catch (Exception $e) {
69
            $this->logger->addError($e->getMessage());
70
            throw new PartFireException($e->getMessage(), $e->getCode(), $e);
71
        }
72
    }
73
74 View Code Duplication
    public function createBankWireDirectPayIn(BankwireDirectPayIn $bankwireDirectPayIn) : BankwireDirectPayIn
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...
75
    {
76
        try {
77
            $payIn = $this->payInTranslator->translateDtoToMangoPayInForBankwireDirectPayIn($bankwireDirectPayIn);
78
            $createdPayIn = $this->mangoPayApi->PayIns->Create($payIn);
79
80
            if ($createdPayIn instanceof PayIn) {
81
82
                if ($createdPayIn->Status == PayInStatus::Created) {
83
                    return $this->payInTranslator->translateMangoPayBankwireDirectPayInToDto($createdPayIn);
84
                }
85
                $this->logger->addCritical(
86
                    "Pay-In has been created with status: ".$createdPayIn->Status . ' (result code: ' . $createdPayIn->ResultCode . ')'
87
                );
88
                throw new PartFireException(
89
                    "Pay-In has been created with status: ".$createdPayIn->Status . ' (result code: ' . $createdPayIn->ResultCode . ')'
90
                );
91
            }
92
            $this->logger->addCritical("Failed to create PayIn");
93
            throw new PartFireException("Failed to create PayIn");
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($id) : BankwireDirectPayIn
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
            $payIn = $this->mangoPayApi->PayIns->Get($id);
107
            if ($payIn instanceof PayIn) {
108
                return $this->payInTranslator->translateMangoPayBankwireDirectPayInToDto($payIn);
109
            }
110
            $this->logger->addCritical("Failed to get PayIn");
111
            throw new PartFireException("Failed to get PayIn");
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