Completed
Pull Request — master (#11)
by Fabien
05:38 queued 02:48
created

DefaultController::defineCVViewVariables()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 9.4285
cc 3
eloc 4
nc 2
nop 0
crap 3
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 5
    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 5
            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 5
                '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 1
    public function exportPDFAction($cvxmlfile, $_locale)
101
    {
102 1
        $this->initialization($cvxmlfile, $_locale);
103 1
        $this->setViewParameters();
104
105 1
        if (!$this->container->has('knp_snappy.pdf')) {
106 1
            throw new NotFoundHttpException('knp_snappy.pdf is non-existent');
107
        };
108
109
        $html = $this->container->get('templating')->render(
110
            'FabienCrassatCurriculumVitaeBundle:CurriculumVitae:index.pdf.twig',
111
            $this->parameters);
112
113
        return new Response($this->container->get('knp_snappy.pdf')->getOutputFromHtml($html),
114
            200,
115
            array('Content-Type'        => 'application/pdf',
116
                  'Content-Disposition' => 'attachment; filename="'.$this->curriculumVitae->getHumanFileName().'.pdf"')
117
        );
118
    }
119
120 9
    private function initialization($file = NULL, $lang = NULL)
121
    {
122 9
        $this->cvxmlfile = $file;
123 9
        if (!$this->cvxmlfile) {
124
            // Retreive the CV file depending the configuration
125 1
            $this->cvxmlfile = $this->container->getParameter('fabiencrassat_curriculumvitae.default_cv');
126 1
        }
127
        // Check the file in the filesystem
128 9
        $this->pathToFile = 
129 9
            $this->container->getParameter('fabiencrassat_curriculumvitae.path_to_cv')
130 9
            .'/'.$this->cvxmlfile.'.xml';
131
        
132 9
        if (!is_file($this->pathToFile)) {
133 1
            throw new NotFoundHttpException(
134 1
                'There is no curriculum vitae file defined for '.$this->cvxmlfile.' ('.$this->pathToFile.').');
135
        }
136
137 8
        $this->lang = $lang;
138 8
        if (!$this->lang) {
139 2
            $this->lang = $this->container->getParameter('fabiencrassat_curriculumvitae.default_lang');
140 2
        }
141
        
142 8
        $this->readCVFile();
143 7
    }
144
145 8
    private function readCVFile() {
146
        // Read the Curriculum Vitae
147 8
        $this->curriculumVitae = new CurriculumVitae($this->pathToFile, $this->lang);
148
149
        // Check if there is at least 1 language defined
150 8
        $this->exposedLanguages = $this->curriculumVitae->getDropDownLanguages();
151 8
        if(is_array($this->exposedLanguages)) {
152 8
            if (!array_key_exists($this->lang, $this->exposedLanguages)) {
153 1
                throw new NotFoundHttpException('There is no curriculum vitae defined for the language '.$this->lang);
154
            }
155 7
        }
156 7
    }
157
158 6
    private function setViewParameters()
159
    {
160 6
        if ($this->requestFormat != 'json' && $this->requestFormat != 'xml') {
161 4
            $this->setToolParameters();
162 4
        }
163 6
        $this->setCoreParameters();
164 6
    }
165
166 4
    private function setToolParameters()
167
    {
168 4
        $this->setParameters(array(
169 4
            'cvxmlfile'    => $this->cvxmlfile,
170 4
            'languageView' => $this->lang,
171 4
            'languages'    => $this->exposedLanguages,
172 4
            'anchors'      => $this->curriculumVitae->getAnchors(),
173 4
            'hasSnappyPDF' => $this->container->has('knp_snappy.pdf'),
174 4
        ));
175 4
    }
176
177 6
    private function setCoreParameters()
178
    {
179 6
        $this->setParameters(array(
180 6
            'identity'          => $this->curriculumVitae->getIdentity(),
181 6
            'followMe'          => $this->curriculumVitae->getFollowMe(),
182 6
            'lookingFor'        => $this->curriculumVitae->getLookingFor(),
183 6
            'experiences'       => $this->curriculumVitae->getExperiences(),
184 6
            'skills'            => $this->curriculumVitae->getSkills(),
185 6
            'educations'        => $this->curriculumVitae->getEducations(),
186 6
            'languageSkills'    => $this->curriculumVitae->getLanguageSkills(),
187 6
            'miscellaneous'     => $this->curriculumVitae->getMiscellaneous(),
188 6
        ));
189 6
    }
190
191
    /**
192
     * @param array $parametersToAdd
193
     */
194 6
    private function setParameters(array $parametersToAdd)
195
    {
196 6
        $this->parameters = array_merge($this->parameters, $parametersToAdd);
197 6
    }
198
}
199