Url::link()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
namespace System;
4
5
class Url
6
{
7
    /**
8
     * Protocol
9
     *
10
     * @var string
11
     */
12
    private $protocol;
13
14
    /**
15
     * Host
16
     *
17
     * @var string
18
     */
19
    private $host;
20
21
    /**
22
     * Parameters
23
     *
24
     * @var string
25
     */
26
    private $parameters;
27
28
    /**
29
     * Base Url
30
     *
31
     * @var string
32
     */
33
    private $baseUrl;
34
35
    /**
36
     * Application Object
37
     *
38
     * @var \System\Application
39
     */
40
    private $app;
41
42
    /**
43
     * Constructor
44
     *
45
     * @param \System\Application $app
46
     */
47
    public function __construct(Application $app)
48
    {
49
        $this->app = $app;
50
51
        $this->prepare();
52
    }
53
54
    /**
55
     * Prepare url
56
     *
57
     * @property object $request
58
     * @property object $http
59
     * @return void
60
     */
61
    private function prepare()
62
    {
63
        $script = dirname($this->app->request->server('SCRIPT_NAME'));
0 ignored issues
show
Documentation introduced by
The property request does not exist on object<System\Application>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
64
65
        $requestUri = $this->app->request->server('REQUEST_URI');
0 ignored issues
show
Documentation introduced by
The property request does not exist on object<System\Application>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
66
67
        if (strpos($requestUri, '?')) {
68
            list($requestUri) = explode('?', $requestUri);
69
        }
70
71
        $this->protocol = $this->app->http->requestProtocol();
0 ignored issues
show
Documentation introduced by
The property http does not exist on object<System\Application>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
72
73
        $this->rootDomain = $this->app->request->server('HTTP_HOST');
0 ignored issues
show
Bug introduced by
The property rootDomain does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Documentation introduced by
The property request does not exist on object<System\Application>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
74
75
        $this->host = $this->protocol . '://' . $this->rootDomain;
76
77
        $this->parameters = cleanUrl($script, $requestUri);
78
79
        $this->baseUrl = $this->host . $this->parameters;
80
    }
81
82
    /**
83
     * Get Protocol
84
     *
85
     * @return string
86
     */
87
    public function protocol()
88
    {
89
        return $this->protocol;
90
    }
91
92
    /**
93
     * Get Root domain
94
     *
95
     * @return string
96
     */
97
    public function rootDomain()
98
    {
99
        return $this->rootDomain;
100
    }
101
102
    /**
103
     * Get parameters
104
     *
105
     * @return string
106
     */
107
    public function parameters()
108
    {
109
        return $this->parameters;
110
    }
111
112
    /**
113
     * Get only host
114
     *
115
     * @return string
116
     */
117
    public function host()
118
    {
119
        return $this->host;
120
    }
121
122
    /**
123
     * Get full url of the script
124
     *
125
     * @return string
126
     */
127
    public function baseUrl()
128
    {
129
        return $this->baseUrl;
130
    }
131
132
    /**
133
     * Generate full link for the given path
134
     *
135
     * @param string $path
136
     * @return string
137
     */
138
    public function link(string $path)
139
    {
140
        $link = $this->host();
141
142
        $path = rtrim($path, '/');
143
        $path = ltrim($path, '/');
144
145
        return $link . '/' . $path;
146
    }
147
148
    /**
149
     * Redirect to the given path
150
     *
151
     * @param string $path
152
     * @param int $num
153
     * @return void
154
     */
155
    public function redirectTo(string $path, int $num = 0)
156
    {
157
        header('Refresh: ' . $num . '; URL=' . $this->link($path));
158
        exit;
159
    }
160
161
    /**
162
     * Redirect to the previous page
163
     *
164
     * @return void
165
     */
166
    public function redirectToPreviousPage()
167
    {
168
        header('Refresh: ' . $this->request->referer());
0 ignored issues
show
Bug introduced by
The property request does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
169
        exit;
170
    }
171
172
    /**
173
     * Redirect to the 404 page
174
     *
175
     * @property object $request
176
     * @property object $load
177
     * @param string $path
178
     * @return void
179
     */
180
    public function notfound(string $path = '')
181
    {
182
        if (!$path) {
183
            $path = '/404';
184
185
            if ($this->app->request->isRequestToAdminManagement() && $this->app->load->model('User')->isAdmin()) {
0 ignored issues
show
Documentation introduced by
The property request does not exist on object<System\Application>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation introduced by
The property load does not exist on object<System\Application>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
186
                $path = 'admin/404';
187
            }
188
        }
189
        header('Refresh: 0; URL=' . $this->link($path));
190
        exit;
191
    }
192
}
193