Failed Conditions
Push — master ( a427b2...509ea4 )
by Adrien
16:44
created

BankingInfos   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Test Coverage

Coverage 10.77%

Importance

Changes 0
Metric Value
wmc 10
eloc 72
dl 0
loc 115
ccs 7
cts 65
cp 0.1077
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B qrBill() 0 70 9
A build() 0 36 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application\Api\Field\Query;
6
7
use Application\Model\User;
8
use Ecodev\Felix\Api\Field\FieldInterface;
9
use Ecodev\Felix\Service\Bvr;
10
use GraphQL\Type\Definition\Type;
11
use Laminas\Http\Client;
12
use Laminas\Http\Client\Exception\RuntimeException;
13
use Laminas\Http\Request;
14
use Laminas\Http\Response;
15
use Money\Money;
16
17
abstract class BankingInfos implements FieldInterface
18
{
19 1
    public static function build(): array
20
    {
21
        return
22
            [
23 1
                'name' => 'bankingInfos',
24 1
                'type' => Type::nonNull(_types()->get('BankingInfos')),
25 1
                'description' => 'Info to top-up the current user account by bank transfer',
26
                'args' => [
27 1
                    'user' => Type::nonNull(_types()->getId(User::class)),
28 1
                    'amount' => _types()->get('Money'),
29
                ],
30 1
                'resolve' => function ($root, array $args): array {
31
                    global $container;
32
                    $config = $container->get('config');
33
                    $banking = $config['banking'];
34
                    $iban = $banking['iban'];
35
                    $paymentTo = $banking['paymentTo'];
36
                    $paymentFor = $banking['paymentFor'];
37
38
                    $user = $args['user']->getEntity();
39
                    $amount = $args['amount'] ?? null;
40
41
                    $referenceNumber = Bvr::getReferenceNumber('000000', (string) $user->getId());
42
43
                    $result = [
44
                        'iban' => $iban,
45
                        'paymentTo' => $paymentTo,
46
                        'paymentFor' => $paymentFor,
47
                        'referenceNumber' => $referenceNumber,
48
                    ];
49
50
                    $qrCodeField = self::qrBill($user, $amount, $iban, $paymentFor, false);
51
                    $qrBillField = self::qrBill($user, $amount, $iban, $paymentFor, true);
52
                    $result = array_merge($result, $qrCodeField, $qrBillField);
53
54
                    return $result;
55
                },
56
            ];
57
    }
58
59
    /**
60
     * Lazy resolve qrBill or qrCode fields for banking infos query.
61
     */
62
    protected static function qrBill(User $user, ?Money $amount, string $iban, string $paymentFor, bool $paymentPart): array
63
    {
64
        $resolve = function () use ($user, $amount, $iban, $paymentFor, $paymentPart): ?string {
65
            global $container;
66
67
            if (!Bvr::isQrIban($iban)) {
68
                return null;
69
            }
70
71
            $config = $container->get('config');
72
            $request = new Request();
73
            $request->setUri('https://qrbill.ecodev.ch/qr-code');
74
            $request->getHeaders()->addHeaders([
75
                'Content-Type' => 'application/json',
76
                'Accept' => $paymentPart ? 'application/json application/pdf' : 'application/json image/svg+xml',
77
                'Referer' => 'https://' . $config['hostname'],
78
            ]);
79
            $request->setMethod(Request::METHOD_POST);
80
81
            $creditorLines = explode("\n", (string) $paymentFor);
82
            $creditor = [
83
                'name' => $creditorLines[0],
84
            ];
85
            if (count($creditorLines) > 2) {
86
                $creditor['addressLine1'] = $creditorLines[1];
87
                $creditor['addressLine2'] = $creditorLines[2];
88
            } else {
89
                $creditor['addressLine2'] = $creditorLines[1];
90
            }
91
92
            $attrs = [
93
                'creditor' => $creditor,
94
                'customerIdentification' => null,
95
                'referenceNumber' => (string) $user->getId(),
96
                'iban' => $iban,
97
                'amount' => $amount,
98
            ];
99
100
            if (!empty($user->getPostcode()) && !empty($user->getLocality())) {
101
                $debtor = [
102
                    'name' => $user->getName(),
103
                    'addressLine2' => implode(' ', array_filter([$user->getPostcode(), $user->getLocality()])),
104
                ];
105
                $attrs['ultimateDebtor'] = $debtor;
106
            }
107
108
            $request->setContent(json_encode($attrs));
109
            $client = new Client();
110
111
            try {
112
                /** @var Response $response */
113
                $response = $client->dispatch($request);
114
                if (!$response->isSuccess()) {
115
                    _log()->err('Erreur de génération du QR code: ' . $response->getStatusCode() . ' ' . $response->getReasonPhrase());
116
117
                    return null;
118
                }
119
            } catch (RuntimeException $e) {
120
                _log()->err($e->getMessage(), $attrs);
121
122
                return null;
123
            }
124
125
            $content = json_decode($response->getBody());
126
127
            return $content->qrcode;
128
        };
129
130
        return [
131
            $paymentPart ? 'qrBill' : 'qrCode' => $resolve,
132
        ];
133
    }
134
}
135