• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

webeweb / edm-bundle / 4820316576

pending completion
4820316576

push

github

webeweb
Improve metrics workflow

699 of 711 relevant lines covered (98.31%)

5.91 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.95
/Controller/DocumentController.php
1
<?php
2

3
/*
4
 * This file is part of the edm-bundle package.
5
 *
6
 * (c) 2017 WEBEWEB
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
namespace WBW\Bundle\EDMBundle\Controller;
13

14
use DateTime;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\HttpFoundation\Response;
17
use Throwable;
18
use WBW\Bundle\EDMBundle\DataTables\Provider\DocumentDataTablesProvider;
19
use WBW\Bundle\EDMBundle\Entity\Document;
20
use WBW\Bundle\EDMBundle\Event\DocumentEvent;
21
use WBW\Bundle\EDMBundle\Form\Type\Document\MoveDocumentFormType;
22
use WBW\Bundle\EDMBundle\Form\Type\Document\UploadDocumentFormType;
23
use WBW\Bundle\EDMBundle\Form\Type\DocumentFormType;
24
use WBW\Bundle\EDMBundle\Model\DocumentInterface;
25
use WBW\Bundle\EDMBundle\Repository\DocumentRepository;
26
use WBW\Bundle\JQuery\DataTablesBundle\Controller\DataTablesController;
27

28
/**
29
 * Document controller.
30
 *
31
 * @author webeweb <https://github.com/webeweb>
32
 * @package WBW\Bundle\EDMBundle\Controller
33
 */
