DomainsTreeController::getFormattedDomainToAdd()   B
last analyzed

Complexity

Conditions 8
Paths 34

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 20
rs 8.4444
cc 8
nc 34
nop 2
1
<?php
2
3
namespace Uccello\Core\Http\Controllers\Core;
4
5
use Illuminate\Support\Facades\Cache;
6
use Uccello\Core\Models\Domain;
7
8
class DomainsTreeController
9
{
10
    /**
11
     * Returns all roots domains where the user can access
12
     *
13
     * @return array
14
     */
15
    public function root(?Domain $domain)
16
    {
17
        // If we don't use multi domains, find the first one
18
        if (!uccello()->useMultiDomains()) {
0 ignored issues
show
Bug introduced by
Are you sure the usage of uccello() is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
19
            $domain = Domain::firstOrFail();
20
        }
21
22
        return Cache::rememberForever('domains_root_user_'.auth()->id(), function () use($domain) {
23
            $rootDomains = app('uccello')->getRootDomains();
0 ignored issues
show
Bug introduced by
The method getRootDomains() does not exist on Uccello\Core\Facades\Uccello. ( Ignorable by Annotation )

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

23
            $rootDomains = app('uccello')->/** @scrutinizer ignore-call */ getRootDomains();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
24
25
            $domains = [];
26
            foreach ($rootDomains as $_domain) {
27
                $formattedDomain = $this->getFormattedDomainToAdd($domain, $_domain);
28
                if ($formattedDomain) {
29
                    $domains[] = $formattedDomain;
30
                }
31
            }
32
33
            return $domains;
34
        });
35
    }
36
37
    /**
38
     * Returns all domain's children where the user can access
39
     *
40
     * @return array
41
     */
42
    public function children(?Domain $domain)
43
    {
44
        // If we don't use multi domains, find the first one
45
        if (!uccello()->useMultiDomains()) {
0 ignored issues
show
Bug introduced by
Are you sure the usage of uccello() is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
46
            $domain = Domain::firstOrFail();
47
        }
48
49
        $parentDomain = Domain::find(request('id'));
50
51
        return Cache::rememberForever('domains_'.$parentDomain->id.'_children_user_'.auth()->id(), function () use($domain, $parentDomain) {
52
            $domains = [];
53
            if ($parentDomain) {
54
                foreach ($parentDomain->children()->orderBy('name')->get() as $_domain) {
55
                    $formattedDomain = $this->getFormattedDomainToAdd($domain, $_domain);
56
                    if ($formattedDomain) {
57
                        $domains[] = $formattedDomain;
58
                    }
59
                }
60
            }
61
62
            return $domains;
63
        });
64
    }
65
66
    /**
67
     * Get formatted domain to add to the tree
68
     *
69
     * @param \Uccello\Core\Models\Domain $currentDomain
70
     * @param \Uccello\Core\Models\Domain $domain
71
     * @return array|null
72
     */
73
    protected function getFormattedDomainToAdd(Domain $currentDomain, Domain $domain)
74
    {
75
        $formattedDomain = null;
76
77
        $hasRoleOnDomain = auth()->user()->hasRoleOnDomain($domain);
78
        $hasRoleOnDescendantDomain = $domain->children->count() > 0 ? auth()->user()->hasRoleOnDescendantDomain($domain) : false;
79
80
        if ($hasRoleOnDomain || $hasRoleOnDescendantDomain) {
81
            $formattedDomain = [
82
                "id" => $domain->id,
83
                "text" => $domain->name,
84
                "children" => $domain->children->count() > 0,
85
                "a_attr" => [
86
                    "href" => $hasRoleOnDomain && $domain->id !== $currentDomain->id ? ucroute('uccello.home', $domain) : '#',
87
                    "class" => !$hasRoleOnDomain ? 'disabled' : $domain->id === $currentDomain->id ? 'current' : ''
88
                ]
89
            ];
90
        }
91
92
        return $formattedDomain;
93
    }
94
}