Passed
Branch master (249862)
by Adam
07:51
created

CategoriesController::save()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 12
nc 1
nop 1
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
1
<?php
2
3
namespace Coyote\Http\Controllers\Adm\Forum;
4
5
use Boduch\Grid\Source\CollectionSource;
6
use Coyote\Events\ForumWasSaved;
7
use Coyote\Http\Controllers\Adm\BaseController;
8
use Coyote\Http\Forms\Forum\ForumForm;
9
use Coyote\Http\Grids\Adm\Forum\CategoriesGrid;
10
use Coyote\Repositories\Contracts\ForumRepositoryInterface as ForumRepository;
11
use Coyote\Services\Forum\TreeBuilder;
12
use Illuminate\Contracts\Cache\Repository as Cache;
13
use Illuminate\Http\Request;
14
15
class CategoriesController extends BaseController
16
{
17
    /**
18
     * @var ForumRepository
19
     */
20
    private $forum;
21
22
    /**
23
     * @param ForumRepository $forum
24
     */
25
    public function __construct(ForumRepository $forum)
26
    {
27
        parent::__construct();
28
29
        $this->forum = $forum;
30
        $this->breadcrumb->push('Forum', route('adm.forum.categories'));
31
    }
32
33
    /**
34
     * @return \Illuminate\View\View
35
     */
36
    public function index()
37
    {
38
        $treeBuilder = new TreeBuilder();
39
40
        $grid = $this
41
            ->gridBuilder()
42
            ->createGrid(CategoriesGrid::class)
43
            ->setSource(new CollectionSource(collect($treeBuilder->flat($this->forum->orderBy('order')->get()))))
0 ignored issues
show
Bug introduced by
The method orderBy() does not exist on Coyote\Repositories\Cont...orumRepositoryInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Coyote\Repositories\Cont...orumRepositoryInterface. ( Ignorable by Annotation )

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

43
            ->setSource(new CollectionSource(collect($treeBuilder->flat($this->forum->/** @scrutinizer ignore-call */ orderBy('order')->get()))))
Loading history...
44
            ->setEnablePagination(false);
45
46
        return $this->view('adm.forum.categories.home')->with('grid', $grid);
47
    }
48
49
    /**
50
     * @param int|null $id
51
     * @return \Illuminate\View\View
52
     */
53
    public function edit($id = null)
54
    {
55
        $forum = $this->forum->findOrNew($id);
56
        $this->breadcrumb->push($forum->name ?? 'Dodaj nową');
57
58
        return $this->view('adm.forum.categories.save')->with('form', $this->getForm($forum));
59
    }
60
61
    /**
62
     * @param int|null $id
63
     * @return \Illuminate\Http\RedirectResponse
64
     */
65
    public function save($id = null)
66
    {
67
        /** @var \Coyote\Forum $forum */
68
        $forum = $this->forum->findOrNew($id);
69
70
        $form = $this->getForm($forum);
71
        $form->validate();
72
73
        $forum->fill($form->all());
74
75
        $this->transaction(function () use ($form, $forum) {
76
            $forum->save();
77
            $forum->access()->delete();
78
79
            foreach ((array) $form->get('access')->getValue() as $groupId) {
80
                $forum->access()->create(['group_id' => $groupId]);
81
            }
82
83
            $this->flushCache();
84
            event(new ForumWasSaved($forum));
85
        });
86
87
        return redirect()->route('adm.forum.categories')->with('success', 'Zmiany zostały zapisane.');
88
    }
89
90
    /**
91
     * @param int $id
92
     * @param Request $request
93
     * @return \Illuminate\Http\RedirectResponse
94
     */
95
    public function move($id, Request $request)
96
    {
97
        $this->validate($request, ['mode' => 'required|in:up,down']);
98
99
        $mode = $request->get('mode');
100
        $this->forum->$mode($id);
101
102
        $this->flushCache();
103
104
        return redirect()->route('adm.forum.categories')->with('success', 'Pozycja została zmieniona.');
105
    }
106
107
    private function flushCache()
108
    {
109
        app(Cache::class)->tags('menu-for-user')->flush();
110
    }
111
112
    /**
113
     * @param \Coyote\Forum $forum
114
     * @return \Coyote\Services\FormBuilder\Form
115
     */
116
    private function getForm($forum)
117
    {
118
        return $this->createForm(ForumForm::class, $forum);
119
    }
120
}
121