Passed
Push — Codacy ( 230dad...27b2c4 )
by Fabien
02:35
created

DefaultController::initialization()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 4

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 22
ccs 17
cts 17
cp 1
rs 8.9197
cc 4
eloc 14
nc 6
nop 2
crap 4
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 5
    public function indexAction($cvxmlfile = NULL)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
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
48 2
            $request    = $this->container->get('request');
49 2
            $subRequest = $request->duplicate(array(), NULL, $path);
50
51 2
            $httpKernel = $this->container->get('http_kernel');
52 2
            $response   = $httpKernel->handle(
53 2
                $subRequest,
54
                HttpKernelInterface::SUB_REQUEST
55 2
            );
56 2
            return $response;
57
        }
58
        
59 5
        $this->initialization($cvxmlfile);
60 1
        return new RedirectResponse($this->container->get('router')->generate(
61 1
            'fabiencrassat_curriculumvitae_cvxmlfileonly',
62
            array(
63 1
                'cvxmlfile'   => $this->cvxmlfile,
64 1
            )), 301);
65
    }
66
67 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...
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

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...
68
    {
69 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...
70 5
        $this->requestFormat = $request->getRequestFormat();
71 5
        $this->setViewParameters();
72
73 5
        switch ($this->requestFormat) {
74 5
            case 'json':
75 1
                return new Response(json_encode($this->parameters));
76 4
            case 'xml':
77
                //initialisation du serializer
78 1
                $encoders    = array(new XmlEncoder('CurriculumVitae'), new JsonEncoder());
79 1
                $normalizers = array(new GetSetMethodNormalizer());
80 1
                $serializer  = new Serializer($normalizers, $encoders);
81
82 1
                $response = new Response();
83 1
                $response->setContent($serializer->serialize($this->parameters, 'xml'));
84 1
                $response->headers->set('Content-Type', 'application/xml');
85
86 1
                return $response;
87 4
            default:
88 4
                return $this->container->get('templating')->renderResponse(
89 3
                $this->container->getParameter('fabiencrassat_curriculumvitae.template'), $this->parameters);
90 3
        }
91
    }
92
93 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...
94
    {
95 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...
96 1
        $this->setViewParameters();
97
98 1
        if (!$this->container->has('knp_snappy.pdf')) {
99 1
            throw new NotFoundHttpException('knp_snappy.pdf is non-existent');
100
        };
101
102
        $html = $this->container->get('templating')->render(
103
            'FabienCrassatCurriculumVitaeBundle:CurriculumVitae:index.pdf.twig',
104
            $this->parameters);
105
106
        return new Response($this->container->get('knp_snappy.pdf')->getOutputFromHtml($html),
107
            200,
108
            array('Content-Type'        => 'application/pdf',
109
                  'Content-Disposition' => 'attachment; filename="'.$this->curriculumVitae->getHumanFileName().'.pdf"')
110
        );
111
    }
112
113 9
    private function initialization($file = NULL, $_locale = NULL)
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...
114
    {
115 9
        $this->cvxmlfile = $file;
116 9
        if (!$this->cvxmlfile) {
117
            // Retreive the CV file depending the configuration
118 1
            $this->cvxmlfile = $this->container->getParameter('fabiencrassat_curriculumvitae.default_cv');
119 1
        }
120
        // Check the file in the filesystem
121 9
        $this->pathToFile = 
122 9
            $this->container->getParameter('fabiencrassat_curriculumvitae.path_to_cv')
123 9
            .'/'.$this->cvxmlfile.'.xml';
124
        
125 9
        if (!is_file($this->pathToFile)) {
126 1
            throw new NotFoundHttpException(
127 1
                'There is no curriculum vitae file defined for '.$this->cvxmlfile.' ('.$this->pathToFile.').');
128
        }
129 8
        $this->lang = $_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...
130 8
        if (!$this->lang) {
131 2
            $this->lang = $this->container->getParameter('fabiencrassat_curriculumvitae.default_lang');
132 2
        }
133 8
        $this->readCVFile();
134 7
    }
135
136 8
    private function readCVFile() {
137
        // Read the Curriculum Vitae
138 8
        $this->curriculumVitae = new CurriculumVitae($this->pathToFile, $this->lang);
139
140
        // Check if there is at least 1 language defined
141 8
        $this->exposedLanguages = $this->curriculumVitae->getDropDownLanguages();
142 8
        if(is_array($this->exposedLanguages)) {
143 8
            if (!array_key_exists($this->lang, $this->exposedLanguages)) {
144 1
                throw new NotFoundHttpException('There is no curriculum vitae defined for the language '.$this->lang);
145
            }
146 7
        }
147 7
    }
148
149 6
    private function setViewParameters()
150
    {
151 6
        if ($this->requestFormat != 'json' && $this->requestFormat != 'xml') {
152 4
            $this->setToolParameters();
153 4
        }
154 6
        $this->setCoreParameters();
155 6
    }
156
157 4
    private function setToolParameters()
158
    {
159 4
        $this->setParameters(array(
160 4
            'cvxmlfile'    => $this->cvxmlfile,
161 4
            'languageView' => $this->lang,
162 4
            'languages'    => $this->exposedLanguages,
163 4
            'anchors'      => $this->curriculumVitae->getAnchors(),
164 4
            'hasSnappyPDF' => $this->container->has('knp_snappy.pdf'),
165 4
        ));
166 4
    }
167
168 6
    private function setCoreParameters()
169
    {
170 6
        $this->setParameters(array(
171 6
            'identity'          => $this->curriculumVitae->getIdentity(),
172 6
            'followMe'          => $this->curriculumVitae->getFollowMe(),
173 6
            'lookingFor'        => $this->curriculumVitae->getLookingFor(),
174 6
            'experiences'       => $this->curriculumVitae->getExperiences(),
175 6
            'skills'            => $this->curriculumVitae->getSkills(),
176 6
            'educations'        => $this->curriculumVitae->getEducations(),
177 6
            'languageSkills'    => $this->curriculumVitae->getLanguageSkills(),
178 6
            'miscellaneous'     => $this->curriculumVitae->getMiscellaneous(),
179 6
        ));
180 6
    }
181
182
    /**
183
     * @param array $parametersToAdd
184
     */
185 6
    private function setParameters(array $parametersToAdd)
186
    {
187 6
        $this->parameters = array_merge($this->parameters, $parametersToAdd);
188 6
    }
189
}
190