Guided   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 0
Metric Value
wmc 13
lcom 2
cbo 5
dl 0
loc 126
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A bootGuided() 0 8 2
A getRoutedUrl() 0 14 2
A isSafeForDelete() 0 4 1
A routeResized() 0 4 1
A routeThumbnail() 0 4 1
A getClassName() 0 4 1
A getUrl() 0 14 2
A getTitle() 0 4 1
A getFullPath() 0 4 1
A getName() 0 4 1
1
<?php
2
3
/**
4
 * @noinspection PhpUnusedParameterInspection
5
 * @noinspection PhpUnused
6
 */
7
8
declare(strict_types=1);
9
10
namespace ReliqArts\GuidedImage\Concern;
11
12
use Illuminate\Support\Facades\Storage;
13
use Illuminate\Support\Str;
14
use ReliqArts\GuidedImage\Contract\ConfigProvider;
15
use ReliqArts\GuidedImage\Contract\GuidedImage as GuidedImageContract;
16
use ReliqArts\GuidedImage\Demand\Resize;
17
use ReliqArts\GuidedImage\Demand\Thumbnail;
18
use ReliqArts\GuidedImage\Exception\BadImplementation;
19
20
/**
21
 * Trait Guided.
22
 *
23
 * @property mixed $id
24
 * @property string name
25
 * @property string full_path
26
 */
27
trait Guided
28
{
29
    use GuidedRepository;
30
31
    /**
32
     * @throws BadImplementation
33
     */
34
    public static function bootGuided(): void
35
    {
36
        $implementedInterfaces = class_implements(static::class);
37
38
        if (!in_array(GuidedImageContract::class, $implementedInterfaces, true)) {
39
            throw new BadImplementation(sprintf('Model (%s) must implement `%s` to be guided!', static::class, GuidedImageContract::class));
40
        }
41
    }
42
43
    /**
44
     * Get resized/thumbnail photo link.
45
     *
46
     * @param string $type   request type (thumbnail or resize)
47
     * @param array  $params parameters to pass to route
48
     */
49
    public function getRoutedUrl(string $type, array $params = []): string
50
    {
51
        if (empty($params)) {
52
            return $this->getUrl();
53
        }
54
55
        /** @var ConfigProvider $configProvider */
56
        $configProvider = resolve(ConfigProvider::class);
57
        $guidedModelName = $configProvider->getGuidedModelName(true);
58
59
        array_unshift($params, $this->id);
60
61
        return route(sprintf('%s.%s', $guidedModelName, $type), $params);
62
    }
63
64
    /**
65
     * Whether image is safe for deleting.
66
     * Since a single image may be re-used this method is used to determine
67
     * when an image can be safely deleted from disk.
68
     *
69
     * @param int $safeAmount a photo is safe to delete if it is used by $safe_num amount of records
70
     *
71
     * @return bool whether image is safe for delete
72
     */
73
    public function isSafeForDelete(int $safeAmount = 1): bool
74
    {
75
        return $safeAmount === 1;
76
    }
77
78
    /**
79
     * Get link to resized photo.
80
     *
81
     * @param array $params parameters to pass to route
82
     */
83
    public function routeResized(array $params = []): string
84
    {
85
        return $this->getRoutedUrl(Resize::ROUTE_TYPE_NAME, $params);
86
    }
87
88
    /**
89
     * Get link to photo thumbnail.
90
     *
91
     * @param array $params parameters to pass to route
92
     */
93
    public function routeThumbnail(array $params = []): string
94
    {
95
        return $this->getRoutedUrl(Thumbnail::ROUTE_TYPE_NAME, $params);
96
    }
97
98
    /**
99
     * Get class.
100
     */
101
    public function getClassName(): string
102
    {
103
        return get_class($this);
104
    }
105
106
    /**
107
     *  Get URL/path to image.
108
     *
109
     * @param bool $diskRelative whether to return `full path` (relative to disk),
110
     *                           hence skipping call to Storage facade
111
     *
112
     * @uses \Illuminate\Support\Facades\Storage
113
     */
114
    public function getUrl(bool $diskRelative = false): string
115
    {
116
        $path = urldecode($this->getFullPath());
117
118
        if ($diskRelative) {
119
            return $path;
120
        }
121
122
        /** @var ConfigProvider $configProvider */
123
        $configProvider = resolve(ConfigProvider::class);
124
        $diskName = $configProvider->getUploadDiskName();
125
126
        return Storage::disk($diskName)->url($path);
127
    }
128
129
    /**
130
     *  Get ready image title.
131
     */
132
    public function getTitle(): string
133
    {
134
        return Str::title(preg_replace('/[\\-_]/', ' ', $this->getName()));
135
    }
136
137
    /**
138
     * Get full path.
139
     */
140
    public function getFullPath(): string
141
    {
142
        return $this->full_path ?? '';
143
    }
144
145
    /**
146
     * Get name.
147
     */
148
    public function getName(): string
149
    {
150
        return $this->name ?? '';
151
    }
152
}
153