Passed
Branch [email protected] (aae97d)
by Bruno
10:40
created

ConfigWallet::isActive()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 2
b 0
f 0
nc 1
nop 1
dl 0
loc 8
rs 10
1
<?php
2
/**
3
 * Copyright © Getnet. All rights reserved.
4
 *
5
 * @author    Bruno Elisei <[email protected]>
6
 * See LICENSE for license details.
7
 */
8
declare(strict_types=1);
9
10
namespace Getnet\PaymentMagento\Gateway\Config;
11
12
use BaconQrCode\Renderer\Image\SvgImageBackEnd;
13
use BaconQrCode\Renderer\ImageRenderer;
14
use BaconQrCode\Renderer\RendererStyle\RendererStyle;
15
use BaconQrCode\Writer;
16
use Magento\Framework\App\Config\ScopeConfigInterface;
17
use Magento\Framework\App\Filesystem\DirectoryList;
18
use Magento\Framework\Filesystem;
19
use Magento\Framework\Filesystem\Directory\WriteInterface;
20
use Magento\Framework\Filesystem\Io\File;
21
use Magento\Payment\Gateway\Config\Config as PaymentConfig;
22
use Magento\Store\Model\ScopeInterface;
23
24
/**
25
 * Class ConfigWallet - Returns form of payment configuration properties.
26
 *
27
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
28
 */
