Completed
Push — master ( 5b54fc...16c96d )
by Bertrand
14:42
created

Picture::resize()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 15
nc 6
nop 1
dl 0
loc 22
rs 9.7666
c 0
b 0
f 0
1
<?php
2
3
4
namespace App\Src\UseCases\Domain\Shared\Model;
5
6
7
8
use App\Src\UseCases\Domain\Shared\Gateway\PictureHandler;
9
10
class Picture
11
{
12
    private $path;
13
    private $relativePath;
14
    private $ext;
15
16
    public function __construct(string $path, string $ext = null)
17
    {
18
        $this->path = $path;
19
        $this->ext = $ext;
20
    }
21
22
    public function resize(string $newPath)
23
    {
24
        $relativePath = $newPath;
25
        $finalNewPath = storage_path().'/'.$newPath;
26
        if(isset($this->ext)){
27
            $finalNewPath = storage_path().'/'.$newPath.'.'.$this->ext;
28
            $relativePath = $newPath.'.'.$this->ext;
29
        }
30
        if(app(PictureHandler::class)->width($this->path) > 600) {
31
            app(PictureHandler::class)->widen($this->path, $finalNewPath, 600);
32
            $this->relativePath = $relativePath;
33
            return;
34
        }
35
36
        if(app(PictureHandler::class)->height($this->path) > 400) {
37
            app(PictureHandler::class)->heighten($this->path, $finalNewPath, 400);
38
            $this->relativePath = $relativePath;
39
            return;
40
        }
41
42
        app(PictureHandler::class)->write($this->path, $finalNewPath);
43
        $this->relativePath = $relativePath;
44
    }
45
46
    public function relativePath(): string
47
    {
48
        return $this->relativePath;
49
    }
50
}
51