Conversion   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 28
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A transform() 0 4 2
A set() 0 11 3
A get() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Jonasdamher\Simplifyimage\Utils;
6
7
use Jonasdamher\Simplifyimage\Core\ResponseHandler;
8
9
class Conversion
10
{
11
	private string $conversionTo = 'default';
12
	private array $allowFormatConversion = ['webp','png','jpeg'];
13
	
14
	public function get(): string
15
	{
16
		return $this->conversionTo;
17
	}
18
19
	public function set(string $conversionTo)
20
	{
21
		try {
22
			if (!in_array($conversionTo, $this->allowFormatConversion, true)) {
23
				throw new \Exception("Don't exist format to conversion (" . $conversionTo . ')');
24
			}
25
		} catch (\Exception $e) {
26
			$conversionTo = 'png';
27
			ResponseHandler::fail($e->getMessage());
28
		} finally {
29
			$this->conversionTo = $conversionTo;
30
		}
31
	}
32
33
	public function transform($imagecreatefrom, string $transformImage, array $imageArray)
34
	{
35
		return ($this->get() != 'default') ?
36
			('image' . $this->get())($imagecreatefrom, $imageArray['tmp_name']) : ($transformImage)($imagecreatefrom, $imageArray['tmp_name']);
37
	}
38
}
39