Test Failed
Pull Request — master (#29)
by
unknown
07:24
created

ImageFileUpload   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 91.3%

Importance

Changes 0
Metric Value
wmc 14
lcom 2
cbo 4
dl 0
loc 102
ccs 42
cts 46
cp 0.913
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A byteArray() 0 11 2
A getPostBodyData() 0 9 1
A validateFile() 0 6 1
A validateFileSize() 0 7 2
A validatedImageType() 0 20 4
A validateImageSize() 0 16 3
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
    public function getPostBodyData(): string
53
    {
54
        $data = [
55
            'imageFormat' => $this->imageFormat,
56
            'imageData' => $this->imageData,
57
        ];
58
59
        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