Passed
Push — master ( 5471a5...321c34 )
by Radu
01:44
created

RequestUrlTrait::getQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
namespace WebServCo\Framework\Traits;
3
4
trait RequestUrlTrait
5
{
6
    public function getSuffix()
7
    {
8
        return $this->suffix;
0 ignored issues
show
Bug Best Practice introduced by
The property suffix does not exist on WebServCo\Framework\Traits\RequestUrlTrait. Did you maybe forget to declare it?
Loading history...
9
    }
10
    
11
    public function getTarget()
12
    {
13
        return $this->target;
0 ignored issues
show
Bug Best Practice introduced by
The property target does not exist on WebServCo\Framework\Traits\RequestUrlTrait. Did you maybe forget to declare it?
Loading history...
14
    }
15
    
16
    public function getAppUrl()
17
    {
18
        if (\WebServCo\Framework\Framework::isCLI()) {
19
            return false;
20
        }
21
        return $this->getSchema() .
0 ignored issues
show
Bug introduced by
It seems like getSchema() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

21
        return $this->/** @scrutinizer ignore-call */ getSchema() .
Loading history...
22
        '://' .
23
        $this->getHost() .
0 ignored issues
show
Bug introduced by
It seems like getHost() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

23
        $this->/** @scrutinizer ignore-call */ 
24
               getHost() .
Loading history...
24
        $this->path .
0 ignored issues
show
Bug Best Practice introduced by
The property path does not exist on WebServCo\Framework\Traits\RequestUrlTrait. Did you maybe forget to declare it?
Loading history...
25
        DIRECTORY_SEPARATOR;
26
    }
27
    
28
    public function getShortUrl()
29
    {
30
        $url = $this->getAppUrl();
31
        $url .= $this->getTarget();
32
        $url .= $this->getSuffix();
33
        return $url;
34
    }
35
    
36
    public function getUrl($removeParameters = [])
37
    {
38
        $url = $this->getShortUrl();
39
        $query = $this->getQuery();
40
        foreach ($removeParameters as $item) {
41
            if (array_key_exists($item, $query)) {
42
                unset($query[$item]);
43
            }
44
        }
45
        $url .= $this->queryToString($query);
46
        return $url;
47
    }
48
    
49
    public function getQuery()
50
    {
51
        return $this->query;
0 ignored issues
show
Bug Best Practice introduced by
The property query does not exist on WebServCo\Framework\Traits\RequestUrlTrait. Did you maybe forget to declare it?
Loading history...
52
    }
53
    
54
    public function query($key, $defaultValue = false)
55
    {
56
        return \WebServCo\Framework\ArrayStorage::get(
57
            $this->query,
0 ignored issues
show
Bug Best Practice introduced by
The property query does not exist on WebServCo\Framework\Traits\RequestUrlTrait. Did you maybe forget to declare it?
Loading history...
58
            $key,
59
            $defaultValue
60
        );
61
    }
62
    
63
    public function queryToString($query = [])
64
    {
65
        if (empty($query)) {
66
            return false;
67
        }
68
        $queries = [];
69
        foreach ($query as $k => $v) {
70
            $queries[] = sprintf('%s=%s', $k, $v);
71
        }
72
        return '?' . implode('&', $queries);
73
    }
74
}
75