Completed
Pull Request — master (#14)
by Fabien
15:46 queued 12:42
created

DefaultController::hasSecureDisplayBundle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
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
        $content = "";
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
Coding Style Comprehensibility introduced by
The string literal does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
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->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 hasSecureDisplayBundle()
186
    {
187 3
        return $this->container->has('netinfluence.twig.secure_display_extension');
188 3
    }
189 3
190 3
    private function setToolParameters()
191 3
    {
192 3
        $this->setParameters(array(
193 3
            'cvxmlfile'              => $this->cvxmlfile,
194 3
            'languageView'           => $this->lang,
195
            'languages'              => $this->exposedLanguages,
196 5
            'anchors'                => $this->curriculumVitae->getAnchors(),
197
            'hasExportPDF'           => $this->hasExportPDF(),
198 5
            'hasSecureDisplayBundle' => $this->hasSecureDisplayBundle(),
199 5
        ));
200 5
    }
201 5
202 5
    private function setCoreParameters()
203 5
    {
204 5
        $this->setParameters(array(
205 5
            'identity'          => $this->curriculumVitae->getIdentity(),
206 5
            'followMe'          => $this->curriculumVitae->getFollowMe(),
207 5
            'lookingFor'        => $this->curriculumVitae->getLookingFor(),
208 5
            'experiences'       => $this->curriculumVitae->getExperiences(),
209
            'skills'            => $this->curriculumVitae->getSkills(),
210
            'educations'        => $this->curriculumVitae->getEducations(),
211
            'languageSkills'    => $this->curriculumVitae->getLanguageSkills(),
212
            'miscellaneous'     => $this->curriculumVitae->getMiscellaneous(),
213 5
        ));
214
    }
215 5
216 5
    /**
217
     * @param array $parametersToAdd
218
     */
219
    private function setParameters(array $parametersToAdd)
220
    {
221
        $this->parameters = array_merge($this->parameters, $parametersToAdd);
222
    }
223
}
224