PayOutQuery   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 66
Duplicated Lines 25.76 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A getBankWire() 17 17 4
B createBankWire() 0 28 5

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:    20/01/2017
9
 * Time:    15:40
10
 * File:    PayOutQuery.php
11
 **/
12
13
namespace PartFire\MangoPayBundle\Models\Adapters;
14
15
16
use MangoPay\PayOut;
17
use MangoPay\PayOutStatus;
18
use PartFire\MangoPayBundle\Models\DTOs\PayOutBankWire;
19
use PartFire\MangoPayBundle\Models\DTOs\Translators\PayOutTranslator;
20
use PartFire\MangoPayBundle\Models\PayOutQueryInterface;
21
use PartFire\MangoPayBundle\Models\Exception as PartFireException;
22
use MangoPay\Libraries\Exception;
23
use MangoPay\Libraries\ResponseException;
24
use MangoPay\MangoPayApi;
25
use Symfony\Bridge\Monolog\Logger;
26
27
class PayOutQuery extends AbstractQuery implements PayOutQueryInterface
28
{
29
    /**
30
     * @var PayOutTranslator
31
     */
32
    private $payOutTranslator;
33
34
    public function __construct(
35
        $clientId,
36
        $clientPassword,
37
        $baseUrl,
38
        MangoPayApi $mangoPayApi,
39
        Logger $logger,
40
        PayOutTranslator $payOutTranslator
41
    ) {
42
        parent::__construct($clientId, $clientPassword, $baseUrl, $mangoPayApi, $logger);
43
        $this->payOutTranslator = $payOutTranslator;
44
    }
45
46
    public function createBankWire(PayOutBankWire $payOutBankWire) : PayOutBankWire
47
    {
48
        try {
49
            $payout = $this->payOutTranslator->translatePayOutDtoToMango($payOutBankWire);
50
            $createdPayout = $this->mangoPayApi->PayOuts->Create($payout);
51
52
            if ($createdPayout instanceof PayOut) {
53
54
                if ($createdPayout->Status == PayOutStatus::Created) {
55
                    return $this->payOutTranslator->translateMangoToPayOutDto($createdPayout);
56
                }
57
                $this->logger->addCritical(
58
                    "Pay-Out has been created with status: ".$createdPayout->Status . ' (result code: ' . $createdPayout->ResultCode . ' '. $createdPayout->ResultMessage.')'
59
                );
60
                throw new PartFireException(
61
                    "Pay-Out has been created with status: ".$createdPayout->Status . ' (result code: ' . $createdPayout->ResultCode . ' '. $createdPayout->ResultMessage.')'
62
                );
63
            }
64
            $this->logger->addCritical("Failed to create PayOut");
65
            throw new PartFireException("Failed to create PayOut");
66
        } catch (ResponseException $e) {
67
            $this->logger->addCritical($e->getMessage(), ['code' => $e->getCode(), 'details' => $e->GetErrorDetails()]);
68
            throw new PartFireException($e->getMessage(), $e->getCode(), $e);
69
        } catch (Exception $e) {
70
            $this->logger->addError($e->getMessage());
71
            throw new PartFireException($e->getMessage(), $e->getCode(), $e);
72
        }
73
    }
74
75 View Code Duplication
    public function getBankWire(string $payOutId): PayOutBankWire
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...
76
    {
77
        try {
78
            $payout = $this->mangoPayApi->PayOuts->Get($payOutId);
79
            if ($payout instanceof PayOut) {
80
                return $this->payOutTranslator->translateMangoToPayOutDto($payout);
81
            }
82
            $this->logger->addCritical("could not get pay out when querying API.");
83
            throw new PartFireException("could not get pay out when querying API.");
84
        } catch (ResponseException $e) {
85
            $this->logger->addCritical($e->getMessage(), ['code' => $e->getCode(), 'details' => $e->GetErrorDetails()]);
86
            throw new PartFireException($e->getMessage(), $e->getCode(), $e);
87
        } catch (Exception $e) {
88
            $this->logger->addError($e->getMessage());
89
            throw new PartFireException($e->getMessage(), $e->getCode(), $e);
90
        }
91
    }
92
}
93