Completed
Pull Request — master (#219)
by bloveless
07:41
created

Reorder::handle()   C

Complexity

Conditions 8
Paths 10

Size

Total Lines 34
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 34
rs 5.3846
cc 8
eloc 13
nc 10
nop 2
1
<?php namespace Anomaly\Streams\Platform\Ui\Table\Component\Action\Handler;
2
3
use Anomaly\Streams\Platform\Model\EloquentModel;
4
use Anomaly\Streams\Platform\Ui\Table\Component\Action\ActionHandler;
5
use Anomaly\Streams\Platform\Ui\Table\TableBuilder;
6
use Illuminate\Contracts\Bus\SelfHandling;
7
use Illuminate\Http\Request;
8
9
/**
10
 * Class ReorderActionHandler
11
 *
12
 * @link          http://anomaly.is/streams-platform
13
 * @author        AnomalyLabs, Inc. <[email protected]>
14
 * @author        Ryan Thompson <[email protected]>
15
 * @package       Anomaly\Streams\Platform\Ui\Table\Component\Action\Handler
16
 */
17
class Reorder extends ActionHandler implements SelfHandling
18
{
19
20
    /**
21
     * Save the order of the entries.
22
     *
23
     * @param TableBuilder $builder
24
     * @param Request      $request
25
     */
26
    public function handle(TableBuilder $builder, Request $request)
27
    {
28
        $count = 0;
29
30
        $model = $builder->getTableModel();
31
32
        /* @var EloquentModel $entry */
33
        foreach ($request->get($builder->getTableOption('prefix') . 'order', []) as $k => $id) {
34
35
            /**
36
             * If this is a many to many relationship (multiple) then use the entry_id and related_id
37
             * rather than the id
38
             */
39
            if (strstr($id, ':')) {
40
                $idParts = explode(':', $id);
41
                if($model->where('entry_id', $idParts[0])->where('related_id', $idParts[1])->update(['sort_order' => $k + 1])) {
0 ignored issues
show
Documentation Bug introduced by
The method where does not exist on object<Anomaly\Streams\P...rm\Model\EloquentModel>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
42
                    $count++;
43
                }
44
            }
45
46
            /**
47
             * Otherwise just use the id
48
             */
49
            if (is_numeric($id) && $entry = $model->find($id)) {
0 ignored issues
show
Documentation Bug introduced by
The method find does not exist on object<Anomaly\Streams\P...rm\Model\EloquentModel>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
50
                if (($entry->sort_order = $k + 1) && $entry->save()) {
51
                    $count++;
52
                }
53
            }
54
        }
55
56
        $builder->fire('reordered', compact('count', 'builder'));
57
58
        $this->messages->success(trans('streams::message.reorder_success', compact('count')));
59
    }
60
}
61