29
class ConfigWallet extends PaymentConfig
30
{
31
    /**
32
     * @const string
33
     */
34
    public const METHOD = 'getnet_paymentmagento_wallet';
35
36
    /**
37
     * @const string
38
     */
39
    public const ACTIVE = 'active';
40
41
    /**
42
     * @const string
43
     */
44
    public const TITLE = 'title';
45
46
    /**
47
     * @const string
48
     */
49
    public const INSTRUCTION_CHECKOUT = 'instruction_checkout';
50
51
    /**
52
     * @var ScopeConfigInterface
53
     */
54
    protected $scopeConfig;
55
56
    /**
57
     * @var File
58
     */
59
    protected $fileIo;
60
61
    /**
62
     * @var Filesystem
63
     */
64
    protected $filesystem;
65
66
    /**
67
     * @var Config
68
     */
69
    protected $config;
70
71
    /**
72
     * @param ScopeConfigInterface $scopeConfig
73
     * @param File                 $fileIo
74
     * @param Filesystem           $filesystem
75
     * @param DirectoryList        $directoryList
76
     * @param Config               $config
77
     * @param string|null          $methodCode
78
     */
79
    public function __construct(
80
        ScopeConfigInterface $scopeConfig,
81
        File $fileIo,
82
        Filesystem $filesystem,
83
        DirectoryList $directoryList,
84
        Config $config,
85
        $methodCode = null
86
    ) {
87
        parent::__construct($scopeConfig, $methodCode);
88
        $this->scopeConfig = $scopeConfig;
89
        $this->fileIo = $fileIo;
90
        $this->filesystem = $filesystem;
91
        $this->directoryList = $directoryList;
0 ignored issues
show
Bug Best Practice introduced by
The property directoryList does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
92
        $this->config = $config;
93
    }
94
95
    /**
96
     * Get Payment configuration status.
97
     *
98
     * @param int|null $storeId
99
     *
100
     * @return bool
101
     */
102
    public function isActive($storeId = null): bool
103
    {
104
        $pathPattern = 'payment/%s/%s';
105
106
        return (bool) $this->scopeConfig->getValue(
107
            sprintf($pathPattern, self::METHOD, self::ACTIVE),
108
            ScopeInterface::SCOPE_STORE,
109
            $storeId
110
        );
111
    }
112
113
    /**
114
     * Get title of payment.
115
     *
116
     * @param int|null $storeId
117
     *
118
     * @return string|null
119
     */
120
    public function getTitle($storeId = null): ?string
121
    {
122
        $pathPattern = 'payment/%s/%s';
123
124
        return $this->scopeConfig->getValue(
125
            sprintf($pathPattern, self::METHOD, self::TITLE),
126
            ScopeInterface::SCOPE_STORE,
127
            $storeId
128
        );
129
    }
130
131
    /**
132
     * Get Instruction - Checkoout.
133
     *
134
     * @param int|null $storeId
135
     *
136
     * @return string|null
137
     */
138
    public function getInstructionCheckout($storeId = null): ?string
139
    {
140
        $pathPattern = 'payment/%s/%s';
141
142
        return $this->scopeConfig->getValue(
143
            sprintf($pathPattern, self::METHOD, self::INSTRUCTION_CHECKOUT),
144
            ScopeInterface::SCOPE_STORE,
145
            $storeId
146
        );
147
    }
148
149
    /**
150
     * Generate Image Qr Code.
151
     *
152
     * @param string $qrCode
153
     * @param string $transactionId
154
     *
155
     * @return string
156
     */
157
    public function generateImageQrCode($qrCode, $transactionId)
158
    {
159
        $fileName = null;
160
        if ($this->hasPathDir()) {
161
            $fileName = 'getnet/wallet/'.$transactionId.'.svg';
162
            $mediaDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::MEDIA);
163
            $filePath = $this->getPathFile($mediaDirectory, $transactionId);
0 ignored issues
show
Bug introduced by
$mediaDirectory of type Magento\Framework\Filesy...irectory\WriteInterface is incompatible with the type string expected by parameter $mediaDirectory of Getnet\PaymentMagento\Ga...igWallet::getPathFile(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

163
            $filePath = $this->getPathFile(/** @scrutinizer ignore-type */ $mediaDirectory, $transactionId);
Loading history...
164
            $generate = $this->createImageQrCode($mediaDirectory, $filePath, $qrCode);
165
            if (!$generate) {
166
                return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type string.
Loading history...
167
            }
168
        }
169
170
        return $fileName;
171
    }
172
173
    /**
174
     * Get Path File.
175
     *
176
     * @param string $mediaDirectory
177
     * @param string $transactionId
178
     *
179
     * @return string
180
     */
181
    public function getPathFile($mediaDirectory, $transactionId): string
182
    {
183
        $filePath = $mediaDirectory->getAbsolutePath('getnet/wallet/'.$transactionId.'.svg');
184
185
        return $filePath;
186
    }
187
188
    /**
189
     * Create Image Qr Code.
190
     *
191
     * @param WriteInterface $writeDirectory
192
     * @param string         $filePath
193
     * @param string         $qrCode
194
     *
195
     * @throws FileSystemException
196
     *
197
     * @return bool
198
     */
199
    public function createImageQrCode(WriteInterface $writeDirectory, $filePath, $qrCode): bool
200
    {
201
        try {
202
            $stream = $writeDirectory->openFile($filePath, 'w+');
203
            $stream->lock();
204
            $renderer = new ImageRenderer(
205
                new RendererStyle(200),
206
                new SvgImageBackEnd()
207
            );
208
            $writer = new Writer($renderer);
209
            $image = $writer->writeString($qrCode);
210
            $stream->write($image);
211
            $stream->unlock();
212
            $stream->close();
213
        } catch (FileSystemException $ex) {
0 ignored issues
show
Bug introduced by
The type Getnet\PaymentMagento\Ga...fig\FileSystemException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
214
            new Phrase($ex->getMessage());
0 ignored issues
show
Bug introduced by
The type Getnet\PaymentMagento\Gateway\Config\Phrase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
215
216
            return false;
217
        }
218
219
        return true;
220
    }
221
222
    /**
223
     * Check or Create Dir.
224
     *
225
     * @return bool
226
     */
227
    public function hasPathDir(): bool
228
    {
229
        $walletPath = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath('/getnet/wallet');
230
231
        return $this->fileIo->checkAndCreateFolder($walletPath);
232
    }
233
}
234