Passed
Push — master ( eca6f3...bde58b )
by Fabien
04:26
created

DefaultController::initialization()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 13
nc 6
nop 2
dl 0
loc 23
rs 8.7972
c 0
b 0
f 0
ccs 16
cts 16
cp 1
crap 4
1
<?php
2
3
/*
4
 * This file is part of the FabienCrassat\CurriculumVitaeBundle Symfony bundle.
5
 *
6
 * (c) Fabien Crassat <[email protected]>
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 FabienCrassat\CurriculumVitaeBundle\Controller;
13
14
use FabienCrassat\CurriculumVitaeBundle\Entity\CurriculumVitae;
15
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
16
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
17
use Symfony\Component\HttpKernel\HttpKernelInterface;
18
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
19
use Symfony\Component\HttpFoundation\Request;
20
use Symfony\Component\HttpFoundation\Response;
21
use Symfony\Component\HttpFoundation\RedirectResponse;
22
use Symfony\Component\Serializer\Serializer;
23
use Symfony\Component\Serializer\Encoder\XmlEncoder;
24
use Symfony\Component\Serializer\Encoder\JsonEncoder;
25
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
26
27
class DefaultController implements ContainerAwareInterface
28
{
29
    use ContainerAwareTrait;
30
31
    private $cvxmlfile;
32
    private $pathToFile;
33
    private $lang;
34
    private $curriculumVitae;
35
    private $exposedLanguages;
36
    private $requestFormat;
37
    private $parameters = [];
38
39
    /**
40
     * @return Response
41
     */
42 4
    public function indexAction($cvxmlfile = NULL)
43
    {
44 3
        if ($cvxmlfile) {
45
            $path = [
46 2
                '_controller' => 'FabienCrassatCurriculumVitaeBundle:Default:display',
47 2
                'cvxmlfile'   => $cvxmlfile,
48 2
                '_locale'     => $this->lang,
49 2
            ];
50
51 2
            $request    = $this->container->get('request');
52 2
            $subRequest = $request->duplicate([], NULL, $path);
53
54 2
            $httpKernel = $this->container->get('http_kernel');
55
56 2
            return $httpKernel->handle(
57 2
                $subRequest,
58
                HttpKernelInterface::SUB_REQUEST
59 4
            );
60
        }
61
62 1
        $this->initialization($cvxmlfile);
63 1
        return new RedirectResponse($this->container->get('router')->generate(
64 1
            'fabiencrassat_curriculumvitae_cvxmlfileonly',
65 1
            ['cvxmlfile' => $this->cvxmlfile]),
66 4
            301);
67
    }
68
69
    /**
70
     * @return Response
71
     */
72 7
    public function displayAction($cvxmlfile, $_locale, Request $request)
73
    {
74 7
        $this->initialization($cvxmlfile, $_locale);
75 5
        $this->requestFormat = $request->getRequestFormat();
76 5
        $this->setViewParameters();
77
78 5
        switch ($this->requestFormat) {
79 5
            case 'json':
80 1
                return new Response(json_encode($this->parameters));
81 4
            case 'xml':
82
                //initialisation du serializer
83 1
                $encoders    = [new XmlEncoder('CurriculumVitae'), new JsonEncoder()];
84 1
                $normalizers = [new GetSetMethodNormalizer()];
85 1
                $serializer  = new Serializer($normalizers, $encoders);
86
87 1
                $response = new Response();
88 1
                $response->setContent($serializer->serialize($this->parameters, 'xml'));
89 1
                $response->headers->set('Content-Type', 'application/xml');
90
91 1
                return $response;
92 3
            default:
93 3
                return $this->container->get('templating')->renderResponse(
94 3
                    $this->container->getParameter('fabiencrassat_curriculumvitae.template'),
95 3
                    $this->parameters);
96 3
        }
97
    }
98
99
    /**
100
     * @return Response
101
     */
102 1
    public function exportPDFAction($cvxmlfile, $_locale)
