| 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 | 1 |  |     public function getPostBodyData(): string | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |         $data = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 40 | 1 |  |             'imageFormat' => $this->imageFormat, | 
            
                                                                        
                            
            
                                    
            
            
                | 41 | 1 |  |             'imageData' => $this->imageData, | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 1 |  |         return json_encode($data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 6 |  |     private function validateFile(string $file) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 6 |  |         self::validateFileSize($file); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 5 |  |         self::validatedImageType($file); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 3 |  |         self::validateImageSize($file); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 2 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 6 |  |     private static function validateFileSize($file) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 6 |  |         $maxFileSizeBytes = (self::MAX_FILE_SIZE_MB * 1024 * 1024); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 6 |  |         if (filesize($file) > $maxFileSizeBytes) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 1 |  |             throw new MaximumImageFileSizeExcededException(sprintf('Max file size is \'%d Mb\'', self::MAX_FILE_SIZE_MB)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 5 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 5 |  |     private static function validatedImageType(string $file) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 5 |  |         $type = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 5 |  |             $type = exif_imagetype($file); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 1 |  |         } catch (Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             //-- $type is already false | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 5 |  |         if ($type === false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 1 |  |             throw new FileIsNotAnImageException(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 4 |  |         if (!array_key_exists($type, self::ALLOWED_FILE_TYPES)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 1 |  |             throw new ImageTypeNotAllowedException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 1 |  |                 sprintf('Allowed: %s', implode(', ', self::ALLOWED_FILE_TYPES)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 3 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 3 |  |     private static function validateImageSize(string $file) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 3 |  |         list($width, $height) = getimagesize($file); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 3 |  |         if ($width < self::MINIMAL_WIDTH or $height < self::MINIMAL_HEIGHT) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 1 |  |             throw new ImageIsToSmallException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 1 |  |                 sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 1 |  |                     'Minimal image dimensions not met. Required: \'%dx%d\' Provided: \'%dx%d\'', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 1 |  |                     self::MINIMAL_HEIGHT, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 1 |  |                     self::MINIMAL_WIDTH, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 1 |  |                     $height, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 1 |  |                     $width | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                 ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 2 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 99 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 100 |  |  |  |