SendTransactions::sendTransaction()   B
last analyzed

Complexity

Conditions 7
Paths 7

Size

Total Lines 48
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
eloc 28
c 4
b 1
f 0
dl 0
loc 48
rs 8.5386
cc 7
nc 7
nop 4
1
<?php
2
3
declare(strict_types=1);
4
/**
5
 * SendTransactions.php
6
 * Copyright (c) 2020 [email protected].
7
 *
8
 * This file is part of the Firefly III bunq importer
9
 * (https://github.com/firefly-iii/bunq-importer).
10
 *
11
 * This program is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License as
13
 * published by the Free Software Foundation, either version 3 of the
14
 * License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License
22
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
23
 */
24
25
namespace App\Services\Sync;
26
27
use App\Services\Configuration\Configuration;
28
use App\Services\Sync\JobStatus\ProgressInformation;
29
use GrumpyDictator\FFIIIApiSupport\Exceptions\ApiHttpException;
30
use GrumpyDictator\FFIIIApiSupport\Model\Transaction;
31
use GrumpyDictator\FFIIIApiSupport\Model\TransactionGroup;
32
use GrumpyDictator\FFIIIApiSupport\Request\PostTransactionRequest;
33
use GrumpyDictator\FFIIIApiSupport\Response\PostTransactionResponse;
34
use GrumpyDictator\FFIIIApiSupport\Response\ValidationErrorResponse;
35
36
/**
37
 * Class SendTransactions.
38
 */
39
class SendTransactions
40
{
41
    use ProgressInformation;
42
43
    /** @var Configuration */
44
    private $configuration;
45
46
    /**
47
     * @param array $transactions
48
     *
49
     * @return array
50
     */
51
    public function send(array $transactions): array
52
    {
53
        $uri   = (string) config('bunq.uri');
54
        $token = (string) config('bunq.access_token');
55
        foreach ($transactions as $index => $transaction) {
56
            app('log')->debug(sprintf('Trying to send transaction #%d', $index), $transaction);
57
            $this->sendTransaction($uri, $token, $index, $transaction);
58
        }
59
60
        return [];
61
    }
62
63
    /**
64
     * @param Configuration $configuration
65
     */
66
    public function setConfiguration(Configuration $configuration): void
67
    {
68
        $this->configuration = $configuration;
69
    }
70
71
    /**
72
     * @param string $uri
73
     * @param string $token
74
     * @param int    $index
75
     * @param array  $transaction
76
     *
77
     * @return array
78
     */
79
    private function sendTransaction(string $uri, string $token, int $index, array $transaction): array
80
    {
81
        $request = new PostTransactionRequest($uri, $token);
82
        $request->setBody($transaction);
83
        try {
84
            /** @var PostTransactionResponse $response */
85
            $response = $request->post();
86
        } catch (ApiHttpException $e) {
87
            app('log')->error($e->getMessage());
88
            $this->addError($index, $e->getMessage());
89
90
            return [];
91
        }
92
        if ($response instanceof ValidationErrorResponse) {
0 ignored issues
show
introduced by
$response is never a sub-type of GrumpyDictator\FFIIIApiS...ValidationErrorResponse.
Loading history...
93
            /** ValidationErrorResponse $error */
94
            foreach ($response->errors->getMessages() as $key => $errors) {
95
                foreach ($errors as $error) {
96
                    // +1 so the line numbers match.
97
                    $this->addError($index + 1, $error);
98
                    app('log')->error(sprintf('Could not create transaction: %s', $error), $transaction);
99
                }
100
            }
101
102
            return [];
103
        }
104
        /** @var TransactionGroup|null $group */
105
        $group = $response->getTransactionGroup();
106
        if (null === $group) {
107
            $this->addError($index + 1, 'Group is unexpectedly NULL.');
108
109
            return [];
110
        }
111
        $groupId  = $group->id;
112
        $uri      = (string) config('bunq.uri');
113
        $groupUri = (string) sprintf('%s/transactions/show/%d', $uri, $groupId);
114
115
        /** @var Transaction $tr */
116
        foreach ($group->transactions as $tr) {
117
            $this->addMessage(
118
                $index + 1,
119
                sprintf(
120
                    'Created transaction #%d: <a href="%s">%s</a> (%s %s)', $groupId, $groupUri, $tr->description, $tr->currencyCode,
121
                    round((float) $tr->amount, 2)
122
                )
123
            );
124
        }
125
126
        return [];
127
    }
128
}
129