Completed
Push — master ( 7e3634...ff897c )
by Gilmar
02:36
created

Manager::refund()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file is part of gpupo/adyen-sdk
5
 *
6
 * (c) Gilmar Pupo <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * For more information, see
12
 * <http://www.g1mr.com/adyen-sdk/>.
13
 */
14
15
namespace Gpupo\AdyenSdk\Payment\Request;
16
17
use Gpupo\AdyenSdk\Factory;
18
use Gpupo\AdyenSdk\Payment\Response\Decorator\ErrorDecorator;
19
use Gpupo\AdyenSdk\Payment\Response\Decorator\ProblematicDecorator;
20
use Gpupo\Common\Interfaces\OptionsInterface;
21
use Gpupo\Common\Traits\OptionsTrait;
22
use Gpupo\CommonSdk\Entity\EntityInterface;
23
use Gpupo\CommonSdk\Entity\ManagerAbstract;
24
use Gpupo\CommonSdk\Response;
25
26
/**
27
 * Gerenciamento de Transações Adyen.
28
 */
29
class Manager extends ManagerAbstract implements OptionsInterface
30
{
31
    use OptionsTrait;
32
33
    /**
34
     * @internal
35
     */
36
    public function update(EntityInterface $entity, EntityInterface $existent)
37
    {
38
    }
39
40
    protected $entity = 'Request';
41
42
    protected $maps = [
43
        'submit'            => ['POST', '/authorise'],
44
        'capture'           => ['POST', '/capture'],
45
        'refund'            => ['POST', '/refund'],
46
        'cancelOrRefund'    => ['POST', '/cancelOrRefund'],
47
    ];
48
49
    protected function preExecute(Request $request)
50
    {
51
        $request->setMerchantAccount($this->getOptions()->get('merchant_account'));
52
53
        return $request;
54
    }
55
56
    protected function call(Request $request, $route)
57
    {
58
        $response = $this->execute($this->factoryMap($route), $request->toJson());
59
60
        return $this->processExecute($request, $response);
61
    }
62
63
    public function blow(Request $request, $route)
64
    {
65
        $request = $this->preExecute($request);
66
        try {
67
            return $this->call($request, $route);
68
        } catch (\Exception $exception) {
69
            return new ErrorDecorator($exception);
0 ignored issues
show
Documentation introduced by
$exception is of type object<Exception>, but the function expects a array|object<Gpupo\Commo...y\EntityInterface>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
70
        }
71
    }
72
73
    public function submit(Request $request)
74
    {
75
        return $this->blow($request, 'submit');
76
    }
77
78
    public function capture(Request $request)
79
    {
80
        $request->setType('capture');
81
82
        return $this->blow($request, 'capture');
83
    }
84
85
    public function refund(Request $request, $modificationValue)
86
    {
87
        $request->setType('refund');
88
        $request->getOrder()->setModificationValue($modificationValue);
89
90
        return $this->blow($request, 'refund');
91
    }
92
93
    public function cancelOrRefund(Request $request)
94
    {
95
        $request->setType('cancelOrRefund');
96
97
        return $this->blow($request, 'cancelOrRefund');
98
    }
99
100
    protected function processExecute(Request $request, Response $response)
101
    {
102
        if (300 > $response->getHttpStatusCode()) {
103
            $decorator = $this->resolveDecorator($request);
104
        } else {
105
            $decorator = $this->getFullyQualifiedDecoratorName('ProblematicDecorator');
106
        }
107
108
        $data = $response->getData()->toArray();
109
        $instance = new $decorator($data);
110
        $instance->setCode($response->getHttpStatusCode());
111
112
        return $instance;
113
    }
114
115
    protected function getFullyQualifiedDecoratorName($name)
116
    {
117
        return Factory::PACKAGENAME . 'Payment\Response\Decorator\\' . $name;
118
    }
119
120
    protected function resolveDecorator(Request $request)
121
    {
122
        $className = $this->getFullyQualifiedDecoratorName($request->getDecoratorName());
123
124
        if ( ! class_exists($className)) {
125
            throw new \InvalidArgumentException('Response type [' . $request->getType() . '] not supported!');
126
        }
127
128
        return $className;
129
    }
130
}
131