Completed
Branch master (afd3db)
by Yaro
01:50
created

Image::cropWidth()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Yaro\Jarboe\Pack;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\Support\Facades\Storage;
7
8
class Image
9
{
10
    private $data;
11
12 2
    public function __construct($data)
13
    {
14 2
        $this->data = is_array($data) ? $data : [];
15 2
    }
16
17 2
    public function exist(): bool
18
    {
19 2
        return (bool) $this->originalSource();
20
    }
21
22 2
    public function hasCropped(): bool
23
    {
24 2
        return (bool) $this->croppedSource();
25
    }
26
27 2
    public function croppedOrOriginalSourceUrl($default = null)
28
    {
29 2
        $source = Arr::get($this->data, 'sources.cropped') ?: $this->originalSource();
30 2
        if (!$source) {
31 1
            return $default;
32
        }
33
34 1
        if ($this->isEncoded()) {
35 1
            return $source;
36
        }
37
38 1
        return Storage::disk($this->getDisk())->url($source);
39
    }
40
41 2 View Code Duplication
    public function originalSourceUrl($default = null)
42
    {
43 2
        $source = $this->originalSource();
44 2
        $sourceUrl = $source;
45 2
        if (!$this->isEncoded()) {
46 2
            $sourceUrl = Storage::disk($this->getDisk())->url($source);
47
        }
48
49 2
        return $source ? $sourceUrl : $default;
50
    }
51
52 2
    public function originalSource($default = null)
53
    {
54 2
        return Arr::get($this->data, 'sources.original') ?: $default;
55
    }
56
57 2 View Code Duplication
    public function croppedSourceUrl($default = null)
58
    {
59 2
        $source = $this->croppedSource();
60 2
        $sourceUrl = $source;
61 2
        if (!$this->isEncoded()) {
62 2
            $sourceUrl = Storage::disk($this->getDisk())->url($source);
63
        }
64
65 2
        return $source ? $sourceUrl : $default;
66
    }
67
68 2
    public function croppedSource($default = null)
69
    {
70 2
        return Arr::get($this->data, 'sources.cropped') ?: $default;
71
    }
72
73 2
    public function isEncoded(): bool
74
    {
75
        // booleans passed as string on repeater item render, so (bool) true becomes (string) "true"
76 2
        $isEncode = Arr::get($this->data, 'storage.is_encoded', false);
77 2
        if (is_string($isEncode)) {
78
            $isEncode = $isEncode === 'true' ? true : false;
79
        }
80
81 2
        return (bool) $isEncode;
82
    }
83
84 2
    public function getDisk()
85
    {
86 2
        return Arr::get($this->data, 'storage.disk');
87
    }
88
89 2
    public function cropWidth()
90
    {
91 2
        return Arr::get($this->data, 'crop.width');
92
    }
93
94 2
    public function cropHeight()
95
    {
96 2
        return Arr::get($this->data, 'crop.height');
97
    }
98
99 2
    public function cropX()
100
    {
101 2
        return Arr::get($this->data, 'crop.x');
102
    }
103
104 2
    public function cropY()
105
    {
106 2
        return Arr::get($this->data, 'crop.y');
107
    }
108
109 2
    public function cropRotate()
110
    {
111 2
        return Arr::get($this->data, 'crop.rotate');
112
    }
113
114 2
    public function cropRotateBackground()
115
    {
116 2
        return Arr::get($this->data, 'crop.rotate_background');
117
    }
118
}
119