These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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 | $content = ''; |
||
119 | if (!$hasPdfService && $this->container->has('a5sys_pdf.pdf_service')) { |
||
120 | $hasPdfService = true; |
||
121 | $content = $this->container->get('a5sys_pdf.pdf_service')->sendPDF($html, $filename); |
||
122 | }; |
||
123 | if (!$hasPdfService && $this->container->has('knp_snappy.pdf')) { |
||
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->setParameters($this->curriculumVitae->getCurriculumViateArray()); |
|
0 ignored issues
–
show
|
|||
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 hasSecureDisplayBundle() |
|
186 | { |
||
187 | 3 | return $this->container->has('netinfluence.twig.secure_display_extension'); |
|
188 | } |
||
189 | |||
190 | 3 | private function setToolParameters() |
|
191 | { |
||
192 | 3 | $this->setParameters(array( |
|
193 | 3 | 'cvxmlfile' => $this->cvxmlfile, |
|
194 | 3 | 'languageView' => $this->lang, |
|
195 | 3 | 'languages' => $this->exposedLanguages, |
|
196 | 3 | 'anchors' => $this->curriculumVitae->getAnchors(), |
|
197 | 3 | 'hasExportPDF' => $this->hasExportPDF(), |
|
198 | 3 | 'hasSecureDisplayBundle' => $this->hasSecureDisplayBundle(), |
|
199 | 3 | )); |
|
200 | 3 | } |
|
201 | |||
202 | /** |
||
203 | * @param array $parametersToAdd |
||
204 | */ |
||
205 | 5 | private function setParameters(array $parametersToAdd) |
|
206 | { |
||
207 | 5 | $this->parameters = array_merge($this->parameters, $parametersToAdd); |
|
208 | 5 | } |
|
209 | } |
||
210 |
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.