Passed
Push — shop ( 5180ff...89f6e7 )
by Fèvre
05:07
created

ItemController::showCreateForm()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 9
rs 10
1
<?php
2
3
namespace Xetaravel\Http\Controllers\Admin\Shop;
4
5
use Illuminate\Http\RedirectResponse;
6
use Illuminate\Http\Request;
7
use Illuminate\Support\Str;
8
use Illuminate\View\View;
9
use Xetaio\Mentions\Parser\MentionParser;
10
use Xetaravel\Http\Controllers\Admin\Controller;
11
use Xetaravel\Models\ShopItem;
12
use Xetaravel\Models\ShopCategory;
13
use Xetaravel\Models\Repositories\ShopItemRepository;
14
use Xetaravel\Models\Validators\ShopItemValidator;
15
16
class ItemController extends Controller
17
{
18
    /**
19
     * Constructor.
20
     */
21
    public function __construct()
22
    {
23
        parent::__construct();
24
25
        $this->breadcrumbs->addCrumb('Shop', route('admin.shop.item.index'));
26
    }
27
28
    /**
29
     * Show all articles.
30
     *
31
     * @return \Illuminate\View\View
32
     */
33
    public function index(): View
34
    {
35
        $this->breadcrumbs->addCrumb('Manage Articles', route('admin.blog.article.index'));
36
37
        return view('Admin::Shop.item.index', ['breadcrumbs' => $this->breadcrumbs]);
38
    }
39
40
    /**
41
     * Show the item create form.
42
     *
43
     * @return \Illuminate\View\View
44
     */
45
    public function showCreateForm(): View
46
    {
47
        $categories = ShopCategory::pluck('title', 'id');
48
49
        $breadcrumbs = $this->breadcrumbs
50
            ->addCrumb('Manage Items', route('admin.shop.item.index'))
51
            ->addCrumb("Create", route('admin.shop.item.create'));
52
53
        return view('Admin::Shop.item.create', compact('categories', 'breadcrumbs'));
54
    }
55
56
    /**
57
     * Handle an article create request for the application.
58
     *
59
     * @param \Illuminate\Http\Request $request
60
     *
61
     * @return \Illuminate\Http\RedirectResponse
62
     */
63
    public function create(Request $request): RedirectResponse
64
    {
65
        ShopItemValidator::create($request->all())->validate();
66
        $item = ShopItemRepository::create($request->all());
67
68
        $item->save();
69
70
        // Default icon for the article.
71
        $icon = public_path('images/shop/default_icon.svg');
72
73
        if (!is_null($request->file('item'))) {
74
            $icon = $request->file('item');
75
        }
76
77
        $item->clearMediaCollection('item');
78
        $item->addMedia($icon)
0 ignored issues
show
Bug introduced by
It seems like $icon can also be of type Illuminate\Http\UploadedFile[] and array and null; however, parameter $file of Xetaravel\Models\ShopItem::addMedia() does only seem to accept Symfony\Component\HttpFo...ile\UploadedFile|string, maybe add an additional type check? ( Ignorable by Annotation )

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

78
        $item->addMedia(/** @scrutinizer ignore-type */ $icon)
Loading history...
79
            ->preservingOriginal()
80
            ->setName(substr(md5($item->slug), 0, 10))
81
            ->setFileName(
82
                substr(md5($item->slug), 0, 10) . '.svg'
83
            )
84
            ->toMediaCollection('item');
85
86
        return redirect()
87
            ->route('admin.shop.item.index')
88
            ->with('success', 'Your item has been created successfully !');
89
    }
90
}
91