Test Failed
Pull Request — master (#29)
by
unknown
02:05
created

ImageFileUpload::byteArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.9
c 0
b 0
f 0
cc 2
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 = $this->byteArray($filename);
35 2
    }
36
37
    /**
38
     *  Generate byte array similar to the one in C# and Java
39
     */
40 2
    public function byteArray(string $filename): array
41
    {
42 2
        $data   = file_get_contents($filename);
43 2
        $array  = array();
44 2
        foreach(str_split($data) as $char)
45
        {
46 2
            array_push($array, ord($char));
47
        }
48
49 2
        return $array;
50
    }
51
52 1
    public function getPostBodyData(): string
53
    {
54
        $data = [
55 1
            'imageFormat' => $this->imageFormat,
56 1
            'imageData' => $this->imageData,
57
        ];
58
59 1
        return json_encode($data);
60
    }
61
62 6
    private function validateFile(string $file): void
63
    {
64 6
        self::validateFileSize($file);
65 5
        self::validatedImageType($file);
66 3
        self::validateImageSize($file);
67 2
    }
68
69 6
    private static function validateFileSize($file): void
70
    {
71 6
        $maxFileSizeBytes = (self::MAX_FILE_SIZE_MB * 1024 * 1024);
72 6
        if (filesize($file) > $maxFileSizeBytes) {
73 1
            throw new MaximumImageFileSizeExcededException(sprintf('Max file size is \'%d Mb\'', self::MAX_FILE_SIZE_MB));
74
        }
75 5
    }
76
77 5
    private static function validatedImageType(string $file): void
78
    {
79 5
        $type = false;
80
81
        try {
82 5
            $type = exif_imagetype($file);
83 1
        } catch (Exception $e) {
84
            //-- $type is already false
85
        }
86
87 5
        if ($type === false) {
88 1
            throw new FileIsNotAnImageException();
89
        }
90
91 4
        if (!array_key_exists($type, self::ALLOWED_FILE_TYPES)) {
92 1
            throw new ImageTypeNotAllowedException(
93 1
                sprintf('Allowed: %s', implode(', ', self::ALLOWED_FILE_TYPES))
94
            );
95
        }
96 3
    }
97
98 3
    private static function validateImageSize(string $file): void
99
    {
100 3
        list($width, $height) = getimagesize($file);
101
102 3
        if ($width < self::MINIMAL_WIDTH or $height < self::MINIMAL_HEIGHT) {
103 1
            throw new ImageIsToSmallException(
104 1
                sprintf(
105 1
                    'Minimal image dimensions not met. Required: \'%dx%d\' Provided: \'%dx%d\'',
106 1
                    self::MINIMAL_HEIGHT,
107 1
                    self::MINIMAL_WIDTH,
108 1
                    $height,
109 1
                    $width
110
                )
111
            );
112
        }
113 2
    }
114
}
115