Passed
Push — ft/appmove ( db87fd...97613e )
by Philippe
45:05 queued 26:47
created

InternalLinksController::index()   A

Complexity

Conditions 5
Paths 2

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 13
nc 2
nop 1
dl 0
loc 23
c 0
b 0
f 0
cc 5
rs 9.5222
1
<?php
2
3
namespace Thinktomorrow\Chief\App\Http\Controllers\Api;
4
5
use Illuminate\Http\Request;
6
use Thinktomorrow\Chief\App\Http\Controllers\Controller;
7
use Thinktomorrow\Chief\Pages\Page;
8
use Thinktomorrow\Chief\Urls\ProvidesUrl\ProvidesUrl;
9
use Thinktomorrow\Chief\Urls\UrlHelper;
10
11
class InternalLinksController extends Controller
12
{
13
    public function index(Request $request)
14
    {
15
        $locale = app()->getLocale();
0 ignored issues
show
introduced by
The method getLocale() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

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

15
        $locale = app()->/** @scrutinizer ignore-call */ getLocale();
Loading history...
Unused Code introduced by
The assignment to $locale is dead and can be removed.
Loading history...
16
17
        // Fetch the links for specific locale
18
        if ($request->has('locale')) {
19
            $locale = $request->input('locale');
20
            app()->setLocale($locale);
0 ignored issues
show
introduced by
The method setLocale() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

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

20
            app()->/** @scrutinizer ignore-call */ setLocale($locale);
Loading history...
21
        }
22
23
        $onlineModels = UrlHelper::onlineModels();
24
25
        $links = $onlineModels->reject(function (ProvidesUrl $model) {
26
            return !$model->url();
27
        })->map(function ($model) {
28
            $name = (method_exists($model, 'menuLabel') && $model->menuLabel()) ? $model->menuLabel() : (isset($model->title) ? $model->title : $model->url());
29
            return [
30
                'name' => $name ??  $model->url(),
31
                'url' => $model->url(),
32
            ];
33
        });
34
35
        return response()->json($links->prepend(['name' => '...', 'url' => ''])->all());
36
    }
37
}
38