Completed
Push — master ( fc2141...f0457d )
by Davide
10:25 queued 08:14
created

CodiceFiscaleControllerProvider   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 158
Duplicated Lines 15.19 %

Coupling/Cohesion

Components 0
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 7
c 3
b 1
f 0
lcom 0
cbo 8
dl 24
loc 158
ccs 107
cts 107
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B connect() 24 132 5
A generateResponseFromErrors() 0 15 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace DavidePastore\CodiceFiscaleRest;
4
5
use CodiceFiscale\Calculator;
6
use CodiceFiscale\Checker;
7
use CodiceFiscale\Subject;
8
use Silex\Application;
9
use Silex\ControllerProviderInterface;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpFoundation\Response;
12
use Symfony\Component\HttpFoundation\JsonResponse;
13
use Symfony\Component\PropertyAccess\PropertyAccess;
14
use Symfony\Component\Validator\Constraints as Assert;
15
16
class CodiceFiscaleControllerProvider implements ControllerProviderInterface
17
{
18 13
    public function connect(Application $app)
19
    {
20 13
        $api = $app['controllers_factory'];
21
22
        //Calculate the codice fiscale by the given parameters.
23
        $api->get('/calculate', function (Application $app, Request $request) {
24 5
            $constraint = new Assert\Collection(array(
25 5
                'name' => $app['codice-fiscale-rest.constraints']['name'],
26 5
                'surname' => $app['codice-fiscale-rest.constraints']['surname'],
27 5
                'gender' => $app['codice-fiscale-rest.constraints']['gender'],
28 5
                'birthDate' => $app['codice-fiscale-rest.constraints']['birthDate'],
29 5
                'belfioreCode' => $app['codice-fiscale-rest.constraints']['belfioreCode'],
30 5
                'omocodiaLevel' => $app['codice-fiscale-rest.constraints']['omocodiaLevel'],
31 5
            ));
32
33
            $data = array(
34 5
                'name' => $request->get('name', ''),
35 5
                'surname' => $request->get('surname', ''),
36 5
                'gender' => $request->get('gender', ''),
37 5
                'birthDate' => $request->get('birthDate', ''),
38 5
                'belfioreCode' => $request->get('belfioreCode', ''),
39 5
                'omocodiaLevel' => $request->get('omocodiaLevel', 0),
40 5
            );
41
42 5
            $errors = $app['validator']->validate($data, $constraint);
43 5
            $response = new \stdClass();
44
45 5 View Code Duplication
            if (count($errors) > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
46 4
                $response = $this->generateResponseFromErrors($errors);
47 4
            } else {
48 1
                $subject = new Subject($data);
49
50 1
                $calculator = new Calculator($subject, array(
51 1
                    'omocodiaLevel' => $data['omocodiaLevel'],
52 1
                ));
53 1
                $codiceFiscale = $calculator->calculate();
54
55 1
                $response->status = true;
56 1
                $response->codiceFiscale = $codiceFiscale;
57
            }
58
59 5
            return new JsonResponse($response);
60 13
        })
61 13
        ->bind('apiCalculate');
62
63
        //Calculate all the codice fiscale by the given parameters.
64
        $api->get('/calculateAll', function (Application $app, Request $request) {
65 4
            $constraint = new Assert\Collection(array(
66 4
                'name' => $app['codice-fiscale-rest.constraints']['name'],
67 4
                'surname' => $app['codice-fiscale-rest.constraints']['surname'],
68 4
                'gender' => $app['codice-fiscale-rest.constraints']['gender'],
69 4
                'birthDate' => $app['codice-fiscale-rest.constraints']['birthDate'],
70 4
                'belfioreCode' => $app['codice-fiscale-rest.constraints']['belfioreCode'],
71 4
            ));
72
73
            $data = array(
74 4
                'name' => $request->get('name', ''),
75 4
                'surname' => $request->get('surname', ''),
76 4
                'gender' => $request->get('gender', ''),
77 4
                'birthDate' => $request->get('birthDate', ''),
78 4
                'belfioreCode' => $request->get('belfioreCode', ''),
79 4
            );
80
81 4
            $errors = $app['validator']->validate($data, $constraint);
82 4
            $response = new \stdClass();
83
84 4 View Code Duplication
            if (count($errors) > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
85 3
                $response = $this->generateResponseFromErrors($errors);
86 3
            } else {
87 1
                $subject = new Subject($data);
88
89 1
                $calculator = new Calculator($subject);
90 1
                $codiciFiscali = $calculator->calculateAllPossibilities();
91
92 1
                $response->status = true;
93 1
                $response->codiciFiscali = $codiciFiscali;
94
            }
95
96 4
            return new JsonResponse($response);
97 13
        })
98 13
        ->bind('apiCalculateAll');
99
100
        //Check if the given parameters are ok for the given parameters.
101 13
        $api->get('/check', function (Application $app, Request $request) {
102 4
            $constraint = new Assert\Collection(array(
103 4
                'name' => $app['codice-fiscale-rest.constraints']['name'],
104 4
                'surname' => $app['codice-fiscale-rest.constraints']['surname'],
105 4
                'gender' => $app['codice-fiscale-rest.constraints']['gender'],
106 4
                'birthDate' => $app['codice-fiscale-rest.constraints']['birthDate'],
107 4
                'belfioreCode' => $app['codice-fiscale-rest.constraints']['belfioreCode'],
108 4
                'codiceFiscale' => $app['codice-fiscale-rest.constraints']['codiceFiscale'],
109 4
                'omocodiaLevel' => $app['codice-fiscale-rest.constraints']['omocodiaLevel'],
110 4
            ));
111
112
            $data = array(
113 4
                'name' => $request->get('name', ''),
114 4
                'surname' => $request->get('surname', ''),
115 4
                'gender' => $request->get('gender', ''),
116 4
                'birthDate' => $request->get('birthDate', ''),
117 4
                'belfioreCode' => $request->get('belfioreCode', ''),
118 4
                'codiceFiscale' => $request->get('codiceFiscale', ''),
119 4
                'omocodiaLevel' => $request->get('omocodiaLevel', Checker::ALL_OMOCODIA_LEVELS),
120 4
            );
121
122 4
            $errors = $app['validator']->validate($data, $constraint);
123 4
            $response = new \stdClass();
124
125 4
            if (count($errors) > 0) {
126 1
                $response = $this->generateResponseFromErrors($errors);
127 1
            } else {
128 3
                $subject = new Subject($data);
129
130 3
                $checker = new Checker($subject, array(
131 3
                  'codiceFiscaleToCheck' => $data['codiceFiscale'],
132 3
                  'omocodiaLevel' => $data['omocodiaLevel'],
133 3
                ));
134
135 3
                if ($checker->check()) {
136 2
                    $response->status = true;
137 2
                    $response->message = 'Valid codice fiscale';
138 2
                } else {
139 1
                    $response->status = false;
140 1
                    $response->message = 'Invalid codice fiscale';
141
                }
142
            }
143
144 4
            return new JsonResponse($response);
145 13
        })
146 13
        ->bind('apiCheck');
147
148 13
        return $api;
149
    }
150
151
    /**
152
     * Generate response from the given ConstraintViolationList.
153
     *
154
     * @param $errors The ConstraintViolationList instance.
155
     *
156
     * @return Returns the response from the given errors.
157
     */
158 8
    private function generateResponseFromErrors(\Symfony\Component\Validator\ConstraintViolationList $errors)
159 1
    {
160 8
        $response = new \stdclass();
161 8
        $response->status = false;
162 8
        $accessor = PropertyAccess::createPropertyAccessor();
163 8
        $responseErrors = array();
164
165 8
        foreach ($errors as $error) {
166 8
            $accessor->setValue($responseErrors, $error->getPropertyPath(), $error->getMessage());
167 8
        }
168
169 8
        $response->errors = $responseErrors;
170
171 8
        return $response;
172
    }
173
}
174