103
    {
104 1
        if (!$this->hasExportPDF()) {
105 1
            throw new NotFoundHttpException('No export PDF service installed.');
106
        }
107
108
        $this->initialization($cvxmlfile, $_locale);
109
        $this->setViewParameters();
110
111
        $html     = $this->container->get('templating')->render(
112
                    'FabienCrassatCurriculumVitaeBundle:CurriculumVitae:index.pdf.twig',
113
                    $this->parameters);
114
        $filename = $this->curriculumVitae->getHumanFileName().'.pdf';
115
116
        $hasPdfService = false;
117
        $content       = '';
118
        if (!$hasPdfService && $this->container->has('a5sys_pdf.pdf_service')) {
0 ignored issues
show
introduced by
The condition $hasPdfService is always false.
Loading history...
119
            $hasPdfService = true;
120
            $content       = $this->container->get('a5sys_pdf.pdf_service')->sendPDF($html, $filename);
121
        }
122
        if (!$hasPdfService && $this->container->has('knp_snappy.pdf')) {
123
            $content = $this->container->get('knp_snappy.pdf')->getOutputFromHtml($html);
124
        }
125
126
        return new Response($content, 200,
127
            ['Content-Type'       => 'application/pdf',
128
            'Content-Disposition' => 'attachment; filename="'.$filename.'"']
129
        );
130
    }
131
132 8
    private function initialization($file = NULL, $lang = NULL)
133
    {
134 8
        $this->cvxmlfile = $file;
135 8
        if (!$this->cvxmlfile) {
136
            // Retreive the CV file depending the configuration
137 1
            $this->cvxmlfile = $this->container->getParameter('fabiencrassat_curriculumvitae.default_cv');
138 1
        }
139
        // Check the file in the filesystem
140 8
        $this->pathToFile =
141 8
            $this->container->getParameter('fabiencrassat_curriculumvitae.path_to_cv')
142 8
            .'/'.$this->cvxmlfile.'.xml';
143
144 8
        if (!is_file($this->pathToFile)) {
145 1
            throw new NotFoundHttpException(
146 1
                'There is no curriculum vitae file defined for '.$this->cvxmlfile.' ('.$this->pathToFile.').');
147
        }
148
149 7
        $this->lang = $lang;
150 7
        if (!$this->lang) {
151 2
            $this->lang = $this->container->getParameter('fabiencrassat_curriculumvitae.default_lang');
152 2
        }
153
154 7
        $this->readCVFile();
155 6
    }
156
157 7
    private function readCVFile() {
158
        // Read the Curriculum Vitae
159 7
        $this->curriculumVitae = new CurriculumVitae($this->pathToFile, $this->lang);
160
161
        // Check if there is at least 1 language defined
162 7
        $this->exposedLanguages = $this->curriculumVitae->getDropDownLanguages();
163 7
        if (is_array($this->exposedLanguages) && !array_key_exists($this->lang, $this->exposedLanguages)) {
164 1
            throw new NotFoundHttpException('There is no curriculum vitae defined for the language '.$this->lang);
165
        }
166 6
    }
167
168 5
    private function setViewParameters()
169
    {
170 5
        if ($this->requestFormat != 'json' && $this->requestFormat != 'xml') {
171 3
            $this->setToolParameters();
172 3
        }
173 5
        $this->setParameters($this->curriculumVitae->getCurriculumVitaeArray());
174 5
    }
175
176 4
    private function hasExportPDF()
177
    {
178 4
        return $this->container->has('knp_snappy.pdf') xor $this->container->has('a5sys_pdf.pdf_service');
179
    }
180
181 3
    private function hasSecureDisplayBundle()
182
    {
183 3
        return $this->container->has('netinfluence.twig.secure_display_extension');
184
    }
185
186 3
    private function setToolParameters()
187
    {
188 3
        $this->setParameters([
189 3
            'cvxmlfile'              => $this->cvxmlfile,
190 3
            'languageView'           => $this->lang,
191 3
            'languages'              => $this->exposedLanguages,
192 3
            'anchors'                => $this->curriculumVitae->getAnchors(),
193 3
            'hasExportPDF'           => $this->hasExportPDF(),
194 3
            'hasSecureDisplayBundle' => $this->hasSecureDisplayBundle()
195 3
        ]);
196 3
    }
197
198
    /**
199
     * @param array $parametersToAdd
200
     */
201 5
    private function setParameters(array $parametersToAdd)
202
    {
203 5
        $this->parameters = array_merge($this->parameters, $parametersToAdd);
204 5
    }
205
}
206