34
class DocumentController extends AbstractController {
35

36
    /**
37
     * Service name.
38
     *
39
     * @var string
40
     */
41
    const SERVICE_NAME = "wbw.edm.controller.document";
42

43
    /**
44
     * Deletes an existing document.
45
     *
46
     * @param int $id The document.
47
     * @return Response Returns the response.
48
     * @throws Throwable Throws an exception if an error occurs.
49
     */
50
    public function deleteAction(int $id): Response {
51

52
        $document = $this->findDocument($id, true);
3✔
53

54
        $type = $document->isDocument() ? "document" : "directory";
2✔
55

56
        try {
57

58
            $backedUp = clone $document; // Clone to preserve id attribute
2✔
59

60
            $this->dispatchDocumentEvent(DocumentEvent::PRE_DELETE, $document);
2✔
61

62
            $em = $this->getEntityManager();
2✔
63
            $em->remove($document);
2✔
64
            $em->flush();
1✔
65

66
            $this->dispatchDocumentEvent(DocumentEvent::POST_DELETE, $backedUp);
1✔
67

68
            $this->notifySuccess($this->translate("DocumentController.deleteAction.success.$type"));
1✔
69
        } catch (Throwable $ex) {
1✔
70

71
            $this->notifyDanger($this->translate("DocumentController.deleteAction.danger.$type"));
1✔
72
        }
73

74
        [$route, $parameters] = $this->buildRedirectRoute($document);
2✔
75

76
        return $this->redirectToRoute($route, $parameters);
2✔
77
    }
78

79
    /**
80
     * Downloads an existing document.
81
     *
82
     * @param int $id The document.
83
     * @return Response Returns the response.
84
     * @throws Throwable Throws an exception if an error occurs.
85
     */
86
    public function downloadAction(int $id): Response {
87

88
        $document = $this->findDocument($id, true);
2✔
89

90
        $event = $this->dispatchDocumentEvent(DocumentEvent::PRE_DOWNLOAD, $document);
1✔
91
        if (null === $event->getResponse()) {
1✔
92
            return new Response("Internal Server Error", 500);
×
93
        }
94

95
        return $event->getResponse();
1✔
96
    }
97

98
    /**
99
     * Displays a form to edit an existing document.
100
     *
101
     * @param Request $request The request.
102
     * @param int $id The document.
103
     * @return Response Returns the response.
104
     * @throws Throwable Throws an exception if an error occurs.
105
     */
106
    public function editAction(Request $request, int $id): Response {
107

108
        $document = $this->findDocument($id, true);
2✔
109

110
        $type = $document->isDocument() ? "document" : "directory";
1✔
111

112
        $form = $this->createForm(DocumentFormType::class, $document);
1✔
113

114
        $form->handleRequest($request);
1✔
115
        if ($form->isSubmitted() && $form->isValid()) {
1✔
116

117
            $this->dispatchDocumentEvent(DocumentEvent::PRE_EDIT, $document);
1✔
118

119
            $document->setUpdatedAt(new DateTime());
1✔
120
            $this->getEntityManager()->flush();
1✔
121

122
            $this->dispatchDocumentEvent(DocumentEvent::POST_EDIT, $document);
1✔
123

124
            $this->notifySuccess($this->translate("DocumentController.editAction.success.$type"));
1✔
125

126
            [$route, $parameters] = $this->buildRedirectRoute($document);
1✔
127
            return $this->redirectToRoute($route, $parameters);
1✔
128
        }
129

130
        return $this->render("@WBWEDM/document/form.html.twig", [
1✔
131
            "form"     => $form->createView(),
1✔
132
            "document" => $document,
1✔
133
        ]);
134
    }
135

136
    /**
137
     * Index all documents.
138
     *
139
     * @param Request $request The request.
140
     * @return Response Returns the response.
141
     */
142
    public function indexAction(Request $request): Response {
143

144
        $id = $request->attributes->get("id");
2✔
145

146
        $path  = ["name" => DocumentDataTablesProvider::DATATABLES_NAME];
2✔
147
        $query = null === $id ? [] : ["id" => $id];
2✔
148

149
        return $this->forward(DataTablesController::class . "::indexAction", $path, $query);
2✔
150
    }
151

152
    /**
153
     * Displays a form to move an existing document.
154
     *
155
     * @param Request $request The request.
156
     * @param int $id The document.
157
     * @return Response Returns the response.
158
     * @throws Throwable Throws an exception if an error occurs.
159
     */
160
    public function moveAction(Request $request, int $id): Response {
161

162
        $document = $this->findDocument($id, true);
2✔
163

164
        $except = $document->isDirectory() ? $document : $document->getParent();
1✔
165
        $type   = $document->isDocument() ? "document" : "directory";
1✔
166

167
        /** @var DocumentRepository $repository */
168
        $repository = $this->getEntityManager()->getRepository(Document::class);
1✔
169

170
        $form = $this->createForm(MoveDocumentFormType::class, $document, [
1✔
171
            "entity.parent" => $repository->findAllDirectoriesExcept($except),
1✔
172
        ]);
173

174
        $form->handleRequest($request);
1✔
175
        if ($form->isSubmitted() && $form->isValid()) {
1✔
176

177
            $this->dispatchDocumentEvent(DocumentEvent::PRE_MOVE, $document);
1✔
178

179
            $document->setUpdatedAt(new DateTime());
1✔
180
            $this->getEntityManager()->flush();
1✔
181

182
            $this->dispatchDocumentEvent(DocumentEvent::POST_MOVE, $document);
1✔
183

184
            $this->notifySuccess($this->translate("DocumentController.moveAction.success.{$type}"));
1✔
185

186
            [$route, $parameters] = $this->buildRedirectRoute($document);
1✔
187
            return $this->redirectToRoute($route, $parameters);
1✔
188
        }
189

190
        return $this->render("@WBWEDM/document/move.html.twig", [
1✔
191
            "form"     => $form->createView(),
1✔
192
            "document" => $document,
1✔
193
        ]);
194
    }
195

196
    /**
197
     * Creates a new document.
198
     *
199
     * @param Request $request The request.
200
     * @param int|null $id The parent.
201
     * @return Response Returns the response.
202
     * @throws Throwable Throws an exception if an error occurs.
203
     */
204
    public function newAction(Request $request, int $id = null): Response {
205

206
        $parent = $this->findDocument($id, false);
1✔
207

208
        $document = new Document();
1✔
209
        $document->setCreatedAt(new DateTime());
1✔
210
        $document->setParent($parent);
1✔
211
        $document->setSize(0);
1✔
212
        $document->setType(DocumentInterface::TYPE_DIRECTORY);
1✔
213

214
        $form = $this->createForm(DocumentFormType::class, $document);
1✔
215

216
        $form->handleRequest($request);
1✔
217
        if ($form->isSubmitted() && $form->isValid()) {
1✔
218

219
            $this->dispatchDocumentEvent(DocumentEvent::PRE_NEW, $document);
1✔
220

221
            $em = $this->getEntityManager();
1✔
222
            $em->persist($document);
1✔
223
            $em->flush();
1✔
224

225
            $this->dispatchDocumentEvent(DocumentEvent::POST_NEW, $document);
1✔
226

227
            $this->notifySuccess($this->translate("DocumentController.newAction.success.directory"));
1✔
228

229
            [$route, $parameters] = $this->buildRedirectRoute($document);
1✔
230
            return $this->redirectToRoute($route, $parameters);
1✔
231
        }
232

233
        return $this->render("@WBWEDM/document/form.html.twig", [
1✔
234
            "form"     => $form->createView(),
1✔
235
            "document" => $document,
1✔
236
        ]);
237
    }
238

239
    /**
240
     * Uploads a document.
241
     *
242
     * @param Request $request The request.
243
     * @param int|null $id The parent.
244
     * @return Response Returns the response.
245
     * @throws Throwable Throws an exception if an error occurs.
246
     */
247
    public function uploadAction(Request $request, int $id = null): Response {
248

249
        $parent = $this->findDocument($id, false);
1✔
250

251
        $document = new Document();
1✔
252
        $document->setCreatedAt(new DateTime());
1✔
253
        $document->setParent($parent);
1✔
254
        $document->setSize(0);
1✔
255
        $document->setType(DocumentInterface::TYPE_DOCUMENT);
1✔
256

257
        $form = $this->createForm(UploadDocumentFormType::class, $document);
1✔
258

259
        $form->handleRequest($request);
1✔
260
        if ($form->isSubmitted() && $form->isValid()) {
1✔
261

262
            $this->dispatchDocumentEvent(DocumentEvent::PRE_NEW, $document);
1✔
263

264
            $em = $this->getEntityManager();
1✔
265
            $em->persist($document);
1✔
266
            $em->flush();
1✔
267

268
            $this->dispatchDocumentEvent(DocumentEvent::POST_NEW, $document);
1✔
269

270
            $this->notifySuccess($this->translate("DocumentController.uploadAction.success.document"));
1✔
271

272
            [$route, $parameters] = $this->buildRedirectRoute($document);
1✔
273
            return $this->redirectToRoute($route, $parameters);
1✔
274
        }
275

276
        return $this->render("@WBWEDM/document/upload.html.twig", [
1✔
277
            "form"     => $form->createView(),
1✔
278
            "document" => $document,
1✔
279
        ]);
280
    }
281
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc