Completed
Pull Request — master (#206)
by Ryan
07:29
created

SetDefaultOptions::handle()   C

Complexity

Conditions 11
Paths 48

Size

Total Lines 49
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
dl 0
loc 49
rs 5.2653
c 2
b 1
f 0
cc 11
eloc 15
nc 48
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php namespace Anomaly\Streams\Platform\Ui\Table\Command;
2
3
use Anomaly\Streams\Platform\Addon\Module\ModuleCollection;
4
use Anomaly\Streams\Platform\Ui\Table\TableBuilder;
5
use Illuminate\Contracts\Bus\SelfHandling;
6
7
/**
8
 * Class SetDefaultOptions
9
 *
10
 * @link    http://anomaly.is/streams-platform
11
 * @author  AnomalyLabs, Inc. <[email protected]>
12
 * @author  Ryan Thompson <[email protected]>
13
 * @package Anomaly\Streams\Platform\Ui\Table\Command
14
 */
15
class SetDefaultOptions implements SelfHandling
16
{
17
18
    /**
19
     * The table builder.
20
     *
21
     * @var TableBuilder
22
     */
23
    protected $builder;
24
25
    /**
26
     * Create a new SetDefaultOptions instance.
27
     *
28
     * @param TableBuilder $builder
29
     */
30
    public function __construct(TableBuilder $builder)
31
    {
32
        $this->builder = $builder;
33
    }
34
35
    /**
36
     * Handle the command.
37
     *
38
     * @param ModuleCollection $modules
39
     */
40
    public function handle(ModuleCollection $modules)
41
    {
42
        $table = $this->builder->getTable();
43
44
        /**
45
         * Set the default sortable option.
46
         */
47
        if ($table->getOption('sortable') === null) {
48
49
            $stream = $table->getStream();
50
51
            if ($stream && $stream->isSortable()) {
52
                $table->setOption('sortable', true);
53
            }
54
        }
55
56
        /**
57
         * Sortable tables have no pages.
58
         */
59
        if (($table->getOption('sortable') === true)) {
60
            $table->setOption('limit', $table->getOption('limit', 99999));
61
        }
62
63
        /**
64
         * Set the default breadcrumb.
65
         */
66
        if ($table->getOption('breadcrumb') === null && $title = $table->getOption('title')) {
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $title is correct as $table->getOption('title') (which targets Anomaly\Streams\Platform...able\Table::getOption()) 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...
67
            $table->setOption('breadcrumb', $title);
68
        }
69
70
        /**
71
         * If the table ordering is currently being overridden
72
         * then set the values from the request on the builder
73
         * last so it actually has an effect.
74
         */
75
        if ($orderBy = $this->builder->getRequestValue('order_by')) {
76
            $table->setOption('order_by', [$orderBy => $this->builder->getRequestValue('sort', 'asc')]);
77
        }
78
79
        /**
80
         * If the permission is not set then
81
         * try and automate it.
82
         */
83
        if ($table->getOption('permission') === null && ($module = $modules->active(
84
            )) && ($stream = $this->builder->getTableStream())
85
        ) {
86
            $table->setOption('permission', $module->getNamespace($stream->getSlug() . '.read'));
87
        }
88
    }
89
}
90