Passed
Push — master ( 64a7f1...87b8d3 )
by Vasyl
02:04
created

UrlAlias::route()   C

Complexity

Conditions 12
Paths 18

Size

Total Lines 34
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
cc 12
eloc 20
c 3
b 1
f 1
nc 18
nop 4
dl 0
loc 34
rs 6.9666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: fomvasss
5
 * Date: 11.02.19
6
 * Time: 21:51
7
 */
8
9
namespace Fomvasss\UrlAliases;
10
11
class UrlAlias
12
{
13
    protected $app;
14
15
    protected $config;
16
    
17
    /**
18
     * UrlAlias constructor.
19
     *
20
     * @param $app
21
     * @throws \Exception
22
     */
23
    public function __construct($app = null)
24
    {
25
        if (!$app) {
26
            $app = app();   //Fallback when $app is not given
0 ignored issues
show
Bug introduced by
The function app was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

26
            $app = /** @scrutinizer ignore-call */ app();   //Fallback when $app is not given
Loading history...
27
        }
28
        $this->app = $app;
29
30
        $this->config = $this->app['config'];
31
    }
32
    
33
    public function route(string $systemName, $parameters = [], $absolute = true, $forceWithLocalePrefix = false): string
34
    {
35
        $parameters = array_wrap($parameters);
36
37
        if (! empty($parameters[0]) && ($parameters[0] instanceof \Illuminate\Database\Eloquent\Model)) {
38
            $entity = $parameters[0];
39
            if ($entity->urlAlias) {
40
                unset($parameters[0]);
41
                if (! $this->config->get('url-aliases.use_localization')) {
42
                	$alias = $entity->urlAlias->alias;
43
                } elseif ($this->config->get('url-aliases-laravellocalization.origin_default_locale') == $entity->urlAlias->locale && $this->config->get('url-aliases-laravellocalization.hideDefaultLocaleInURL') && !$forceWithLocalePrefix) {
44
                    $alias = $entity->urlAlias->alias;
45
                } else {
46
                    $alias = $entity->urlAlias->localeAlias;
47
                }
48
49
                if (count($parameters)) {
50
                    $alias .= '?' . http_build_query($parameters);
51
                }
52
53
                return $absolute ? url($alias) : $alias;
0 ignored issues
show
Bug introduced by
The function url was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

53
                return $absolute ? /** @scrutinizer ignore-call */ url($alias) : $alias;
Loading history...
54
            }
55
        }
56
57
        if (!empty($this->config['url-aliases']['use_localization'])) {
58
            $relativePath = trim(route($systemName, $parameters, false), '/');
0 ignored issues
show
Bug introduced by
The function route was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

58
            $relativePath = trim(/** @scrutinizer ignore-call */ route($systemName, $parameters, false), '/');
Loading history...
59
60
            if ($absolute) {
61
                return url($this->config['app']['locale'] . '/' . $relativePath);
62
            }
63
            return $relativePath;
64
        }
65
66
        return route($systemName, $parameters, $absolute);
67
    }
68
69
    public function current($absolute = true): string
70
    {
71
        $path = request()->server('ALIAS_REQUEST_URI', request()->path());
0 ignored issues
show
Bug introduced by
The function request was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

71
        $path = /** @scrutinizer ignore-call */ request()->server('ALIAS_REQUEST_URI', request()->path());
Loading history...
72
73
        return $absolute ? url($path) : $path;
0 ignored issues
show
Bug introduced by
The function url was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

73
        return $absolute ? /** @scrutinizer ignore-call */ url($path) : $path;
Loading history...
74
    }
75
}