Passed
Push — master ( 5d1dbf...ac3204 )
by Paul
04:29
created

Tinre::addUserToConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 8
rs 10
1
<?php
2
3
namespace Devpri\Tinre;
4
5
use Illuminate\Session\SessionManager;
6
7
class Tinre
8
{
9
    public static $config = [];
10
11
    public static $styles = [];
12
13
    public static $scripts = [];
14
15
    public static $translations = [];
16
17
    public static function dashboardPath(): string
18
    {
19
        return config('tinre.dashboard_path', '/dashboard');
20
    }
21
22
    public static function routes(): RouteRegistration
23
    {
24
        return new RouteRegistration;
25
    }
26
27
    public static function config(): array
28
    {
29
        return static::$config;
30
    }
31
32
    public static function addToConfig(array $variables): Tinre
33
    {
34
        static::$config = array_merge(static::$config, $variables);
35
36
        return new static;
37
    }
38
39
    public static function addDashboardConfig()
40
    {
41
        static::addToConfig([
42
            'app_url' => rtrim(config('app.url', null), '/').'/',
43
            'dashboard_path' => static::dashboardPath(),
44
            'timezone' => config('app.timezone', 'UTC'),
45
            'date_format' => config('tinre.date_format', 'MM/DD/YYYY, h:mm:ss a'),
46
            'roles' => config('tinre.roles', []),
47
        ]);
48
49
        return new static;
50
    }
51
52
    public static function addUserToConfig($request): Tinre
53
    {
54
        static::addToConfig([
55
            'user_permissions' => $request->user()->permissions(),
56
            'user_api_permissions' => $request->user()->apiPermissions(),
57
        ]);
58
59
        return new static;
60
    }
61
62
    public static function styles(): array
63
    {
64
        return static::$styles;
65
    }
66
67
    public static function addStyle($name, $path): Tinre
68
    {
69
        static::$styles[$name] = $path;
70
71
        return new static;
72
    }
73
74
    public static function scripts(): array
75
    {
76
        return static::$scripts;
77
    }
78
79
    public static function addScript($name, $path): Tinre
80
    {
81
        static::$scripts[$name] = $path;
82
83
        return new static;
84
    }
85
86
    public static function translations(): array
87
    {
88
        $json = [];
89
90
        foreach (static::$translations as $translation) {
91
            if (is_string($translation)) {
92
                if (is_readable($translation)) {
93
                    $translation = json_decode(file_get_contents($translation), true);
94
95
                    $json = array_merge($json, $translation);
96
                }
97
            }
98
        }
99
100
        return $json;
101
    }
102
103
    public static function addTranslation($path): Tinre
104
    {
105
        static::$translations[] = $path;
106
107
        return new static;
108
    }
109
110
    public static function messages(SessionManager $session): array
111
    {
112
        $messages = [];
113
114
        if ($session->has('status')) {
115
            $messages[] = [
116
                'type' => 'success',
117
                'text' => $session->get('status'),
118
            ];
119
        }
120
121
        if ($session->has('warning')) {
122
            $messages[] = [
123
                'type' => 'warning',
124
                'text' => $session->get('warning'),
125
            ];
126
        }
127
128
        return $messages;
129
    }
130
}
131