Passed
Push — master ( 16566e...bc93e7 )
by Manuel
01:06 queued 11s
created

QrCode   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 15
dl 0
loc 30
rs 10
c 3
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A writeFile() 0 5 1
A setWriterByExtension() 0 11 2
1
<?php
2
3
namespace Sprain\SwissQrBill\QrCode;
4
5
use Endroid\QrCode\QrCode as BaseQrCode;
6
use Endroid\QrCode\QrCodeInterface;
7
use Sprain\SwissQrBill\QrCode\Exception\UnsupportedFileExtensionException;
8
9
class QrCode extends BaseQrCode implements QrCodeInterface
10
{
11
    const FILE_FORMAT_PNG = 'png';
12
    const FILE_FORMAT_SVG = 'svg';
13
14
    // A file extension is supported if the underlying library supports it,
15
    // including the possibility to add a logo in the center of the qr code.
16
    const SUPPORTED_FILE_FORMATS = [
17
        self::FILE_FORMAT_PNG,
18
        self::FILE_FORMAT_SVG
19
    ];
20
21
    public function writeFile(string $path): void
22
    {
23
        $extension = strtolower(pathinfo($path, PATHINFO_EXTENSION));
24
        $this->setWriterByExtension($extension);
25
        parent::writeFile($path);
26
    }
27
28
    public function setWriterByExtension(string $extension): void
29
    {
30
        if (!in_array($extension, self::SUPPORTED_FILE_FORMATS)) {
31
            throw new UnsupportedFileExtensionException(sprintf(
32
                'The qr code file cannot be created. Only these file extensions are supported: %s. You provided: %s.',
33
                implode(', ', self::SUPPORTED_FILE_FORMATS),
34
                $extension
35
            ));
36
        }
37
38
        parent::setWriterByExtension($extension);
39
    }
40
}