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 |
|
|
|
|
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
|
|
|
|
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.