Completed
Push — master ( 2ae089...5f42d6 )
by Luis Ramón
13:38
created

EntryController::downloadEntryAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
/*
3
  ÁTICA - Aplicación web para la gestión documental de centros educativos
4
5
  Copyright (C) 2015-2017: Luis Ramón López López
6
7
  This program is free software: you can redistribute it and/or modify
8
  it under the terms of the GNU Affero General Public License as published by
9
  the Free Software Foundation, either version 3 of the License, or
10
  (at your option) any later version.
11
12
  This program is distributed in the hope that it will be useful,
13
  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
  GNU Affero General Public License for more details.
16
17
  You should have received a copy of the GNU Affero General Public License
18
  along with this program.  If not, see [http://www.gnu.org/licenses/].
19
*/
20
21
namespace AppBundle\Controller\Documentation;
22
23
use AppBundle\Entity\Documentation\DownloadLog;
24
use AppBundle\Entity\Documentation\Entry;
25
use AppBundle\Entity\User;
26
use AppBundle\Entity\Documentation\Version;
27
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
28
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
29
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
30
use Symfony\Component\HttpFoundation\BinaryFileResponse;
31
use Symfony\Component\HttpFoundation\Request;
32
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
33
34
class EntryController extends Controller
35
{
36
    /**
37
     * @Route("/publico/{id}/{publicToken}", name="documentation_entry_public_download", requirements={"id" = "\d+"}, methods={"GET"})
38
     * @Security("entry.isPublic()")
39
     */
40
    public function publicDownloadEntryAction(Request $request, Entry $entry)
41
    {
42
        return $this->doDownloadEntry($request, $entry, null);
43
    }
44
45
    /**
46
     * @Route("/documentos/descargar/{id}", name="documentation_entry_download", requirements={"id" = "\d+"}, methods={"GET"})
47
     * @Security("is_granted('ENTRY_ACCESS', entry)")
48
     */
49
    public function downloadEntryAction(Request $request, Entry $entry)
50
    {
51
        return $this->doDownloadEntry($request, $entry, $this->getUser());
52
    }
53
54
    /**
55
     * @param Request $request
56
     * @param Entry $entry
57
     * @param User|null $user
58
     *
59
     * @return BinaryFileResponse
60
     */
61
    private function doDownloadEntry(Request $request, Entry $entry, User $user = null)
62
    {
63
        $version = $entry->getCurrentVersion();
64
        if (null === $version || null === $version->getFile()) {
65
            throw $this->createNotFoundException();
66
        }
67
        return $this->doDownloadVersion($request, $version, $user);
68
    }
69
70
    /**
71
     * @param Request $request
72
     * @param Version $version
73
     * @param User|null $user
74
     *
75
     * @return BinaryFileResponse
76
     */
77
    private function doDownloadVersion(Request $request, Version $version, User $user = null)
78
    {
79
        $filepath = 'gaufrette://entries/' . $version->getFile();
80
81
        $response = new BinaryFileResponse($filepath);
82
83
        $response
84
            ->setContentDisposition(
85
                ResponseHeaderBag::DISPOSITION_INLINE,
86
                $version->getEntry()->getName()
87
            );
88
89
        $em = $this->getDoctrine()->getManager();
90
91
        $log = new DownloadLog();
92
        $log
93
            ->setUser($user)
94
            ->setVersion($version->getVersionNr())
95
            ->setEntry($version->getEntry())
96
            ->setIpAddress($request->getClientIp());
97
98
        $em->persist($log);
99
100
        $em->flush();
101
102
        return $response;
103
    }
104
}
105