Passed
Push — Codacy ( 03f7d9...230dad )
by Fabien
02:39
created

DefaultController::setCVVariables()   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 1
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 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('FabienCrassatCurriculumVitaeBundle:CurriculumVitae:index.pdf.twig', $this->parameters);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 148 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
103
104
        return new Response($this->container->get('knp_snappy.pdf')->getOutputFromHtml($html),
105
            200,
106
            array('Content-Type'        => 'application/pdf',
107
                  'Content-Disposition' => 'attachment; filename="'.$this->curriculumVitae->getHumanFileName().'.pdf"')
108
        );
109
    }
110
111 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...
112
    {
113 9
        $this->cvxmlfile = $file;
114 9
        if (!$this->cvxmlfile) {
115
            // Retreive the CV file depending the configuration
116 1
            $this->cvxmlfile = $this->container->getParameter('fabiencrassat_curriculumvitae.default_cv');
117 1
        }
118
        // Check the file in the filesystem
119 9
        $this->pathToFile = $this->container->getParameter('fabiencrassat_curriculumvitae.path_to_cv').'/'.$this->cvxmlfile.'.xml';
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 131 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
120 9
        if (!is_file($this->pathToFile)) {
121 1
            throw new NotFoundHttpException('There is no curriculum vitae file defined for '.$this->cvxmlfile.' ('.$this->pathToFile.').');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 139 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
122
        }
123 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...
124 8
        if (!$this->lang) {
125 2
            $this->lang = $this->container->getParameter('fabiencrassat_curriculumvitae.default_lang');
126 2
        }
127 8
        $this->readCVFile();
128 7
    }
129
130 8
    private function readCVFile() {
131
        // Read the Curriculum Vitae
132 8
        $this->curriculumVitae = new CurriculumVitae($this->pathToFile, $this->lang);
133
134
        // Check if there is at least 1 language defined
135 8
        $this->exposedLanguages = $this->curriculumVitae->getDropDownLanguages();
136 8
        if(is_array($this->exposedLanguages)) {
137 8
            if (!array_key_exists($this->lang, $this->exposedLanguages)) {
138 1
                throw new NotFoundHttpException('There is no curriculum vitae defined for the language '.$this->lang);
139
            }
140 7
        }
141 7
    }
142
143 6
    private function setViewParameters()
144
    {
145 6
        if ($this->requestFormat != 'json' && $this->requestFormat != 'xml') {
146 4
            $this->setToolParameters();
147 4
        }
148 6
        $this->setCoreParameters();
149 6
    }
150
151 4
    private function setToolParameters()
152
    {
153 4
        $this->setParameters(array(
154 4
            'cvxmlfile'    => $this->cvxmlfile,
155 4
            'languageView' => $this->lang,
156 4
            'languages'    => $this->exposedLanguages,
157 4
            'anchors'      => $this->curriculumVitae->getAnchors(),
158 4
            'hasSnappyPDF' => $this->container->has('knp_snappy.pdf'),
159 4
        ));
160 4
    }
161
162 6
    private function setCoreParameters()
163
    {
164 6
        $this->setParameters(array(
165 6
            'identity'          => $this->curriculumVitae->getIdentity(),
166 6
            'followMe'          => $this->curriculumVitae->getFollowMe(),
167 6
            'lookingFor'        => $this->curriculumVitae->getLookingFor(),
168 6
            'experiences'       => $this->curriculumVitae->getExperiences(),
169 6
            'skills'            => $this->curriculumVitae->getSkills(),
170 6
            'educations'        => $this->curriculumVitae->getEducations(),
171 6
            'languageSkills'    => $this->curriculumVitae->getLanguageSkills(),
172 6
            'miscellaneous'     => $this->curriculumVitae->getMiscellaneous(),
173 6
        ));
174 6
    }
175
176
    /**
177
     * @param array $parametersToAdd
178
     */
179 6
    private function setParameters(array $parametersToAdd)
180
    {
181 6
        $this->parameters = array_merge($this->parameters, $parametersToAdd);
182 6
    }
183
}
184