Completed
Push — master ( f67f4f...77dcfd )
by Curtis
40s queued 12s
created

AppState::meta()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 11
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 13
rs 9.9
1
<?php
2
3
namespace App\Http\Response\enso\core;
4
5
use Illuminate\Contracts\Support\Responsable;
6
use Illuminate\Support\Collection;
7
use Illuminate\Support\Facades\App;
8
use Illuminate\Support\Facades\Auth;
9
use Illuminate\Support\Facades\Config;
10
use Illuminate\Support\Facades\Route;
11
use Illuminate\Support\Facades\Session;
12
use LaravelEnso\Core\App\Enums\Themes;
13
use LaravelEnso\Core\App\Http\Resources\User;
14
use LaravelEnso\Core\App\Services\Inspiring;
15
use LaravelEnso\Core\App\Services\LocalState;
16
use LaravelEnso\Enums\App\Facades\Enums;
17
use LaravelEnso\Enums\App\Services\Enum;
18
use LaravelEnso\Helpers\App\Classes\JsonReader;
19
use App\Models\enso\Localisation\Language;
20
use LaravelEnso\Menus\App\Http\Resources\Menu;
21
use LaravelEnso\Menus\App\Services\TreeBuilder;
22
use App\Models\enso\Permissions\Permission;
23
use LaravelEnso\Roles\App\Enums\Roles;
24
use App\Models\enso\Roles\Role;
25
26
class AppState implements Responsable
27
{
28
    protected Role $role;
29
    protected Collection $languages;
30
31
    public function toResponse($request): array
32
    {
33
        $this->prepare();
34
35
        return $this->response();
36
    }
37
38
    protected function response(): array
39
    {
40
        return [
41
            'user' => new User(Auth::user()->load(['person', 'avatar', 'role', 'group'])),
42
            'preferences' => Auth::user()->preferences(),
0 ignored issues
show
Bug introduced by
The method preferences() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of Illuminate\Contracts\Auth\Authenticatable such as Illuminate\Foundation\Auth\User. ( Ignorable by Annotation )

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

42
            'preferences' => Auth::user()->/** @scrutinizer ignore-call */ preferences(),
Loading history...
43
            'i18n' => $this->i18n(),
44
            'languages' => $this->languages->pluck('flag', 'name'),
45
            'rtl' => $this->rtl(),
46
            'themes' => Themes::all(),
47
            'routes' => $this->routes(),
48
            'implicitRoute' => $this->role->menu->permission->name,
49
            'menus' => Menu::collection(App::make(TreeBuilder::class)->handle()),
50
            'impersonating' => Session::has('impersonating'),
51
            'websockets' => [
52
                'pusher' => [
53
                    'key' => Config::get('broadcasting.connections.pusher.key'),
54
                    'options' => Config::get('broadcasting.connections.pusher.options'),
55
                ],
56
                'channels' => [
57
                    'privateChannel' => $this->privateChannel(),
58
                    'ioChannel' => $this->ioChannel(),
59
                    'appUpdates' => 'app-updates',
60
                ],
61
            ],
62
            'meta' => $this->meta(),
63
            'enums' => Enums::all(),
64
            'local' => App::make(LocalState::class)->build(),
65
        ];
66
    }
67
68
    protected function i18n(): Collection
69
    {
70
        return $this->languages
71
            ->reject(fn ($language) => $language->name === 'en')
72
            ->mapWithKeys(fn ($language) => [
73
                $language->name => $this->lang($language),
74
            ]);
75
    }
76
77
    protected function lang(Language $language)
78
    {
79
        return (new JsonReader(
80
            resource_path('lang'.DIRECTORY_SEPARATOR."{$language->name}.json")
81
        ))->object();
82
    }
83
84
    protected function rtl(): Collection
85
    {
86
        return $this->languages
87
            ->filter(fn ($lang) => $lang->is_rtl)->pluck('name');
88
    }
89
90
    protected function meta(): array
91
    {
92
        return [
93
            'appName' => Config::get('app.name'),
94
            'appUrl' => url('/').'/',
95
            'version' => Config::get('enso.config.version'),
96
            'quote' => Inspiring::quote(),
97
            'env' => App::environment(),
98
            'dateFormat' => Config::get('enso.config.dateFormat'),
99
            'dateTimeFormat' => Config::get('enso.config.dateFormat').' H:i:s',
100
            'extendedDocumentTitle' => Config::get('enso.config.extendedDocumentTitle'),
101
            'csrfToken' => csrf_token(),
102
            'sentryDsn' => Config::get('sentry.dsn'),
103
        ];
104
    }
105
106
    protected function routes(): Collection
107
    {
108
        return $this->role->permissions
109
            ->mapWithKeys(fn ($permission) => [
110
                $permission->name => $this->route($permission),
111
            ]);
112
    }
113
114
    protected function route(Permission $permission): ?array
115
    {
116
        $route = Route::getRoutes()->getByName($permission->name);
117
118
        return $route
119
            ? (new Collection($route))->only(['uri', 'methods'])
120
            ->put('domain', $route->domain())
121
            ->toArray()
122
            : null;
123
    }
124
125
    protected function privateChannel(): string
126
    {
127
        return (new Collection(
128
            explode('\\', Config::get('auth.providers.users.model'))
129
        ))->push(Auth::user()->id)->implode('.');
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
130
    }
131
132
    protected function ioChannel(): string
133
    {
134
        $roles = App::make(Roles::class);
135
136
        return in_array(Auth::user()->role_id, [$roles::Admin, $roles::Supervisor])
0 ignored issues
show
Bug introduced by
Accessing role_id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
137
            ? 'operations'
138
            : 'operations'.Auth::user()->id;
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
139
    }
140
141
    protected function prepare(): void
142
    {
143
        $this->role = Auth::user()->role()
0 ignored issues
show
Bug introduced by
The method role() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of Illuminate\Contracts\Auth\Authenticatable such as Illuminate\Foundation\Auth\User. ( Ignorable by Annotation )

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

143
        $this->role = Auth::user()->/** @scrutinizer ignore-call */ role()
Loading history...
144
            ->with('menu.permission', 'permissions')->first();
145
146
        $this->languages = Language::active()
147
            ->get(['name', 'flag', 'is_rtl']);
148
149
        Enum::localisation(false);
150
    }
151
}
152