Completed
Push — master ( 1a22b8...361f69 )
by Ryan
07:46
created

AssetPaths   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 178
Duplicated Lines 12.92 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 18
c 2
b 1
f 0
lcom 1
cbo 3
dl 23
loc 178
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A addPath() 0 6 1
A hint() 0 12 3
A extension() 0 4 1
A realPath() 15 15 3
A downloadPath() 0 8 3
A __construct() 8 8 1
B outputPath() 0 32 5
A prefix() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php namespace Anomaly\Streams\Platform\Asset;
2
3
use Anomaly\Streams\Platform\Application\Application;
4
use Illuminate\Contracts\Config\Repository;
5
use Illuminate\Http\Request;
6
7
/**
8
 * Class AssetPaths
9
 *
10
 * @link          http://anomaly.is/streams-platform
11
 * @author        AnomalyLabs, Inc. <[email protected]>
12
 * @author        Ryan Thompson <[email protected]>
13
 * @package       Anomaly\Streams\Platform\Asset
14
 */
15
class AssetPaths
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 AssetPaths instance.
48
     *
49
     * @param Repository $config
50
     * @param Request    $request
51
     */
52 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...
53
    {
54
        $this->config      = $config;
55
        $this->request     = $request;
56
        $this->application = $application;
57
58
        $this->paths = $config->get('streams::assets.paths', []);
0 ignored issues
show
Documentation Bug introduced by
It seems like $config->get('streams::assets.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...
59
    }
60
61
    /**
62
     * Add an asset path hint.
63
     *
64
     * @param $namespace
65
     * @param $path
66
     * @return $this
67
     */
68
    public function addPath($namespace, $path)
69
    {
70
        $this->paths[$namespace] = $path;
71
72
        return $this;
73
    }
74
75
    /**
76
     * Return the hinted extension.
77
     *
78
     * @param $path
79
     * @return string
80
     */
81
    public function hint($path)
82
    {
83
        $hint = $this->extension($path);
84
85
        foreach ($this->config->get('streams::assets.hints', []) as $extension => $hints) {
86
            if (in_array($hint, $hints)) {
87
                return $extension;
88
            }
89
        }
90
91
        return $hint;
92
    }
93
94
    /**
95
     * Return the extension of the path.
96
     *
97
     * @param $path
98
     * @return string
99
     */
100
    public function extension($path)
101
    {
102
        return pathinfo($path, PATHINFO_EXTENSION);
103
    }
104
105
    /**
106
     * Return the real path for a given path.
107
     *
108
     * @param $path
109
     * @return string
110
     * @throws \Exception
111
     */
112 View Code Duplication
    public function realPath($path)
0 ignored issues
show
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...
113
    {
114
        if (str_contains($path, '::')) {
115
116
            list($namespace, $path) = explode('::', $path);
117
118
            if (!isset($this->paths[$namespace])) {
119
                throw new \Exception("Path hint [{$namespace}::{$path}] does not exist!");
120
            }
121
122
            return rtrim($this->paths[$namespace], '/') . '/' . $path;
123
        }
124
125
        return $path;
126
    }
127
128
    /**
129
     * Return the download path for a remote asset.
130
     *
131
     * @param      $url
132
     * @param null $path
133
     * @return string
134
     */
135
    public function downloadPath($url, $path = null)
136
    {
137
        if (!$path && $parsed = parse_url($url)) {
138
            $path = array_get($parsed, 'host') . '/' . basename(array_get($parsed, 'path'));
139
        }
140
141
        return $path = $this->outputPath('downloads/' . $path);
0 ignored issues
show
Unused Code introduced by
$path 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...
142
    }
143
144
    /**
145
     * Return the output path.
146
     *
147
     * @param $collection
148
     * @return string
149
     */
150
    public function outputPath($collection)
151
    {
152
        /**
153
         * If the path is already public
154
         * then just use it as it is.
155
         */
156
        if (str_contains($collection, public_path())) {
157
            return str_replace(public_path(), '', $collection);
158
        }
159
160
        /**
161
         * Get the real path relative to our installation.
162
         */
163
        $path = str_replace(base_path(), '', $this->realPath($collection));
164
165
        /**
166
         * Build out path parts.
167
         */
168
        $directory   = ltrim(dirname($path), '/\\') . '/';
169
        $application = $this->application->getReference();
170
        $filename    = basename($path, $this->extension($path)) . $this->hint($path);
171
172
        if (starts_with($directory, 'vendor/')) {
173
            $directory = substr($directory, 7);
174
        }
175
176
        if (starts_with($directory, './')) {
177
            $directory = in_array($this->request->segment(1), ['admin', 'installer']) ? 'admin/' : 'public/';
178
        }
179
180
        return "/app/{$application}/assets/{$directory}{$filename}";
181
    }
182
183
    /**
184
     * Return the path prefix.
185
     *
186
     * @return string
187
     */
188
    public function prefix()
189
    {
190
        return rtrim(array_get(parse_url($this->request->root()), 'path'), '/');
0 ignored issues
show
Security Bug introduced by
It seems like parse_url($this->request->root()) targeting parse_url() can also be of type false; however, array_get() does only seem to accept array, did you maybe forget to handle an error condition?
Loading history...
191
    }
192
}
193