Completed
Pull Request — master (#14)
by
unknown
15:13 queued 07:44
created

Image::hasCropped()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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