Passed
Push — feature/#95-files-suite ( e0433c...d39b8d )
by FX
04:00
created

SuiteController::docAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 13
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 19
rs 9.4285
1
<?php
2
/**
3
 * Copyright (c) 2017 Francois-Xavier Soubirou.
4
 *
5
 * This file is part of ci-report.
6
 *
7
 * ci-report is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU 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
 * ci-report 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 General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with ci-report. If not, see <http://www.gnu.org/licenses/>.
19
 */
20
declare(strict_types=1);
21
22
namespace AppBundle\Controller;
23
24
use AppBundle\Entity\Campaign;
25
use AppBundle\Entity\Project;
26
use AppBundle\Entity\Suite;
27
use AppBundle\Entity\Test;
28
use AppBundle\Service\DocumentStorageService;
29
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
30
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
31
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
32
use Symfony\Component\HttpFoundation\BinaryFileResponse;
33
use Symfony\Component\HttpFoundation\Response;
34
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
35
36
/**
37
 * Suite controller class.
38
 *
39
 * @category  ci-report app
40
 *
41
 * @author    Francois-Xavier Soubirou <[email protected]>
42
 * @copyright 2017 Francois-Xavier Soubirou
43
 * @license   http://www.gnu.org/licenses/   GPLv3
44
 *
45
 * @see      https://www.ci-report.io
46
 */
47
class SuiteController extends Controller
48
{
49
    /**
50
     * Index action.
51
     *
52
     * @param Project  $project  Project
53
     * @param Campaign $campaign Campaign
54
     * @param Suite    $suite    The suite to display
55
     *
56
     * @return Response A Response instance
57
     *
58
     * @Route(
59
     *    "/project/{prefid}/campaign/{crefid}/suite/{srefid}",
60
     *    requirements={"crefid" = "\d+", "srefid" = "\d+"},
61
     *    name="suite-view"
62
     * )
63
     *
64
     * @ParamConverter("project", options={"mapping": {"prefid": "refid"}})
65
     * @ParamConverter("campaign", class="AppBundle:Campaign", options={
66
     *    "repository_method" = "findCampaignByProjectRefidAndRefid",
67
     *    "mapping": {"prefid": "prefid", "crefid": "crefid"},
68
     *    "map_method_signature" = true
69
     * })
70
     * @ParamConverter("suite", class="AppBundle:Suite", options={
71
     *    "repository_method" = "findSuiteByProjectRefidCampaignRefidAndRefid",
72
     *    "mapping": {"prefid": "prefid", "crefid": "crefid", "srefid": "srefid"},
73
     *    "map_method_signature" = true
74
     * })
75
     */
76
    public function indexAction(Project $project, Campaign $campaign, Suite $suite): Response
77
    {
78
        $prevSuite = $this->getDoctrine()
79
            ->getRepository(Suite::class)
80
            ->findPrevSuiteByCampaign($campaign, $suite);
81
        $nextSuite = $this->getDoctrine()
82
            ->getRepository(Suite::class)
83
            ->findNextSuiteByCampaign($campaign, $suite);
84
85
        $testsList = $this->getDoctrine()
86
            ->getRepository(Test::class)
87
            ->findTestsBySuite($suite);
88
89
        return $this->render(
90
            'suite/view.html.twig',
91
            array(
92
                'project' => $project,
93
                'campaign' => $campaign,
94
                'suite' => $suite,
95
                'prevSuite' => $prevSuite,
96
                'nextSuite' => $nextSuite,
97
                'tests' => $testsList,
98
            )
99
        );
100
    }
101
102
    /**
103
     * Download document action.
104
     *
105
     * @param Project  $project  Project
106
     * @param Campaign $campaign Campaign
107
     * @param Suite    $suite    The suite to display
108
     *
109
     * @return BinaryFileResponse The file to download
110
     *
111
     * @Route(
112
     *    "/project/{prefid}/campaign/{crefid}/suite/{srefid}/doc",
113
     *    requirements={"crefid" = "\d+", "srefid" = "\d+"},
114
     *    name="suite-doc"
115
     * )
116
     *
117
     * @ParamConverter("project", options={"mapping": {"prefid": "refid"}})
118
     * @ParamConverter("campaign", class="AppBundle:Campaign", options={
119
     *    "repository_method" = "findCampaignByProjectRefidAndRefid",
120
     *    "mapping": {"prefid": "prefid", "crefid": "crefid"},
121
     *    "map_method_signature" = true
122
     * })
123
     * @ParamConverter("suite", class="AppBundle:Suite", options={
124
     *    "repository_method" = "findSuiteByProjectRefidCampaignRefidAndRefid",
125
     *    "mapping": {"prefid": "prefid", "crefid": "crefid", "srefid": "srefid"},
126
     *    "map_method_signature" = true
127
     * })
128
     */
129
    public function docAction(Project $project, Campaign $campaign, Suite $suite): BinaryFileResponse
130
    {
131
        if (null === $suite->getDocumentUid()) {
132
            throw $this->createNotFoundException('The zip document does not exist');
133
        }
134
        $docStoreService = $this->get(DocumentStorageService::class);
135
        $filePath = $docStoreService->getFullPath(
136
            $project,
137
            $campaign,
138
            $suite->getDocumentUid()
139
        );
140
        $response = new BinaryFileResponse($filePath);
141
        $response->setContentDisposition(
142
            ResponseHeaderBag::DISPOSITION_ATTACHMENT,
143
            'documents.zip'
144
        );
145
        $response->headers->set('Content-Type', 'application/zip');
146
147
        return $response;
148
    }
149
}
150