DocumentsController   B
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 16

Importance

Changes 0
Metric Value
wmc 9
lcom 2
cbo 16
dl 0
loc 112
c 0
b 0
f 0
rs 8.4614

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A index() 0 8 1
A store() 0 7 1
A show() 0 11 1
A destroy() 0 8 1
A byTag() 0 8 1
A edit() 0 6 1
A update() 0 9 1
A listTags() 0 5 1
1
<?php
2
3
/**
4
 * Storgman - Student Organizations Management
5
 * Copyright (C) 2014-2016, Dejan Angelov <[email protected]>
6
 *
7
 * This file is part of Storgman.
8
 *
9
 * Storgman is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation, either version 3 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * Storgman is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with Storgman.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 * @package Storgman
23
 * @copyright Copyright (C) 2014-2016, Dejan Angelov <[email protected]>
24
 * @license https://github.com/angelov/storgman/blob/master/LICENSE
25
 * @author Dejan Angelov <[email protected]>
26
 */
27
28
namespace Angelov\Storgman\Documents\Http\Controllers;
29
30
use Angelov\Storgman\Core\Http\Controllers\BaseController;
31
use Angelov\Storgman\Documents\Commands\DeleteDocumentCommand;
32
use Angelov\Storgman\Documents\Commands\StoreDocumentCommand;
33
use Angelov\Storgman\Documents\Commands\UpdateDocumentCommand;
34
use Angelov\Storgman\Documents\Events\DocumentWasOpened;
35
use Angelov\Storgman\Documents\Http\Requests\StoreDocumentRequest;
36
use Angelov\Storgman\Documents\DocumentsPaginator;
37
use Angelov\Storgman\Documents\Repositories\DocumentsRepositoryInterface;
38
use Angelov\Storgman\Documents\Tags\Repositories\TagsRepositoryInterface;
39
use Angelov\Storgman\Members\Member;
40
use Illuminate\Contracts\Auth\Guard;
41
use Illuminate\Contracts\Events\Dispatcher as EventsDispatcher;
42
use Illuminate\Contracts\View\View;
43
use Illuminate\Http\JsonResponse;
44
use Illuminate\Http\RedirectResponse;
45
use Illuminate\Http\Request;
46
47
class DocumentsController extends BaseController
48
{
49
    protected $documents;
50
    protected $tags;
51
    protected $events;
52
53
    public function __construct(
54
        DocumentsRepositoryInterface $documents,
55
        TagsRepositoryInterface $tags,
56
        EventsDispatcher $events
57
    ) {
58
        $this->documents = $documents;
59
        $this->tags = $tags;
60
        $this->events = $events;
61
    }
62
63
    /**
64
     * List the submitted documents
65
     *
66
     * @param Request $request
67
     * @param DocumentsPaginator $paginator
68
     * @return View
69
     */
70
    public function index(Request $request, DocumentsPaginator $paginator)
71
    {
72
        $page = $request->get('page', 1);
73
        $documents = $paginator->get($page, $with = ['submitter', 'openedBy', 'tags']);
74
        $tags = $this->tags->all($with = ['documents']);
75
76
        return view('documents.index', compact('documents', 'tags'));
77
    }
78
79
    /**
80
     * Store a new document
81
     *
82
     * @param StoreDocumentRequest $request
83
     * @return View
84
     */
85
    public function store(StoreDocumentRequest $request)
86
    {
87
        $data = $request->all();
88
        $document = dispatch(new StoreDocumentCommand($data));
89
90
        return view('documents.components.document', compact('document'));
91
    }
92
93
    /**
94
     * Redirect the user to the document's url
95
     *
96
     * @param Guard $authenticator
97
     * @param int $id
98
     * @return RedirectResponse
99
     */
100
    public function show(Guard $authenticator, $id)
101
    {
102
        $document = $this->documents->get($id);
103
104
        /** @var Member $member */
105
        $member = $authenticator->user();
106
107
        $this->events->fire(new DocumentWasOpened($document, $member));
108
109
        return redirect()->to($document->getUrl(), 301);
110
    }
111
112
    /**
113
     * Delete the specific document
114
     *
115
     * @param $id
116
     * @return JsonResponse
117
     */
118
    public function destroy($id)
119
    {
120
        // @todo Check if the member is allowed to delete the document
121
122
        dispatch(new DeleteDocumentCommand($id));
123
124
        return $this->successfulJsonResponse("Document deleted successfully.");
125
    }
126
127
    public function byTag(TagsRepositoryInterface $tags, $id)
128
    {
129
        $tag = $tags->get($id);
130
        $documents = $tag->getDocuments();
131
        $tags = $tags->all($with = ['documents']);
132
133
        return view('documents.by-tag', compact('tag', 'documents', 'tags'));
134
    }
135
136
    public function edit($id)
137
    {
138
        $document = $this->documents->get($id);
139
140
        return view('documents.modals.edit-document', compact('document'));
141
    }
142
143
    public function update(StoreDocumentRequest $request, $id)
144
    {
145
        $data = $request->all();
146
        $data['id'] = $id;
147
148
        dispatch(new UpdateDocumentCommand($id, $data));
149
150
        return $this->successfulJsonResponse("Document updated successfully.", ['document' => $data]);
151
    }
152
153
    public function listTags(TagsRepositoryInterface $tags)
154
    {
155
        $all = $tags->all();
156
        return new JsonResponse($all);
157
    }
158
}
159