Passed
Push — master ( 5c444f...454f16 )
by Fabien
03:16
created

DefaultController   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 181
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 12

Test Coverage

Coverage 82.41%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 25
c 2
b 0
f 0
lcom 1
cbo 12
dl 0
loc 181
ccs 89
cts 108
cp 0.8241
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
B indexAction() 0 26 2
B displayAction() 0 26 3
B exportPDFAction() 0 29 6
B initialization() 0 24 4
A readCVFile() 0 12 3
A setViewParameters() 0 7 3
A hasExportPDF() 0 4 1
A hasSecureDisplayBundle() 0 4 1
A setToolParameters() 0 11 1
A setParameters() 0 4 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 = [];
38
39
    /**
40
     * @return Response
41
     */
42 4
    public function indexAction($cvxmlfile = NULL)
43
    {
44 3
        if ($cvxmlfile) {
45
            $path = [
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([], 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 1
            ['cvxmlfile' => $this->cvxmlfile]),
66 4
            301);
67
    }
68
69
    /**
70
     * @return Response
71
     */
72 7
    public function displayAction($cvxmlfile, $_locale, Request $request)
0 ignored issues
show
Coding Style introduced by
$_locale does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
Coding Style Naming introduced by
The parameter $_locale is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
73
    {
74 7
        $this->initialization($cvxmlfile, $_locale);
75 5
        $this->requestFormat = $request->getRequestFormat();
76 5
        $this->setViewParameters();
77
78 5
        switch ($this->requestFormat) {
79 5
            case 'json':
80 1
                return new Response(json_encode($this->parameters));
81 4
            case 'xml':
82
                //initialisation du serializer
83 1
                $encoders    = [new XmlEncoder('CurriculumVitae'), new JsonEncoder()];
84 1
                $normalizers = [new GetSetMethodNormalizer()];
85 1
                $serializer  = new Serializer($normalizers, $encoders);
86
87 1
                $response = new Response();
88 1
                $response->setContent($serializer->serialize($this->parameters, 'xml'));
89 1
                $response->headers->set('Content-Type', 'application/xml');
90
91 1
                return $response;
92 3
            default:
93 3
                return $this->container->get('templating')->renderResponse(
94 3
                    $this->container->getParameter('fabiencrassat_curriculumvitae.template'),
95 3
                    $this->parameters);
96 3
        }
97
    }
98
99
    /**
100
     * @return Response
101
     */
102 1
    public function exportPDFAction($cvxmlfile, $_locale)
0 ignored issues
show
Coding Style introduced by
$_locale does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
Coding Style Naming introduced by
The parameter $_locale is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
103
    {
104 1
        if (!$this->hasExportPDF()) {
105 1
            throw new NotFoundHttpException('No export PDF service installed.');
106
        }
107
108
        $this->initialization($cvxmlfile, $_locale);
109
        $this->setViewParameters();
110
111
        $html     = $this->container->get('templating')->render(
112
                    'FabienCrassatCurriculumVitaeBundle:CurriculumVitae:index.pdf.twig',
113
                    $this->parameters);
114
        $filename = $this->curriculumVitae->getHumanFileName().'.pdf';
115
116
        $hasPdfService = false;
117
        $content       = '';
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
            $content = $this->container->get('knp_snappy.pdf')->getOutputFromHtml($html);
124
        };
125
126
        return new Response($content, 200,
127
            ['Content-Type'       => 'application/pdf',
128
            'Content-Disposition' => 'attachment; filename="'.$filename.'"']
129
        );
130
    }
131
132 8
    private function initialization($file = NULL, $lang = NULL)
133
    {
134 8
        $this->cvxmlfile = $file;
135 8
        if (!$this->cvxmlfile) {
136
            // Retreive the CV file depending the configuration
137 1
            $this->cvxmlfile = $this->container->getParameter('fabiencrassat_curriculumvitae.default_cv');
138 1
        }
139
        // Check the file in the filesystem
140 8
        $this->pathToFile =
141 8
            $this->container->getParameter('fabiencrassat_curriculumvitae.path_to_cv')
142 8
            .'/'.$this->cvxmlfile.'.xml';
143
144 8
        if (!is_file($this->pathToFile)) {
145 1
            throw new NotFoundHttpException(
146 1
                'There is no curriculum vitae file defined for '.$this->cvxmlfile.' ('.$this->pathToFile.').');
147
        }
148
149 7
        $this->lang = $lang;
150 7
        if (!$this->lang) {
151 2
            $this->lang = $this->container->getParameter('fabiencrassat_curriculumvitae.default_lang');
152 2
        }
153
154 7
        $this->readCVFile();
155 6
    }
156
157 7
    private function readCVFile() {
158
        // Read the Curriculum Vitae
159 7
        $this->curriculumVitae = new CurriculumVitae($this->pathToFile, $this->lang);
160
161
        // Check if there is at least 1 language defined
162 7
        $this->exposedLanguages = $this->curriculumVitae->getDropDownLanguages();
163 7
        if (is_array($this->exposedLanguages)) {
164 7
            if (!array_key_exists($this->lang, $this->exposedLanguages)) {
165 1
                throw new NotFoundHttpException('There is no curriculum vitae defined for the language '.$this->lang);
166
            }
167 6
        }
168 6
    }
169
170 5
    private function setViewParameters()
171
    {
172 5
        if ($this->requestFormat != 'json' && $this->requestFormat != 'xml') {
173 3
            $this->setToolParameters();
174 3
        }
175 5
        $this->setParameters($this->curriculumVitae->getCurriculumVitaeArray());
176 5
    }
177
178 4
    private function hasExportPDF()
179
    {
180 4
        return $this->container->has('knp_snappy.pdf') xor $this->container->has('a5sys_pdf.pdf_service');
181
    }
182
183 3
    private function hasSecureDisplayBundle()
184
    {
185 3
        return $this->container->has('netinfluence.twig.secure_display_extension');
186
    }
187
188 3
    private function setToolParameters()
189
    {
190 3
        $this->setParameters([
191 3
            'cvxmlfile'              => $this->cvxmlfile,
192 3
            'languageView'           => $this->lang,
193 3
            'languages'              => $this->exposedLanguages,
194 3
            'anchors'                => $this->curriculumVitae->getAnchors(),
195 3
            'hasExportPDF'           => $this->hasExportPDF(),
196 3
            'hasSecureDisplayBundle' => $this->hasSecureDisplayBundle()
197 3
        ]);
198 3
    }
199
200
    /**
201
     * @param array $parametersToAdd
202
     */
203 5
    private function setParameters(array $parametersToAdd)
204
    {
205 5
        $this->parameters = array_merge($this->parameters, $parametersToAdd);
206 5
    }
207
}
208