Sort::getIcon()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
cc 4
eloc 7
c 2
b 0
f 2
nc 6
nop 1
dl 0
loc 11
rs 10
1
<?php
2
3
namespace Ka4ivan\ViewSortable\Support;
4
5
class Sort
6
{
7
    protected $sortUrl;
8
    protected $orderUrl;
9
10
    public function __construct()
11
    {
12
        if (is_null($this->sortUrl) || is_null($this->orderUrl)) {
0 ignored issues
show
introduced by
The condition is_null($this->orderUrl) is always false.
Loading history...
13
            $this->sortUrl = config('view-sortable.url.sort', 'sort');
0 ignored issues
show
Bug introduced by
The function config 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

13
            $this->sortUrl = /** @scrutinizer ignore-call */ config('view-sortable.url.sort', 'sort');
Loading history...
14
            $this->orderUrl = config('view-sortable.url.order', 'order');
15
        }
16
    }
17
18
    /**
19
     * @param string $sort
20
     * @param string|null $text
21
     * @param string|null $order
22
     * @param string $class
23
     * @return string
24
     */
25
    public function getSortLink(string $sort, string $text = null, string $order = null, string $class = 'lte-sort-link'): string
26
    {
27
        $order = $order ?: ((request()->{$this->orderUrl} === 'asc') ? 'desc' : 'asc');
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

27
        $order = $order ?: ((/** @scrutinizer ignore-call */ request()->{$this->orderUrl} === 'asc') ? 'desc' : 'asc');
Loading history...
28
29
        $url = $this->buildUrl([$this->sortUrl => $sort, $this->orderUrl => $order]);
30
        $image = $this->getIcon($sort);
31
        $text = $text ?: $sort;
32
33
        return "<a class='{$class}' href='{$url}' style='position: relative'>{$text} {$image}</a>";
34
    }
35
36
    /**
37
     * @param string $sort
38
     * @param string|null $order
39
     * @return string
40
     */
41
    public function getSortUrl(string $sort, string $order = null): string
42
    {
43
        $order = $order ?: ((request()->{$this->orderUrl} === 'asc') ? 'desc' : 'asc');
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

43
        $order = $order ?: ((/** @scrutinizer ignore-call */ request()->{$this->orderUrl} === 'asc') ? 'desc' : 'asc');
Loading history...
44
        $url = $this->buildUrl(['sort' => $sort, 'order' => $order]);
45
46
        return $url;
47
    }
48
49
    /**
50
     * @param array $queryString
51
     * @return string
52
     */
53
    protected function buildUrl(array $queryString): string
54
    {
55
        return url(request()->fullUrlWithQuery($queryString));
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

55
        return url(/** @scrutinizer ignore-call */ request()->fullUrlWithQuery($queryString));
Loading history...
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

55
        return /** @scrutinizer ignore-call */ url(request()->fullUrlWithQuery($queryString));
Loading history...
56
    }
57
58
    /**
59
     * @param string $sort
60
     * @return string
61
     */
62
    protected function getIcon(string $sort): string
63
    {
64
        if (request()->{$this->sortUrl} === $sort) {
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

64
        if (/** @scrutinizer ignore-call */ request()->{$this->sortUrl} === $sort) {
Loading history...
65
            $icon = request()->{$this->orderUrl} === 'asc'
66
                ? config('view-sortable.icons.asc', 'fas fa-long-arrow-alt-up')
0 ignored issues
show
Bug introduced by
The function config 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

66
                ? /** @scrutinizer ignore-call */ config('view-sortable.icons.asc', 'fas fa-long-arrow-alt-up')
Loading history...
67
                : config('view-sortable.icons.desc', 'fas fa-long-arrow-alt-down');
68
        } else {
69
            $icon = config('view-sortable.icons.default', '');
70
        }
71
72
        return $icon ? "<i class='{$icon}' style='position: absolute; top: 3px; right: -10px'></i>" : '';
73
    }
74
}
75