Completed
Push — master ( 510f92...73b08b )
by Laurens
01:58
created

ImageFileUpload::validateFileSize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2
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 6
    public function __construct(string $filename)
31
    {
32 6
        $this->validateFile($filename);
33 2
        $this->imageFormat = self::ALLOWED_FILE_TYPES[exif_imagetype($filename)];
34 2
        $this->imageData = iconv('UTF-8', 'UTF-8//IGNORE', utf8_encode(file_get_contents($filename)));
35 2
    }
36
37 2
    public function getUploadRequest(): array
38
    {
39
        return [
40 2
            'imageFormat' => $this->imageFormat,
41 2
            'imageData' => $this->imageData,
42
        ];
43
    }
44
45 6
    private function validateFile(string $file)
46
    {
47 6
        self::validateFileSize($file);
48 5
        self::validatedImageType($file);
49 3
        self::validateImageSize($file);
50 2
    }
51
52 6
    private static function validateFileSize($file)
53
    {
54 6
        $maxFileSizeBytes = (self::MAX_FILE_SIZE_MB * 1024 * 1024);
55 6
        if (filesize($file) > $maxFileSizeBytes) {
56 1
            throw new MaximumImageFileSizeExcededException(sprintf('Max file size is \'%d Mb\'', self::MAX_FILE_SIZE_MB));
57
        }
58 5
    }
59
60 5
    private static function validatedImageType(string $file)
61
    {
62 5
        $type = false;
63
64
        try {
65 5
            $type = exif_imagetype($file);
66 1
        } catch (Exception $e) {
67
            //-- $type is already false
68
        }
69
70 5
        if ($type === false) {
71 1
            throw new FileIsNotAnImageException();
72
        }
73
74 4
        if (!array_key_exists($type, self::ALLOWED_FILE_TYPES)) {
75 1
            throw new ImageTypeNotAllowedException(
76 1
                sprintf('Allowed: %s', implode(', ', self::ALLOWED_FILE_TYPES))
77
            );
78
        }
79 3
    }
80
81 3
    private static function validateImageSize(string $file)
82
    {
83 3
        list($width, $height) = getimagesize($file);
84
85 3
        if ($width < self::MINIMAL_WIDTH or $height < self::MINIMAL_HEIGHT) {
86 1
            throw new ImageIsToSmallException(
87 1
                sprintf(
88 1
                    'Minimal image dimensions not met. Required: \'%dx%d\' Provided: \'%dx%d\'',
89 1
                    self::MINIMAL_HEIGHT,
90 1
                    self::MINIMAL_WIDTH,
91 1
                    $height,
92 1
                    $width
93
                )
94
            );
95
        }
96 2
    }
97
}
98