ImagePaths::getPaths()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php namespace Anomaly\Streams\Platform\Image;
2
3
use Anomaly\FilesModule\File\Contract\FileInterface;
4
use Anomaly\Streams\Platform\Application\Application;
5
use Illuminate\Contracts\Config\Repository;
6
use Illuminate\Http\Request;
7
8
/**
9
 * Class ImagePaths
10
 *
11
 * @link   http://pyrocms.com/
12
 * @author PyroCMS, Inc. <[email protected]>
13
 * @author Ryan Thompson <[email protected]>
14
 */
15
class ImagePaths
16
{
17
18
    /**
19
     * Predefined paths.
20
     *
21
     * @var array
22
     */
23
    protected $paths = [];
24
25
    /**
26
     * The config repository.
27
     *
28
     * @var Repository
29
     */
30
    protected $config;
31
32
    /**
33
     * The request object.
34
     *
35
     * @var Request
36
     */
37
    protected $request;
38
39
    /**
40
     * The application object.
41
     *
42
     * @var Application
43
     */
44
    protected $application;
45
46
    /**
47
     * Create a new ImagePaths instance.
48
     *
49
     * @param Repository  $config
50
     * @param Request     $request
51
     * @param Application $application
52
     */
53 View Code Duplication
    public function __construct(Repository $config, Request $request, Application $application)
0 ignored issues
show
Bug introduced by
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
54
    {
55
        $this->config      = $config;
56
        $this->request     = $request;
57
        $this->application = $application;
58
59
        $this->paths = $config->get('streams::images.paths', []);
0 ignored issues
show
Documentation Bug introduced by
It seems like $config->get('streams::images.paths', array()) of type * is incompatible with the declared type array of property $paths.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
60
    }
61
62
    /**
63
     * Get the paths.
64
     *
65
     * @return array|mixed
66
     */
67
    public function getPaths()
68
    {
69
        return $this->paths;
70
    }
71
72
    /**
73
     * Set the paths.
74
     *
75
     * @param  array $paths
76
     * @return $this
77
     */
78
    public function setPaths(array $paths)
79
    {
80
        $this->paths = $paths;
81
82
        return $this;
83
    }
84
85
    /**
86
     * Add an image path hint.
87
     *
88
     * @param $namespace
89
     * @param $path
90
     * @return $this
91
     */
92
    public function addPath($namespace, $path)
93
    {
94
        $this->paths[$namespace] = rtrim($path, '/\\');
95
96
        return $this;
97
    }
98
99
    /**
100
     * Return the real path for a given path.
101
     *
102
     * @param $path
103
     * @return string
104
     * @throws \Exception
105
     */
106
    public function realPath($path)
107
    {
108
        if (str_contains($path, '::')) {
109
            list($namespace, $path) = explode('::', $path);
110
111
            if (!isset($this->paths[$namespace])) {
112
                throw new \Exception("Path hint [{$namespace}::{$path}] does not exist!");
113
            }
114
115
            return rtrim($this->paths[$namespace], '/') . '/' . $path;
116
        }
117
118
        return $path;
119
    }
120
121
    /**
122
     * Return the output path for an image.
123
     *
124
     * @param $path
125
     * @return string
126
     */
127
    public function outputPath(Image $image)
128
    {
129
        $path = $image->getImage();
130
131
        if ($path instanceof FileInterface) {
0 ignored issues
show
Bug introduced by
The class Anomaly\FilesModule\File\Contract\FileInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
132
            $path = $path->path();
133
        }
134
135
        /*
136
         * If the path is already public
137
         * then just use it as it is.
138
         */
139
        if (str_contains($path, public_path())) {
140
            return str_replace(public_path(), '', $path);
141
        }
142
143
        /*
144
         * If the path is a file or file path then
145
         * put it in /app/{$application}/files/disk/folder/filename.ext
146
         */
147
        if (is_string($path) && str_is('*://*', $path)) {
148
149
            $application = $this->application->getReference();
150
151
            list($disk, $folder, $filename) = explode('/', str_replace('://', '/', $path));
152
153 View Code Duplication
            if ($image->getAlterations() || $image->getQuality()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
154
                $filename = md5(
155
                        var_export([$path, $image->getAlterations()], true) . $image->getQuality()
156
                    ) . '.' . $image->getExtension();
157
            }
158
            
159
            if ($rename = $image->getFilename()) {
160
161
                $filename = $rename;
162
163
                if (strpos($filename, DIRECTORY_SEPARATOR)) {
164
                    $directory = null;
0 ignored issues
show
Unused Code introduced by
$directory is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
165
                }
166
            }
167
168
            return "/app/{$application}/files/{$disk}/{$folder}/{$filename}";
169
        }
170
171
        /*
172
         * Get the real path relative to our installation.
173
         */
174
        $path = str_replace(base_path(), '', $this->realPath($path));
175
176
        /*
177
         * Build out path parts.
178
         */
179
        $filename    = basename($path);
180
        $directory   = ltrim(dirname($path), '/\\') . '/';
181
        $application = $this->application->getReference();
182
183 View Code Duplication
        if ($image->getAlterations() || $image->getQuality()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
184
            $filename = md5(
185
                    var_export([$path, $image->getAlterations()], true) . $image->getQuality()
186
                ) . '.' . $image->getExtension();
187
        }
188
189
        if ($rename = $image->getFilename()) {
190
            $directory = null;
191
            $filename  = ltrim($rename, '/\\');
192
        }
193
194
        return "/app/{$application}/assets/{$directory}{$filename}";
195
    }
196
}
197