Passed
Push — master ( b94297...d3f672 )
by Carl
07:13
created

PayInQuery::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 6
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\CardDirectPayIn;
18
use PartFire\MangoPayBundle\Models\DTOs\Translators\PayInTranslator;
19
use PartFire\MangoPayBundle\Models\PayInQueryInterface;
20
use Symfony\Bridge\Monolog\Logger;
21
use PartFire\MangoPayBundle\Models\Exception as PartFireException;
22
use MangoPay\Libraries\Exception;
23
use MangoPay\Libraries\ResponseException;
24
use MangoPay\MangoPayApi;
25
26
class PayInQuery extends AbstractQuery implements PayInQueryInterface
27
{
28
    /**
29
     * @var PayInTranslator
30
     */
31
    private $payInTranslator;
32
33
    public function __construct(
34
        $clientId,
35
        $clientPassword,
36
        $baseUrl,
37
        MangoPayApi $mangoPayApi,
38
        Logger $logger,
39
        PayInTranslator $payInTranslator
40
    ) {
41
        parent::__construct($clientId, $clientPassword, $baseUrl, $mangoPayApi, $logger);
42
        $this->payInTranslator = $payInTranslator;
43
    }
44
45
    public function createPayInCardDirect(CardDirectPayIn $cardDirectPayIn)
46
    {
47
        try {
48
            $payIn = $this->payInTranslator->translateDtoToMangoPayInForDirectPayIn($cardDirectPayIn);
49
            $createdPayIn = $this->mangoPayApi->PayIns->Create($payIn);
50
51
            if ($createdPayIn instanceof PayIn) {
52
                if ($createdPayIn->Status == PayInStatus::Succeeded) {
53
                    return $this->payInTranslator->translateMangoPayDirectPayInToDto($createdPayIn);
54
                }
55
                $this->logger->addCritical(
56
                    "Pay-In has been created with status: ".$createdPayIn->Status . ' (result code: ' . $createdPayIn->ResultCode . ')'
57
                );
58
                throw new PartFireException(
59
                    "Pay-In has been created with status: ".$createdPayIn->Status . ' (result code: ' . $createdPayIn->ResultCode . ')'
60
                );
61
            }
62
            $this->logger->addCritical("Failed to create PayIn");
63
            throw new PartFireException("Failed to create PayIn");
64
        } catch (ResponseException $e) {
65
            $this->logger->addCritical($e->getMessage(), ['code' => $e->getCode(), 'details' => $e->GetErrorDetails()]);
66
            throw new PartFireException($e->getMessage(), $e->getCode(), $e);
67
        } catch (Exception $e) {
68
            $this->logger->addError($e->getMessage());
69
            throw new PartFireException($e->getMessage(), $e->getCode(), $e);
70
        }
71
    }
72
}
73