Passed
Push — master ( ffec38...d087b3 )
by
unknown
02:56
created

PayInQuery::createPayInCardDirect()   A

Complexity

Conditions 4
Paths 11

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.2
c 0
b 0
f 0
cc 4
eloc 14
nc 11
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\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
    public function createPayInCardDirect(CardDirectPayIn $cardDirectPayIn) : CardDirectPayIn
47
    {
48
        try {
49
            $payIn = $this->payInTranslator->translateDtoToMangoPayInForDirectPayIn($cardDirectPayIn);
50
            $createdPayIn = $this->mangoPayApi->PayIns->Create($payIn);
51
52
            if ($createdPayIn instanceof PayIn) {
53
                return $this->payInTranslator->translateMangoPayDirectPayInToDto($createdPayIn);
54
            }
55
            $this->logger->addCritical("Failed to create PayIn");
56
            throw new PartFireException("Failed to create PayIn");
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 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...
67
    {
68
        try {
69
            $payIn = $this->payInTranslator->translateDtoToMangoPayInForBankwireDirectPayIn($bankwireDirectPayIn);
70
            $createdPayIn = $this->mangoPayApi->PayIns->Create($payIn);
71
72
            if ($createdPayIn instanceof PayIn) {
73
74
                if ($createdPayIn->Status == PayInStatus::Created) {
75
                    return $this->payInTranslator->translateMangoPayBankwireDirectPayInToDto($createdPayIn);
76
                }
77
                $this->logger->addCritical(
78
                    "Pay-In has been created with status: ".$createdPayIn->Status . ' (result code: ' . $createdPayIn->ResultCode . ')'
79
                );
80
                throw new PartFireException(
81
                    "Pay-In has been created with status: ".$createdPayIn->Status . ' (result code: ' . $createdPayIn->ResultCode . ')'
82
                );
83
            }
84
            $this->logger->addCritical("Failed to create PayIn");
85
            throw new PartFireException("Failed to create PayIn");
86
        } catch (ResponseException $e) {
87
            $this->logger->addCritical($e->getMessage(), ['code' => $e->getCode(), 'details' => $e->GetErrorDetails()]);
88
            throw new PartFireException($e->getMessage(), $e->getCode(), $e);
89
        } catch (Exception $e) {
90
            $this->logger->addError($e->getMessage());
91
            throw new PartFireException($e->getMessage(), $e->getCode(), $e);
92
        }
93
    }
94
95 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...
96
    {
97
        try {
98
            $payIn = $this->mangoPayApi->PayIns->Get($id);
99
            if ($payIn instanceof PayIn) {
100
                return $this->payInTranslator->translateMangoPayBankwireDirectPayInToDto($payIn);
101
            }
102
            $this->logger->addCritical("Failed to get PayIn");
103
            throw new PartFireException("Failed to get PayIn");
104
        } catch (ResponseException $e) {
105
            $this->logger->addCritical($e->getMessage(), ['code' => $e->getCode(), 'details' => $e->GetErrorDetails()]);
106
            throw new PartFireException($e->getMessage(), $e->getCode(), $e);
107
        } catch (Exception $e) {
108
            $this->logger->addError($e->getMessage());
109
            throw new PartFireException($e->getMessage(), $e->getCode(), $e);
110
        }
111
    }
112
}
113