Passed
Push — master ( 340202...35f943 )
by Jonathan
19:55
created

DomainsTreeController::getFormattedDomainToAdd()   B

Complexity

Conditions 7
Paths 17

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.8333
cc 7
nc 17
nop 2
1
<?php
2
3
namespace Uccello\Core\Http\Controllers\Core;
4
5
use Uccello\Core\Models\Domain;
6
7
class DomainsTreeController
8
{
9
    /**
10
     * Returns all roots domains where the user can access
11
     *
12
     * @return array
13
     */
14
    public function root(?Domain $domain)
15
    {
16
        // If we don't use multi domains, find the first one
17
        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...
18
            $domain = Domain::firstOrFail();
19
        }
20
21
        $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

21
        $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...
22
23
        $domains = [];
24
        foreach ($rootDomains as $_domain) {
25
            $formattedDomain = $this->getFormattedDomainToAdd($domain, $_domain);
26
            if ($formattedDomain) {
27
                $domains[] = $formattedDomain;
28
            }
29
        }
30
31
        return $domains;
32
    }
33
34
    /**
35
     * Returns all domain's children where the user can access
36
     *
37
     * @return array
38
     */
39
    public function children(?Domain $domain)
40
    {
41
        // If we don't use multi domains, find the first one
42
        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...
43
            $domain = Domain::firstOrFail();
44
        }
45
46
        $parentDomain = Domain::find(request('id'));
47
48
        $domains = [];
49
        if ($parentDomain) {
50
            foreach ($parentDomain->children()->orderBy('name')->get() as $_domain) {
51
                $formattedDomain = $this->getFormattedDomainToAdd($domain, $_domain);
52
                if ($formattedDomain) {
53
                    $domains[] = $formattedDomain;
54
                }
55
            }
56
        }
57
58
        return $domains;
59
    }
60
61
    /**
62
     * Get formatted domain to add to the tree
63
     *
64
     * @param \Uccello\Core\Models\Domain $currentDomain
65
     * @param \Uccello\Core\Models\Domain $domain
66
     * @return array|null
67
     */
68
    protected function getFormattedDomainToAdd(Domain $currentDomain, Domain $domain)
69
    {
70
        $formattedDomain = null;
71
72
        $hasRoleOnDomain = auth()->user()->hasRoleOnDomain($domain);
73
        $hasRoleOnDescendantDomain = auth()->user()->hasRoleOnDescendantDomain($domain);
74
75
        if ($hasRoleOnDomain || $hasRoleOnDescendantDomain) {
76
            $formattedDomain = [
77
                "id" => $domain->id,
78
                "text" => $domain->name,
79
                "children" => $domain->children->count() > 0,
80
                "a_attr" => [
81
                    "href" => $hasRoleOnDomain && $domain->id !== $currentDomain->id ? ucroute('uccello.home', $domain) : '#',
82
                    "class" => !$hasRoleOnDomain ? 'disabled' : $domain->id === $currentDomain->id ? 'current' : ''
83
                ]
84
            ];
85
        }
86
87
        return $formattedDomain;
88
    }
89
}