Completed
Push — master ( 5b4baf...4a3f73 )
by ARCANEDEV
04:07
created

CategoriesController::transNotification()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
ccs 0
cts 8
cp 0
rs 9.4285
cc 1
eloc 6
nc 1
nop 3
crap 2
1
<?php namespace Arcanesoft\Blog\Http\Controllers\Admin;
2
3
use Arcanedev\LaravelApiHelper\Traits\JsonResponses;
4
use Arcanesoft\Blog\Http\Requests\Admin\Categories\CreateCategoryRequest;
5
use Arcanesoft\Blog\Http\Requests\Admin\Categories\UpdateCategoryRequest;
6
use Arcanesoft\Blog\Models\Category;
7
use Arcanesoft\Blog\Policies\CategoriesPolicy;
8
use Illuminate\Support\Facades\Log;
9
10
/**
11
 * Class     CategoriesController
12
 *
13
 * @package  Arcanesoft\Blog\Http\Controllers\Admin
14
 * @author   ARCANEDEV <[email protected]>
15
 */
16
class CategoriesController extends Controller
17
{
18
    /* -----------------------------------------------------------------
19
     |  Traits
20
     | -----------------------------------------------------------------
21
     */
22
23
    use JsonResponses;
24
25
    /* -----------------------------------------------------------------
26
     |  Properties
27
     | -----------------------------------------------------------------
28
     */
29
30
    /**
31
     * The category model.
32
     *
33
     * @var \Arcanesoft\Blog\Models\Category
34
     */
35
    private $category;
36
37
    /* -----------------------------------------------------------------
38
     |  Constructor
39
     | -----------------------------------------------------------------
40
     */
41
42
    /**
43
     * Instantiate the controller.
44
     *
45
     * @param  \Arcanesoft\Blog\Models\Category  $category
46
     */
47
    public function __construct(Category $category)
48
    {
49
        parent::__construct();
50
51
        $this->category = $category;
52
53
        $this->setCurrentPage('blog-categories');
54
        $this->addBreadcrumbRoute(trans('blog::categories.titles.categories'), 'admin::blog.categories.index');
55
    }
56
57
    /* -----------------------------------------------------------------
58
     |  Main Methods
59
     | -----------------------------------------------------------------
60
     */
61
62
    public function index($trashed = false)
63
    {
64
        $this->authorize(CategoriesPolicy::PERMISSION_LIST);
65
66
        $categories = $this->category->with(['posts']);
67
        $categories = $trashed
68
            ? $categories->onlyTrashed()->paginate(30)
69
            : $categories->paginate(30);
70
71
        $this->setTitle($title = trans('blog::categories.titles.categories-list'));
72
        $this->addBreadcrumb($title);
73
74
        return $this->view('admin.categories.list', compact('categories', 'trashed'));
75
    }
76
77
    public function trash()
78
    {
79
        return $this->index(true);
80
    }
81
82
    public function create()
83
    {
84
        $this->authorize(CategoriesPolicy::PERMISSION_CREATE);
85
86
        $this->setTitle($title = trans('blog::categories.titles.create-category'));
87
        $this->addBreadcrumb($title);
88
89
        return $this->view('admin.categories.create');
90
    }
91
92
    public function store(CreateCategoryRequest $request, Category $category)
93
    {
94
        $this->authorize(CategoriesPolicy::PERMISSION_CREATE);
95
96
        $category->fill($request->only(['name']));
97
        $category->save();
98
99
        $this->transNotification('created', ['name' => $category->name], $category->toArray());
100
101
        return redirect()->route('admin::blog.categories.index');
102
    }
103
104
    public function show(Category $category)
105
    {
106
        $this->authorize(CategoriesPolicy::PERMISSION_SHOW);
107
108
        $category->load(['posts']);
109
110
        $this->setTitle($title = trans('blog::categories.titles.category-details'));
111
        $this->addBreadcrumb($category->name);
112
113
        return $this->view('admin.categories.show', compact('category'));
114
    }
115
116
    public function edit(Category $category)
117
    {
118
        $this->authorize(CategoriesPolicy::PERMISSION_UPDATE);
119
120
        $this->setTitle($title = trans('blog::categories.titles.edit-category'));
121
        $this->addBreadcrumb($title);
122
123
        return $this->view('admin.categories.edit', compact('category'));
124
    }
125
126
    public function update(UpdateCategoryRequest $request, Category $category)
127
    {
128
        $this->authorize(CategoriesPolicy::PERMISSION_UPDATE);
129
130
        $category->update($request->only(['name']));
131
132
        $this->transNotification('updated', ['name' => $category->name], $category->toArray());
133
134
        return redirect()->route('admin::blog.categories.show', [$category]);
135
    }
136
137
    public function delete(Category $category)
138
    {
139
        $this->authorize(CategoriesPolicy::PERMISSION_DELETE);
140
141
        try {
142
            $category->trashed() ? $category->forceDelete() : $category->delete();
143
144
            return $this->jsonResponseSuccess([
145
                'message' => $this->transNotification('deleted', ['name' => $category->name], $category->toArray())
146
            ]);
147
        }
148
        catch(\Exception $e) {
149
            return $this->jsonResponseError($e->getMessage(), 500);
150
        }
151
    }
152
153
    public function restore(Category $category)
154
    {
155
        $this->authorize(CategoriesPolicy::PERMISSION_UPDATE);
156
157
        try {
158
            $category->restore();
159
160
            return $this->jsonResponseSuccess([
161
                'message' => $this->transNotification('restored', ['name' => $category->name], $category->toArray())
162
            ]);
163
        }
164
        catch (\Exception $e) {
165
            return $this->jsonResponseError($e->getMessage(), 500);
166
        }
167
    }
168
169
    /* -----------------------------------------------------------------
170
     |  Other Methods
171
     | -----------------------------------------------------------------
172
     */
173
174
    /**
175
     * Notify with translation.
176
     *
177
     * @param  string  $action
178
     * @param  array   $replace
179
     * @param  array   $context
180
     *
181
     * @return string
182
     */
183
    protected function transNotification($action, array $replace = [], array $context = [])
184
    {
185
        $title   = trans("blog::categories.messages.{$action}.title");
186
        $message = trans("blog::categories.messages.{$action}.message", $replace);
187
188
        Log::info($message, $context);
189
        $this->notifySuccess($message, $title);
190
191
        return $message;
192
    }
193
}
194