InternalLinksController::index()   A
last analyzed

Complexity

Conditions 5
Paths 2

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 11
nc 2
nop 1
dl 0
loc 21
rs 9.6111
c 0
b 0
f 0
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\Site\Urls\UrlHelper;
8
use Thinktomorrow\Chief\Site\Visitable\Visitable;
9
10
class InternalLinksController extends Controller
11
{
12
    public function index(Request $request)
13
    {
14
        // Fetch the links for specific locale
15
        if ($request->has('locale')) {
16
            app()->setLocale($request->input('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

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