Completed
Push — master ( 9e7f7b...705fa0 )
by Fabien
04:07
created

DefaultController::readCVFile()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 8
Bugs 0 Features 0
Metric Value
c 8
b 0
f 0
dl 0
loc 12
ccs 8
cts 8
cp 1
rs 9.4285
cc 3
eloc 6
nc 3
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 $ReadCVXml;
35
    private $exposedLanguages;
36
    private $requestFormat;
37
    private $CVVariables = array();
38
39 3
    public function indexAction($cvxmlfile = NULL)
40
    {
41 3
        if($cvxmlfile) {
42
            $path = array(
43 2
                '_controller' => 'FabienCrassatCurriculumVitaeBundle:Default:display',
44 2
                'cvxmlfile'   => $cvxmlfile,
45 2
                '_locale'     => $this->lang,
46 2
            );
47 2
            $request = $this->container->get('request');
48 2
            $subRequest = $request->duplicate(array(), NULL, $path);
49
50 2
            $httpKernel = $this->container->get('http_kernel');
51 2
            $response = $httpKernel->handle(
52 2
                $subRequest,
53
                HttpKernelInterface::SUB_REQUEST
54 2
            );
55 2
            return $response;
56
        } else {
57 1
            $this->initialization($cvxmlfile);
58 1
            return new RedirectResponse($this->container->get('router')->generate(
59 1
                'fabiencrassat_curriculumvitae_cvxmlfileonly',
60
                array(
61 1
                    'cvxmlfile'   => $this->cvxmlfile,
62 1
                )), 301);
63
        }
64
    }
65
66 7
    public function displayAction($cvxmlfile, $_locale, Request $request)
67
    {
68 7
        $this->initialization($cvxmlfile, $_locale);
69 5
        $this->requestFormat = $request->getRequestFormat();
70 5
        $this->defineCVViewVariables();
71
72 5
        switch ($this->requestFormat) {
73 5
            case 'json':
74 1
                return new Response(json_encode($this->CVVariables));
75 4
            case 'xml':
76
                //initialisation du serializer
77 1
                $encoders = array(new XmlEncoder('CurriculumVitae'), new JsonEncoder());
78 1
                $normalizers = array(new GetSetMethodNormalizer());
79 1
                $serializer = new Serializer($normalizers, $encoders);
80
81 1
                $response = new Response();
82 1
                $response->setContent($serializer->serialize($this->CVVariables, 'xml'));
83 1
                $response->headers->set('Content-Type', 'application/xml');
84
85 1
                return $response;
86 3
            default:
87 4
                return $this->container->get('templating')->renderResponse(
88 4
                $this->container->getParameter('fabiencrassat_curriculumvitae.template'), $this->CVVariables);
89 3
        }
90
    }
91
92 1
    public function exportPDFAction($cvxmlfile, $_locale)
93
    {
94 1
        $this->initialization($cvxmlfile, $_locale);
95 1
        $this->defineCVViewVariables();
96
97 1
        if (!$this->container->has('knp_snappy.pdf')) {
98 1
            throw new NotFoundHttpException('knp_snappy.pdf is non-existent');
99
        };
100
101
        $html = $this->container->get('templating')->render("FabienCrassatCurriculumVitaeBundle:CurriculumVitae:index.pdf.twig", $this->CVVariables);
102
103
        return new Response($this->container->get('knp_snappy.pdf')->getOutputFromHtml($html),
104
            200,
105
            array('Content-Type'        => 'application/pdf',
106
                  'Content-Disposition' => 'attachment; filename="'.$this->ReadCVXml->getHumanFileName().'.pdf"')
107
        );
108
    }
109
110 9
    private function initialization($file = NULL, $_locale = NULL)
111
    {
112 9
        $this->cvxmlfile = $file;
113 9
        if (!$this->cvxmlfile) {
114
            // Retreive the CV file depending the configuration
115 1
            $this->cvxmlfile = $this->container->getParameter('fabiencrassat_curriculumvitae.default_cv');
116 1
        }
117
        // Check the file in the filesystem
118 9
        $this->pathToFile = $this->container->getParameter('fabiencrassat_curriculumvitae.path_to_cv').'/'.$this->cvxmlfile.'.xml';
119 9
        if (!is_file($this->pathToFile)) {
120 1
            throw new NotFoundHttpException('There is no curriculum vitae file defined for '.$this->cvxmlfile.' ('.$this->pathToFile.').');
121
        }
122 8
        $this->lang = $_locale;
123 8
        if (!$this->lang) {
124 2
            $this->lang = $this->container->getParameter('fabiencrassat_curriculumvitae.default_lang');
125 2
        }
126 8
        $this->readCVFile();
127 7
    }
128
129 8
    private function readCVFile() {
130
        // Read the Curriculum Vitae
131 8
        $this->ReadCVXml = new CurriculumVitae($this->pathToFile, $this->lang);
132
133
        // Check if there is at least 1 language defined
134 8
        $this->exposedLanguages = $this->ReadCVXml->getDropDownLanguages();
135 8
        if(is_array($this->exposedLanguages)) {
136 8
            if (!array_key_exists($this->lang, $this->exposedLanguages)) {
137 1
                throw new NotFoundHttpException('There is no curriculum vitae defined for the language '.$this->lang);
138
            }
139 7
        }
140 7
    }
141
142 6
    private function defineCVViewVariables()
143
    {
144 6
        if ($this->requestFormat != 'json' && $this->requestFormat != 'xml') {
145 4
            $this->setToolCVVariables();
146 4
        }
147 6
        $this->setCoreCVVariables();
148 6
    }
149
150 4
    private function setToolCVVariables()
151
    {
152 4
        $this->setCVVariables(array(
153 4
            'cvxmlfile'    => $this->cvxmlfile,
154 4
            'languageView' => $this->lang,
155 4
            'languages'    => $this->exposedLanguages,
156 4
            'anchors'      => $this->ReadCVXml->getAnchors(),
157 4
            'hasSnappyPDF' => $this->container->has('knp_snappy.pdf'),
158 4
        ));
159 4
    }
160
161 6
    private function setCoreCVVariables()
162
    {
163 6
        $this->setCVVariables(array(
164 6
            'identity'          => $this->ReadCVXml->getIdentity(),
165 6
            'followMe'          => $this->ReadCVXml->getFollowMe(),
166 6
            'lookingFor'        => $this->ReadCVXml->getLookingFor(),
167 6
            'experiences'       => $this->ReadCVXml->getExperiences(),
168 6
            'skills'            => $this->ReadCVXml->getSkills(),
169 6
            'educations'        => $this->ReadCVXml->getEducations(),
170 6
            'languageSkills'    => $this->ReadCVXml->getLanguageSkills(),
171 6
            'miscellaneous'     => $this->ReadCVXml->getMiscellaneous(),
172 6
        ));
173 6
    }
174
175
    /**
176
     * @param array $newCVVariables
177
     */
178 6
    private function setCVVariables(array $newCVVariables)
179
    {
180 6
        $this->CVVariables = array_merge($this->CVVariables, $newCVVariables);
181 6
    }
182
}
183