Passed
Push — master ( bb924a...090072 )
by Vasyl
01:56
created

UrlAlias::route()   B

Complexity

Conditions 9
Paths 10

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 13
c 0
b 0
f 0
nc 10
nop 4
dl 0
loc 24
rs 8.0555
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, $forceWithLocalePreffix = 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
42
                if (! $forceWithLocalePreffix && $this->config->get('app.locale') == $entity->urlAlias->locale && $this->config->get('url-aliases-laravellocalization.hideDefaultLocaleInURL')) {
43
                    $alias = $entity->urlAlias->alias;
44
                } else {
45
                    $alias = $entity->urlAlias->localeAlias;
46
                }
47
48
                if (count($parameters)) {
49
                    $alias .= '?' . http_build_query($parameters);
50
                }
51
52
                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

52
                return $absolute ? /** @scrutinizer ignore-call */ url($alias) : $alias;
Loading history...
53
            }
54
        }
55
56
        return route($systemName, $parameters, $absolute);
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

56
        return /** @scrutinizer ignore-call */ route($systemName, $parameters, $absolute);
Loading history...
57
    }
58
59
    public function current($absolute = true): string
60
    {
61
        $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

61
        $path = /** @scrutinizer ignore-call */ request()->server('ALIAS_REQUEST_URI', request()->path());
Loading history...
62
63
        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

63
        return $absolute ? /** @scrutinizer ignore-call */ url($path) : $path;
Loading history...
64
    }
65
}