Passed
Push — master ( d4daf1...cef0e5 )
by Manuel
01:35
created

QrCode   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 25
rs 10
c 0
b 0
f 0
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A writeFile() 0 19 2
1
<?php
2
3
namespace Sprain\SwissQrBill\QrCode;
4
5
use Endroid\QrCode\QrCode as BaseQrCode;
6
use Endroid\QrCode\QrCodeInterface;
7
use Endroid\QrCode\WriterRegistry;
8
use Sprain\SwissQrBill\QrCode\Exception\UnsupportedFileExtensionException;
9
10
class QrCode extends BaseQrCode implements QrCodeInterface
11
{
12
    // A file extension is supported if the underlying library supports it,
13
    // including the possibility to add a logo in the center of the qr code.
14
    private const SUPPORTED_EXTENSIONS = ['png', 'svg'];
15
16
    public function writeFile(string $path): void
17
    {
18
        $extension = strtolower(pathinfo($path, PATHINFO_EXTENSION));
19
20
        if (!in_array($extension, self::SUPPORTED_EXTENSIONS)) {
21
            throw new UnsupportedFileExtensionException(sprintf(
22
                'Your file cannot be saved. Only these file extensions are supported: %s',
23
                implode(', ', self::SUPPORTED_EXTENSIONS)
24
            ));
25
        }
26
27
        // This block becomes obsolete when the following pull request gets merged:
28
        // https://github.com/endroid/qr-code/pull/191
29
        $writerRegistry = new WriterRegistry();
30
        $writerRegistry->loadDefaultWriters();
31
        $this->setWriterRegistry($writerRegistry);
32
33
        $this->setWriterByExtension($extension);
34
        parent::writeFile($path);
35
    }
36
}