Completed
Push — master ( 5ed328...e5c155 )
by Wojciech
03:08
created

ApplicationGateway   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Test Coverage

Coverage 26.67%

Importance

Changes 5
Bugs 0 Features 2
Metric Value
wmc 10
c 5
b 0
f 2
lcom 2
cbo 6
dl 0
loc 146
ccs 12
cts 45
cp 0.2667
rs 10
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 32 and the first side effect is on line 23.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/*
3
* This file is part of the PayBreak/basket package.
4
*
5
* (c) PayBreak <[email protected]>
6
*
7
* For the full copyright and license information, please view the LICENSE
8
* file that was distributed with this source code.
9
*/
10
11
namespace PayBreak\Sdk\Gateways;
12
13
use PayBreak\Sdk\Entities\ApplicationEntity;
14
use PayBreak\Sdk\SdkException;
15
use WNowicki\Generic\ApiClient\ErrorResponseException;
16
17
/**
18
 * Application Gateway
19
 *
20
 * @author WN
21
 * @package PayBreak\Sdk\Gateways
22
 */
23
class ApplicationGateway extends AbstractGateway
0 ignored issues
show
Bug introduced by
Possible parse error: class missing opening or closing brace
Loading history...
24
{
25
    /**
26
     * @author WN
27
     * @param int $id
28
     * @param string $token
29
     * @return ApplicationEntity
30
     * @throws SdkException
31
     */
32 2
    public function getApplication($id, $token)
33
    {
34 2
        return ApplicationEntity::make($this->fetchDocument('/v4/applications/' . $id, $token, 'Application'));
35
    }
36
37
    /**
38
     * @author WN
39
     * @param ApplicationEntity $application
40
     * @param string $token
41
     * @return ApplicationEntity
42
     * @throws SdkException
43
     */
44
    public function initialiseApplication(ApplicationEntity $application, $token)
45
    {
46
        $api = $this->getApiFactory()->makeApiClient($token);
47
48
        try {
49
            $response = $api->post('/v4/initialize-application', $application->toArray(true));
50
51
            $application->setId($response['application']);
52
            $application->setResumeUrl($response['url']);
53
54
            return $application;
55
56
        } catch (ErrorResponseException $e) {
57
58
            $this->logWarning('ApplicationGateway::initialiseApplication[' . $e->getCode() . ']: ' . $e->getMessage());
59
            throw new SdkException($e->getMessage());
60
61
        } catch (\Exception $e) {
62
63
            $this->logError('ApplicationGateway::initialiseApplication[' . $e->getCode() . ']: ' . $e->getMessage());
64
            throw new SdkException('Problem Initialising Application on Provider API');
65
        }
66
    }
67
68
    /**
69
     * @author WN
70
     * @param int $id
71
     * @param string $token
72
     * @return bool
73
     * @throws SdkException
74
     */
75 2
    public function fulfilApplication($id, $token)
76
    {
77 2
        return $this->requestAction('/v4/applications/' . $id . '/fulfil', [], $token);
78
    }
79
80
    /**
81
     * @param int $id
82
     * @param string $description
83
     * @param string $token
84
     * @return bool
85
     * @throws SdkException
86
     */
87 2
    public function cancelApplication($id, $description, $token)
88
    {
89 2
        return $this->requestAction(
90 2
            '/v4/applications/' . $id . '/request-cancellation',
91 2
            ['description' => $description],
92
            $token
93 2
        );
94
    }
95
96
    /**
97
     * @param $token
98
     * @return array
99
     * @throws SdkException
100
     */
101
    public function getPendingCancellations($installationId, $token)
102
    {
103
        return $this->fetchDocument(
104
            '/v4/installations/' . $installationId . '/applications',
105
            $token,
106
            'Pending Cancellations',
107
            ['pending-cancellations' => true]
108
        );
109
    }
110
111
    /**
112
     * Get Merchant Payments. Filter Params can accept
113
     * - since, until : (string - ISO8601 Date Part Only)
114
     * - count, offset : (int)
115
     *
116
     * @author SL
117
     * @param $application
118
     * @param $token
119
     * @param array $filterParams
120
     * @return array
121
     */
122
    public function getMerchantPayments($application, $token, array $filterParams = [])
123
    {
124
        return $this->postDocument(
125
            '/v4/applications/' . $application . '/get-merchant-payments',
126
            $filterParams,
127
            $token,
128
            'Merchant Payments'
129
        );
130
    }
131
132
    /**
133
     * Add a Merchant Payment to the given application. Amount should be supplied in pence.
134
     *
135
     * @author SL
136
     * @param string $application
137
     * @param \DateTime $effectiveDate
138
     * @param int $amount
139
     * @param string $token
140
     * @return array
141
     */
142
    public function addMerchantPayment($application, \DateTime $effectiveDate, $amount, $token)
143
    {
144
        return $this->postDocument(
145
            '/v4/applications/' . $application . '/add-merchant-payment',
146
            [
147
                'amount' => $amount,
148
                'effective_date' => $effectiveDate->format('Y-m-d'),
149
            ],
150
            $token,
151
            'Add Merchant Payment'
152
153
    /**
154
     * @author EB
155
     * @param $installationId
156
     * @param $application
157
     * @param $token
158
     * @return array
159
     */
160
    public function getApplicationCreditInfo($installationId, $application, $token)
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_PUBLIC, expecting ',' or ')'
Loading history...
161
    {
162
        return $this->postDocument(
163 4
            'v4/installations/' . $installationId . '/applications/' . $application . '/get-credit-information',
164
            [],
165 4
            $token,
166 4
            'Application Credit Information'
167
        );
168
    }
169
170
    /**
171
     * @author WN
172
     * @param string $action
173
     * @param array $data
174
     * @param string $token
175
     * @return bool
176
     * @throws SdkException
177
     */
178
    private function requestAction($action, $data, $token)
179
    {
180
        $this->postDocument($action, $data, $token, 'Application');
181
        return true;
182
    }
183
}
184