Completed
Push — master ( bbc7be...00b74d )
by Jonathan
23:11
created

EditController::configTree()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Uccello\Core\Http\Controllers\Domain;
4
5
use Illuminate\Http\Request;
6
use Uccello\Core\Http\Controllers\Core\EditController as CoreEditController;
7
use Uccello\Core\Models\Domain;
8
use Uccello\Core\Models\Module;
9
10
class EditController extends CoreEditController
11
{
12
    /**
13
     * {@inheritdoc}
14
     */
15
    public function save(?Domain $domain, Module $module, Request $request, bool $redirect = true)
16
    {
17
        // To use $record->setAsRoot(), we have to get the current version of the domain,
18
        // before to set parent_id = null. Because $record->setAsRoot() works only on a
19
        // domain where parent_id != null
20
        $record = Domain::find($request->get('id'));
21
22
        // Get mode
23
        $mode = $record->getKey() ? 'edit' : 'create';
24
25
        if ($mode == 'edit') { // Config before update
26
            $this->configTree($record);
27
        }
28
29
        // Default behaviour without redirection
30
        $record = parent::save($domain, $module, $request, false); // Get a fresh version of $record, after saving
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $record is correct as parent::save($domain, $module, $request, false) targeting Uccello\Core\Http\Contro...\EditController::save() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

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

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

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

Loading history...
31
32
        if ($mode == 'create') { // Config after create
33
            $this->configTree($record);
0 ignored issues
show
Bug introduced by
$record of type void is incompatible with the type Uccello\Core\Models\Domain expected by parameter $record of Uccello\Core\Http\Contro...ontroller::configTree(). ( Ignorable by Annotation )

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

33
            $this->configTree(/** @scrutinizer ignore-type */ $record);
Loading history...
34
        }
35
36
        // Update current domain if we are editing it (data could have been changed)
37
        if ($this->domain->getKey() === $record->getKey()) {
38
            $this->domain = $record;
0 ignored issues
show
Documentation Bug introduced by
It seems like $record of type void is incompatible with the declared type Uccello\Core\Models\Domain of property $domain.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
39
        }
40
41
        // Redirect to detail view (we use $this->domain instead of $domain because slug could have been changed)
42
        if ($redirect === true) {
43
            $route = ucroute('uccello.detail', $this->domain, $module, [ 'id' => $record->getKey() ]);
44
45
            return redirect($route);
46
        }
47
    }
48
49
    /**
50
     * Config tree path
51
     *
52
     * @param \Uccello\Core\Models\Domain $record
53
     * @return void
54
     */
55
    protected function configTree($record)
56
    {
57
        // Set parent domain
58
        $parentDomain = Domain::find(request('parent'));
59
        if (!is_null($parentDomain)) {
60
            with($record)->setChildOf($parentDomain);
61
        }
62
        // Remove parent domain
63
        else {
64
            $record->setAsRoot();
65
        }
66
    }
67
}
68