PayInQuery::get()   B
last analyzed

Complexity

Conditions 6
Paths 16

Size

Total Lines 22
Code Lines 16

Duplication

Lines 22
Ratio 100 %

Importance

Changes 0
Metric Value
dl 22
loc 22
rs 8.6737
c 0
b 0
f 0
cc 6
eloc 16
nc 16
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:    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\MangoPayConstants;
18
use PartFire\MangoPayBundle\Models\DTOs\BankwireDirectPayIn;
19
use PartFire\MangoPayBundle\Models\DTOs\CardDirectPayIn;
20
use PartFire\MangoPayBundle\Models\DTOs\Translators\PayInTranslator;
21
use PartFire\MangoPayBundle\Models\PayInQueryInterface;
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
use MangoPay\MangoPayApi;
27
28
class PayInQuery extends AbstractQuery implements PayInQueryInterface
29
{
30
    /**
31
     * @var PayInTranslator
32
     */
33
    private $payInTranslator;
34
35
    public function __construct(
36
        $clientId,
37
        $clientPassword,
38
        $baseUrl,
39
        MangoPayApi $mangoPayApi,
40
        Logger $logger,
41
        PayInTranslator $payInTranslator
42
    ) {
43
        parent::__construct($clientId, $clientPassword, $baseUrl, $mangoPayApi, $logger);
44
        $this->payInTranslator = $payInTranslator;
45
    }
46
47
    public function createPayInCardDirect(CardDirectPayIn $cardDirectPayIn) : CardDirectPayIn
48
    {
49
        try {
50
            $payIn = $this->payInTranslator->translateDtoToMangoPayInForDirectPayIn($cardDirectPayIn);
51
            $createdPayIn = $this->mangoPayApi->PayIns->Create($payIn);
52
53
            if ($createdPayIn instanceof PayIn) {
54
                return $this->payInTranslator->translateMangoPayDirectPayInToDto($createdPayIn);
55
            }
56
            $this->logger->addCritical("Failed to create PayIn");
57
            throw new PartFireException("Failed to create PayIn");
58
        } catch (ResponseException $e) {
59
            $this->logger->addCritical($e->getMessage(), ['code' => $e->getCode(), 'details' => $e->GetErrorDetails()]);
60
            throw new PartFireException($e->getMessage(), $e->getCode(), $e);
61
        } catch (Exception $e) {
62
            $this->logger->addError($e->getMessage());
63
            throw new PartFireException($e->getMessage(), $e->getCode(), $e);
64
        }
65
    }
66
67
    public function createBankWireDirectPayIn(BankwireDirectPayIn $bankwireDirectPayIn) : BankwireDirectPayIn
68
    {
69
        try {
70
            $payIn = $this->payInTranslator->translateDtoToMangoPayInForBankwireDirectPayIn($bankwireDirectPayIn);
71
            $createdPayIn = $this->mangoPayApi->PayIns->Create($payIn);
72
73
            if ($createdPayIn instanceof PayIn) {
74
75
                if ($createdPayIn->Status == PayInStatus::Created) {
76
                    return $this->payInTranslator->translateMangoPayBankwireDirectPayInToDto($createdPayIn);
77
                }
78
                $this->logger->addCritical(
79
                    "Pay-In has been created with status: ".$createdPayIn->Status . ' (result code: ' . $createdPayIn->ResultCode . ')'
80
                );
81
                throw new PartFireException(
82
                    "Pay-In has been created with status: ".$createdPayIn->Status . ' (result code: ' . $createdPayIn->ResultCode . ')'
83
                );
84
            }
85
            $this->logger->addCritical("Failed to create PayIn");
86
            throw new PartFireException("Failed to create PayIn");
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
    /*
97
    * TODO: Sort the return type for this
98
     *
99
     */
100
101 View Code Duplication
    public function get($id)
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...
102
    {
103
        try {
104
            $payIn = $this->mangoPayApi->PayIns->Get($id);
105
            if ($payIn instanceof PayIn) {
106
                if ($payIn->PaymentType == MangoPayConstants::PAY_IN_TYPE_BANK_WIRE) {
107
                    return $this->payInTranslator->translateMangoPayBankwireDirectPayInToDto($payIn);
108
                }
109
                if ($payIn->PaymentType == MangoPayConstants::PAY_IN_TYPE_CARD) {
110
                    return $this->payInTranslator->translateMangoPayDirectPayInToDto($payIn);
111
                }
112
            }
113
            $this->logger->addCritical("Failed to get PayIn");
114
            throw new PartFireException("Failed to get PayIn");
115
        } catch (ResponseException $e) {
116
            $this->logger->addCritical($e->getMessage(), ['code' => $e->getCode(), 'details' => $e->GetErrorDetails()]);
117
            throw new PartFireException($e->getMessage(), $e->getCode(), $e);
118
        } catch (Exception $e) {
119
            $this->logger->addError($e->getMessage());
120
            throw new PartFireException($e->getMessage(), $e->getCode(), $e);
121
        }
122
    }
123
}
124