Completed
Push — 2.4 ( c6e444...d8c2c9 )
by James
97:33 queued 36:20
created

helpers.php ➔ cachet_route_generator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 4
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of Cachet.
5
 *
6
 * (c) Alt Three Services Limited
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
use CachetHQ\Cachet\Settings\Repository;
13
use Illuminate\Contracts\Bus\Dispatcher;
14
use Illuminate\Support\Facades\Config;
15
use Illuminate\Support\Facades\Request;
0 ignored issues
show
Bug introduced by Joseph Cohen
This use statement conflicts with another class in this namespace, Request.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
16
use Jenssegers\Date\Date;
17
18
if (!function_exists('setting')) {
19
    /**
20
     * Get a setting, or the default value.
21
     *
22
     * @param string $name
23
     * @param mixed  $default
24
     *
25
     * @return mixed
26
     */
27
    function setting($name, $default = null)
28
    {
29 1
        static $settings = [];
30
31 1
        if (isset($settings[$name])) {
32 1
            return $settings[$name];
33
        }
34
35 1
        return $settings[$name] = app(Repository::class)->get($name, $default);
36
    }
37
}
38
39
if (!function_exists('set_active')) {
40
    /**
41
     * Set active class if request is in path.
42
     *
43
     * @param string $path
44
     * @param array  $classes
45
     * @param string $active
46
     *
47
     * @return string
48
     */
49
    function set_active($path, array $classes = [], $active = 'active')
0 ignored issues
show
Best Practice introduced by Joseph Cohen
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
50
    {
51
        if (Request::is($path)) {
52
            $classes[] = $active;
53
        }
54
55
        $class = e(implode(' ', $classes));
56
57
        return empty($classes) ? '' : "class=\"{$class}\"";
58
    }
59
}
60
61
if (!function_exists('formatted_date')) {
62
    /**
63
     * Formats a date with the user timezone and the selected format.
64
     *
65
     * @param string $date
66
     *
67
     * @return \Jenssegers\Date\Date
0 ignored issues
show
Documentation introduced by Joseph Cohen
Should the return type not be string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
68
     */
69
    function formatted_date($date)
70
    {
71 1
        $dateFormat = Config::get('setting.date_format', 'jS F Y');
72
73 1
        return (new Date($date))->format($dateFormat);
74
    }
75
}
76
77
if (!function_exists('color_darken')) {
78
    /**
79
     * Darken a color.
80
     *
81
     * @param string $hex
82
     * @param int    $percent
83
     *
84
     * @return string
85
     */
86
    function color_darken($hex, $percent)
87
    {
88 6
        $hex = preg_replace('/[^0-9a-f]/i', '', $hex);
89 6
        $new_hex = '#';
90
91 6
        if (strlen($hex) < 6) {
92
            $hex = $hex[0] + $hex[0] + $hex[1] + $hex[1] + $hex[2] + $hex[2];
93
        }
94
95 6
        for ($i = 0; $i < 3; $i++) {
96 6
            $dec = hexdec(substr($hex, $i * 2, 2));
97 6
            $dec = min(max(0, $dec + $dec * $percent), 255);
98 6
            $new_hex .= str_pad(dechex($dec), 2, 0, STR_PAD_LEFT);
99
        }
100
101 6
        return $new_hex;
102
    }
103
}
104
105
if (!function_exists('color_contrast')) {
106
    /**
107
     * Calculates colour contrast.
108
     *
109
     * https://24ways.org/2010/calculating-color-contrast/
110
     *
111
     * @param string $hexcolor
112
     *
113
     * @return string
114
     */
115
    function color_contrast($hexcolor)
116
    {
117 6
        $r = hexdec(substr($hexcolor, 0, 2));
118 6
        $g = hexdec(substr($hexcolor, 2, 2));
119 6
        $b = hexdec(substr($hexcolor, 4, 2));
120 6
        $yiq = (($r * 100) + ($g * 400) + ($b * 114)) / 1000;
121
122 6
        return ($yiq >= 128) ? 'black' : 'white';
123
    }
124
}
125
126
if (!function_exists('cachet_route_generator')) {
127
    /**
128
     * Generate the route string.
129
     *
130
     * @param string $name
131
     * @param string $method
132
     * @param string $domain
133
     *
134
     * @return string
135
     */
136
    function cachet_route_generator($name, $method = 'get', $domain = 'core')
137
    {
138
        return "{$domain}::{$method}:{$name}";
139 21
    }
140
}
141
142
if (!function_exists('cachet_route')) {
143
    /**
144
     * Generate a URL to a named route, which resides in a given domain.
145
     *
146
     * @param string $name
147
     * @param array  $parameters
148
     * @param string $method
149
     * @param string $domain
150
     *
151
     * @return string
152
     */
153
    function cachet_route($name, $parameters = [], $method = 'get', $domain = 'core')
154
    {
155
        return app('url')->route(
156
            cachet_route_generator($name, $method, $domain),
157
            $parameters,
158
            true
159
        );
160
    }
161
}
162
163
if (!function_exists('cachet_redirect')) {
164
    /**
165
     * Create a new redirect response to a named route, which resides in a given domain.
166
     *
167
     * @param string $name
168
     * @param array  $parameters
169
     * @param int    $status
170
     * @param array  $headers
171
     * @param string $method
172
     * @param string $domain
173
     *
174 45
     * @return \Illuminate\Http\RedirectResponse
175
     */
176
    function cachet_redirect($name, $parameters = [], $status = 302, $headers = [], $method = 'get', $domain = 'core')
177
    {
178
        $url = cachet_route($name, $parameters, $method, $domain);
179
180
        return app('redirect')->to($url, $status, $headers);
181
    }
182
}
183
184
if (!function_exists('execute')) {
185
    /**
186
     * Send the given command to the dispatcher for execution.
187
     *
188
     * @param object $command
189
     *
190
     * @return void
191
     */
192
    function execute($command)
193
    {
194
        return app(Dispatcher::class)->dispatchNow($command);
195
    }
196
}
197