Completed
Push — master ( 64105c...53d741 )
by Nicolas
10:29 queued 08:07
created

TagController   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 109
Duplicated Lines 12.84 %

Coupling/Cohesion

Components 2
Dependencies 6

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 14
loc 109
wmc 9
lcom 2
cbo 6
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A index() 0 6 1
A create() 0 6 1
A store() 0 7 1
A edit() 0 6 1
A update() 7 7 1
A destroy() 7 7 1
A formatNamespaces() 0 9 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Modules\Tag\Http\Controllers\Admin;
4
5
use Modules\Core\Http\Controllers\Admin\AdminBaseController;
6
use Modules\Tag\Entities\Tag;
7
use Modules\Tag\Http\Requests\CreateTagRequest;
8
use Modules\Tag\Http\Requests\UpdateTagRequest;
9
use Modules\Tag\Repositories\TagManager;
10
use Modules\Tag\Repositories\TagRepository;
11
12
class TagController extends AdminBaseController
13
{
14
    /**
15
     * @var TagRepository
16
     */
17
    private $tag;
18
    /**
19
     * @var TagManager
20
     */
21
    private $tagManager;
22
23
    public function __construct(TagRepository $tag, TagManager $tagManager)
24
    {
25
        parent::__construct();
26
27
        $this->tag = $tag;
28
        $this->tagManager = $tagManager;
29
    }
30
31
    /**
32
     * Display a listing of the resource.
33
     *
34
     * @return Response
35
     */
36
    public function index()
37
    {
38
        $tags = $this->tag->all();
39
40
        return view('tag::admin.tags.index', compact('tags'));
41
    }
42
43
    /**
44
     * Show the form for creating a new resource.
45
     *
46
     * @return Response
47
     */
48
    public function create()
49
    {
50
        $namespaces = $this->formatNamespaces($this->tagManager->getNamespaces());
51
52
        return view('tag::admin.tags.create', compact('namespaces'));
53
    }
54
55
    /**
56
     * Store a newly created resource in storage.
57
     *
58
     * @param  CreateTagRequest $request
59
     * @return Response
60
     */
61
    public function store(CreateTagRequest $request)
62
    {
63
        $this->tag->create($request->all());
64
65
        return redirect()->route('admin.tag.tag.index')
66
            ->withSuccess(trans('core::core.messages.resource created', ['name' => trans('tag::tags.tags')]));
67
    }
68
69
    /**
70
     * Show the form for editing the specified resource.
71
     *
72
     * @param  Tag $tag
73
     * @return Response
74
     */
75
    public function edit(Tag $tag)
76
    {
77
        $namespaces = $this->formatNamespaces($this->tagManager->getNamespaces());
78
79
        return view('tag::admin.tags.edit', compact('tag', 'namespaces'));
80
    }
81
82
    /**
83
     * Update the specified resource in storage.
84
     *
85
     * @param  Tag $tag
86
     * @param  UpdateTagRequest $request
87
     * @return Response
88
     */
89 View Code Duplication
    public function update(Tag $tag, UpdateTagRequest $request)
90
    {
91
        $this->tag->update($tag, $request->all());
92
93
        return redirect()->route('admin.tag.tag.index')
94
            ->withSuccess(trans('core::core.messages.resource updated', ['name' => trans('tag::tags.tags')]));
95
    }
96
97
    /**
98
     * Remove the specified resource from storage.
99
     *
100
     * @param  Tag $tag
101
     * @return Response
102
     */
103 View Code Duplication
    public function destroy(Tag $tag)
104
    {
105
        $this->tag->destroy($tag);
106
107
        return redirect()->route('admin.tag.tag.index')
108
            ->withSuccess(trans('core::core.messages.resource deleted', ['name' => trans('tag::tags.tags')]));
109
    }
110
111
    private function formatNamespaces(array $namespaces)
112
    {
113
        $new = [];
114
        foreach ($namespaces as $namespace) {
115
            $new[$namespace] = $namespace;
116
        }
117
118
        return $new;
119
    }
120
}
121