Passed
Push — Codacy ( 27b2c4...212f48 )
by Fabien
02:40
created

DefaultController   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 172
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 12

Test Coverage

Coverage 92.45%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 20
c 2
b 0
f 0
lcom 1
cbo 12
dl 0
loc 172
ccs 98
cts 106
cp 0.9245
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
B indexAction() 0 27 2
B displayAction() 0 26 3
A exportPDFAction() 0 19 2
B initialization() 0 24 4
A readCVFile() 0 12 3
A setViewParameters() 0 7 3
A setToolParameters() 0 10 1
A setCoreParameters() 0 13 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 = 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)
2 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...
74
    {
75 7
        $this->initialization($cvxmlfile, $_locale);
1 ignored issue
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...
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)
2 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...
101
    {
102 1
        $this->initialization($cvxmlfile, $_locale);
1 ignored issue
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...
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