Passed
Push — master ( d25d54...3efdf7 )
by Fabien
02:59
created

DefaultController::setCoreParameters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 13
Ratio 100 %

Code Coverage

Tests 12
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 13
loc 13
ccs 12
cts 12
cp 1
rs 9.4285
cc 1
eloc 10
nc 1
nop 0
crap 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A DefaultController::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 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)
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...
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)
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...
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       = '';
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->setParameters($this->curriculumVitae->getCurriculumViateArray());
0 ignored issues
show
Bug introduced by
It seems like $this->curriculumVitae->getCurriculumViateArray() targeting FabienCrassat\Curriculum...tCurriculumViateArray() can also be of type null; however, FabienCrassat\Curriculum...roller::setParameters() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
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
    }
189
190 3
    private function setToolParameters()
191
    {
192 3
        $this->setParameters(array(
193 3
            'cvxmlfile'              => $this->cvxmlfile,
194 3
            'languageView'           => $this->lang,
195 3
            'languages'              => $this->exposedLanguages,
196 3
            'anchors'                => $this->curriculumVitae->getAnchors(),
197 3
            'hasExportPDF'           => $this->hasExportPDF(),
198 3
            'hasSecureDisplayBundle' => $this->hasSecureDisplayBundle(),
199 3
        ));
200 3
    }
201
202
    /**
203
     * @param array $parametersToAdd
204
     */
205 5
    private function setParameters(array $parametersToAdd)
206
    {
207 5
        $this->parameters = array_merge($this->parameters, $parametersToAdd);
208 5
    }
209
}
210