Passed
Push — master ( 98e928...797c29 )
by Fabien
02:18
created

DefaultController::hasExportPDF()   A

Complexity

Conditions 1
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 2
nop 0
crap 1
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 = array();
38
39
    /**
40
     * @return Response
41
     */
42 4
    public function indexAction($cvxmlfile = NULL)
43
    {
44 3
        if($cvxmlfile) {
45
            $path = array(
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(array(), NULL, $path);
53
54 2
            $httpKernel = $this->container->get('http_kernel');
55 2
            $response   = $httpKernel->handle(
56 2
                $subRequest,
57
                HttpKernelInterface::SUB_REQUEST
58 2
            );
59 4
            return $response;
60
        }
61
62 1
        $this->initialization($cvxmlfile);
63 1
        return new RedirectResponse($this->container->get('router')->generate(
64 1
            'fabiencrassat_curriculumvitae_cvxmlfileonly',
65
            array(
66 4
                'cvxmlfile'   => $this->cvxmlfile,
67 1
            )), 301);
68
    }
69
70
    /**
71
     * @return Response
72
     */
73 7
    public function displayAction($cvxmlfile, $_locale, Request $request)
74
    {
75 7
        $this->initialization($cvxmlfile, $_locale);
76 5
        $this->requestFormat = $request->getRequestFormat();
77 5
        $this->setViewParameters();
78
79 5
        switch ($this->requestFormat) {
80 5
            case 'json':
81 1
                return new Response(json_encode($this->parameters));
82 4
            case 'xml':
83
                //initialisation du serializer
84 1
                $encoders    = array(new XmlEncoder('CurriculumVitae'), new JsonEncoder());
85 1
                $normalizers = array(new GetSetMethodNormalizer());
86 1
                $serializer  = new Serializer($normalizers, $encoders);
87
88 1
                $response = new Response();
89 1
                $response->setContent($serializer->serialize($this->parameters, 'xml'));
90 1
                $response->headers->set('Content-Type', 'application/xml');
91
92 1
                return $response;
93 3
            default:
94 3
                return $this->container->get('templating')->renderResponse(
95 3
                    $this->container->getParameter('fabiencrassat_curriculumvitae.template'),
96 3
                    $this->parameters);
97 3
        }
98
    }
99
100
    /**
101
     * @return Response
102
     */
103 1
    public function exportPDFAction($cvxmlfile, $_locale)
104
    {
105 1
        if (!$this->hasExportPDF()) {
106 1
            throw new NotFoundHttpException('No export PDF service installed.');
107
        }
108
109
        $this->initialization($cvxmlfile, $_locale);
110
        $this->setViewParameters();
111
112
        $html = $this->container->get('templating')->render(
113
            'FabienCrassatCurriculumVitaeBundle:CurriculumVitae:index.pdf.twig',
114
            $this->parameters);
115
        $filename = $this->curriculumVitae->getHumanFileName().'.pdf';
116
117
        $hasPdfService = false;
118
        if (!$hasPdfService && $this->container->has('a5sys_pdf.pdf_service')) {
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
            $hasPdfService = true;
124
            $content = $this->container->get('knp_snappy.pdf')->getOutputFromHtml($html);
125
        };
126
127
        return new Response($content,
128
            200,
129
            array('Content-Type'        => 'application/pdf',
130
                  'Content-Disposition' => 'attachment; filename="'.$filename.'"')
131
        );
132
    }
133
134 8
    private function initialization($file = NULL, $lang = NULL)
135
    {
136 8
        $this->cvxmlfile = $file;
137 8
        if (!$this->cvxmlfile) {
138
            // Retreive the CV file depending the configuration
139 1
            $this->cvxmlfile = $this->container->getParameter('fabiencrassat_curriculumvitae.default_cv');
140 1
        }
141
        // Check the file in the filesystem
142 8
        $this->pathToFile =
143 8
            $this->container->getParameter('fabiencrassat_curriculumvitae.path_to_cv')
144 8
            .'/'.$this->cvxmlfile.'.xml';
145
146 8
        if (!is_file($this->pathToFile)) {
147 1
            throw new NotFoundHttpException(
148 1
                'There is no curriculum vitae file defined for '.$this->cvxmlfile.' ('.$this->pathToFile.').');
149
        }
150
151 7
        $this->lang = $lang;
152 7
        if (!$this->lang) {
153 2
            $this->lang = $this->container->getParameter('fabiencrassat_curriculumvitae.default_lang');
154 2
        }
155
156 7
        $this->readCVFile();
157 6
    }
158
159 7
    private function readCVFile() {
160
        // Read the Curriculum Vitae
161 7
        $this->curriculumVitae = new CurriculumVitae($this->pathToFile, $this->lang);
162
163
        // Check if there is at least 1 language defined
164 7
        $this->exposedLanguages = $this->curriculumVitae->getDropDownLanguages();
165 7
        if(is_array($this->exposedLanguages)) {
166 7
            if (!array_key_exists($this->lang, $this->exposedLanguages)) {
167 1
                throw new NotFoundHttpException('There is no curriculum vitae defined for the language '.$this->lang);
168
            }
169 6
        }
170 6
    }
171
172 5
    private function setViewParameters()
173
    {
174 5
        if ($this->requestFormat != 'json' && $this->requestFormat != 'xml') {
175 3
            $this->setToolParameters();
176 3
        }
177 5
        $this->setCoreParameters();
178 5
    }
179
180 4
    private function hasExportPDF()
181
    {
182 4
        return $this->container->has('knp_snappy.pdf') xor $this->container->has('a5sys_pdf.pdf_service');
183
    }
184
185 3
    private function setToolParameters()
186
    {
187 3
        $this->setParameters(array(
188 3
            'cvxmlfile'    => $this->cvxmlfile,
189 3
            'languageView' => $this->lang,
190 3
            'languages'    => $this->exposedLanguages,
191 3
            'anchors'      => $this->curriculumVitae->getAnchors(),
192 3
            'hasExportPDF' => $this->hasExportPDF(),
193 3
        ));
194 3
    }
195
196 5
    private function setCoreParameters()
197
    {
198 5
        $this->setParameters(array(
199 5
            'identity'          => $this->curriculumVitae->getIdentity(),
200 5
            'followMe'          => $this->curriculumVitae->getFollowMe(),
201 5
            'lookingFor'        => $this->curriculumVitae->getLookingFor(),
202 5
            'experiences'       => $this->curriculumVitae->getExperiences(),
203 5
            'skills'            => $this->curriculumVitae->getSkills(),
204 5
            'educations'        => $this->curriculumVitae->getEducations(),
205 5
            'languageSkills'    => $this->curriculumVitae->getLanguageSkills(),
206 5
            'miscellaneous'     => $this->curriculumVitae->getMiscellaneous(),
207 5
        ));
208 5
    }
209
210
    /**
211
     * @param array $parametersToAdd
212
     */
213 5
    private function setParameters(array $parametersToAdd)
214
    {
215 5
        $this->parameters = array_merge($this->parameters, $parametersToAdd);
216 5
    }
217
}
218