Test Failed
Pull Request — master (#9)
by Laurens
01:59
created

ImageFileUpload::validateImageSize()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LauLamanApps\IzettleApi\Client\Image;
6
7
use Exception;
8
use LauLamanApps\IzettleApi\Client\Image\Exceptions\FileIsNotAnImageException;
9
use LauLamanApps\IzettleApi\Client\Image\Exceptions\ImageIsToSmallException;
10
use LauLamanApps\IzettleApi\Client\Image\Exceptions\ImageTypeNotAllowedException;
11
use LauLamanApps\IzettleApi\Client\Image\Exceptions\MaximumImageFileSizeExcededException;
12
13
final class ImageFileUpload implements ImageUploadRequestInterface
14
{
15
    const ALLOWED_FILE_TYPES = [
16
        IMAGETYPE_GIF => 'GIF',
17
        IMAGETYPE_JPEG => 'JPEG',
18
        IMAGETYPE_PNG => 'PNG',
19
        IMAGETYPE_BMP => 'BMP',
20
        IMAGETYPE_TIFF_II => 'TIFF',
21
        IMAGETYPE_TIFF_MM => 'TIFF'
22
    ];
23
    const MAX_FILE_SIZE_MB = 5;
24
    const MINIMAL_HEIGHT = 50;
25
    const MINIMAL_WIDTH = 50;
26
27
    private $imageFormat;
28
    private $imageData;
29
30
    public function __construct(string $filename)
31
    {
32
        $this->validateFile($filename);
33
        $this->imageFormat = self::ALLOWED_FILE_TYPES[exif_imagetype($filename)];
34
        $this->imageData = file_get_contents($filename);
35
    }
36
37
    public function getUploadRequest(): array
38
    {
39
        return [
40
            'imageFormat' => $this->imageFormat,
41
            'imageData' => $this->imageData,
42
        ];
43
    }
44
45
    private function validateFile(string $file)
46
    {
47
        self::validateFileSize($file);
48
        self::validatedImageType($file);
49
        self::validateImageSize($file);
50
    }
51
52
    private static function validateFileSize($file)
53
    {
54
        $maxFileSizeBytes = (self::MAX_FILE_SIZE_MB * 1024 * 1024);
55
        if (filesize($file) > $maxFileSizeBytes) {
56
            throw new MaximumImageFileSizeExcededException(sprintf('Max file size is \'%d Mb\'', self::MAX_FILE_SIZE_MB));
57
        }
58
    }
59
60
    private static function validatedImageType(string $file)
61
    {
62
        $type = false;
63
64
        try {
65
            $type = exif_imagetype($file);
66
        } catch (Exception $e) {
67
            //-- $type is already false
68
        }
69
70
        if ($type === false) {
71
            throw new FileIsNotAnImageException();
72
        }
73
74
        if (!array_key_exists($type, self::ALLOWED_FILE_TYPES)) {
75
            throw new ImageTypeNotAllowedException(
76
                sprintf('Allowed: %s', implode(', ', self::ALLOWED_FILE_TYPES))
77
            );
78
        }
79
    }
80
81
    private static function validateImageSize(string $file)
82
    {
83
        list($width, $height) = getimagesize($file);
84
85
        if ($width < self::MINIMAL_WIDTH or $height < self::MINIMAL_HEIGHT) {
86
            throw new ImageIsToSmallException(
87
                sprintf(
88
                    'Minimal image dimensions not met. Required: \'%dx%d\' Provided: \'%dx%d\'',
89
                    self::MINIMAL_HEIGHT,
90
                    self::MINIMAL_WIDTH,
91
                    $height,
92
                    $width
93
                )
94
            );
95
        }
96
    }
97
}
98