Completed
Push — master ( 6eaec3...369134 )
by Davide
02:11
created

CodiceFiscaleControllerProvider   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 160
Duplicated Lines 25 %

Coupling/Cohesion

Components 0
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 0
cbo 8
dl 40
loc 160
ccs 114
cts 114
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C connect() 40 157 8

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
namespace DavidePastore\CodiceFiscaleRest;
3
4
use DavidePastore\CodiceFiscale\Calculator;
5
use DavidePastore\CodiceFiscale\Checker;
6
use DavidePastore\CodiceFiscale\Subject;
7
use Silex\Application;
8
use Silex\ControllerProviderInterface;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\HttpFoundation\Response;
11
use Symfony\Component\HttpFoundation\JsonResponse;
12
use Symfony\Component\PropertyAccess\PropertyAccess;
13
use Symfony\Component\Validator\Constraints as Assert;
14
15
16
class CodiceFiscaleControllerProvider implements ControllerProviderInterface
17
{
18 12
    public function connect(Application $app)
19
    {
20 12
        $api = $app['controllers_factory'];
21
22
        //Calculate the codice fiscale by the given parameters.
23
        $api->get('/calculate', function (Application $app, Request $request) {
24 4
            $constraint = new Assert\Collection(array(
25 4
                'name' => $app['codice-fiscale-rest.constraints']['name'],
26 4
                'surname' => $app['codice-fiscale-rest.constraints']['surname'],
27 4
                'gender' => $app['codice-fiscale-rest.constraints']['gender'],
28 4
                'birthDate' => $app['codice-fiscale-rest.constraints']['birthDate'],
29 4
                'belfioreCode' => $app['codice-fiscale-rest.constraints']['belfioreCode'],
30 4
                'omocodiaLevel' => $app['codice-fiscale-rest.constraints']['omocodiaLevel']
31 4
            ));
32
            
33
            $data = array(
34 4
                'name' => $request->get('name', ''),
35 4
                'surname' => $request->get('surname', ''),
36 4
                'gender' => $request->get('gender', ''),
37 4
                'birthDate' => $request->get('birthDate', ''),
38 4
                'belfioreCode' => $request->get('belfioreCode', ''),
39 4
                'omocodiaLevel' => $request->get('omocodiaLevel', 0),
40 4
            );
41
            
42 4
            $errors = $app['validator']->validate($data, $constraint);
43 4
            $response = new \stdClass;
44
            
45 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...
46 3
                $response->status = false;
47 3
                $accessor = PropertyAccess::createPropertyAccessor();
48 3
                $responseErrors = array();
49
                
50 3
                foreach ($errors as $error) {
51 3
                    $accessor->setValue($responseErrors, $error->getPropertyPath(), $error->getMessage());
52 3
                }
53
                
54 3
                $response->errors = $responseErrors;
55 3
            } else {
56 1
                $subject = new Subject($data);
57
58 1
                $calculator = new Calculator($subject, array(
59 1
                    'omocodiaLevel' => $data['omocodiaLevel']
60 1
                ));
61 1
                $codiceFiscale = $calculator->calculate();
62
                
63 1
                $response->status = true;
64 1
                $response->codiceFiscale = $codiceFiscale;
65
            }
66 4
            return new JsonResponse($response);
67 12
        })
68 12
        ->bind('apiCalculate');
69
70
71
        //Calculate all the codice fiscale by the given parameters.
72
        $api->get('/calculateAll', function (Application $app, Request $request) {
73 4
            $constraint = new Assert\Collection(array(
74 4
                'name' => $app['codice-fiscale-rest.constraints']['name'],
75 4
                'surname' => $app['codice-fiscale-rest.constraints']['surname'],
76 4
                'gender' => $app['codice-fiscale-rest.constraints']['gender'],
77 4
                'birthDate' => $app['codice-fiscale-rest.constraints']['birthDate'],
78 4
                'belfioreCode' => $app['codice-fiscale-rest.constraints']['belfioreCode']
79 4
            ));
80
            
81
            $data = array(
82 4
                'name' => $request->get('name', ''),
83 4
                'surname' => $request->get('surname', ''),
84 4
                'gender' => $request->get('gender', ''),
85 4
                'birthDate' => $request->get('birthDate', ''),
86 4
                'belfioreCode' => $request->get('belfioreCode', '')
87 4
            );
88
            
89 4
            $errors = $app['validator']->validate($data, $constraint);
90 4
            $response = new \stdClass;
91
            
92 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...
93 3
                $response->status = false;
94 3
                $accessor = PropertyAccess::createPropertyAccessor();
95 3
                $responseErrors = array();
96
                
97 3
                foreach ($errors as $error) {
98 3
                    $accessor->setValue($responseErrors, $error->getPropertyPath(), $error->getMessage());
99 3
                }
100
                
101 3
                $response->errors = $responseErrors;
102 3
            } else {
103 1
                $subject = new Subject($data);
104
105 1
                $calculator = new Calculator($subject);
106 1
                $codiciFiscali = $calculator->calculateAllPossibilities();
107
                
108 1
                $response->status = true;
109 1
                $response->codiciFiscali = $codiciFiscali;
110
            }
111
            
112 4
            return new JsonResponse($response);
113 12
        })
114 12
        ->bind('apiCalculateAll');
115
116
        //Check if the given parameters are ok for the given parameters.
117 12
        $api->get('/check', function (Application $app, Request $request) {
118 4
            $constraint = new Assert\Collection(array(
119 4
                'name' => $app['codice-fiscale-rest.constraints']['name'],
120 4
                'surname' => $app['codice-fiscale-rest.constraints']['surname'],
121 4
                'gender' => $app['codice-fiscale-rest.constraints']['gender'],
122 4
                'birthDate' => $app['codice-fiscale-rest.constraints']['birthDate'],
123 4
                'belfioreCode' => $app['codice-fiscale-rest.constraints']['belfioreCode'],
124 4
                'codiceFiscale' => $app['codice-fiscale-rest.constraints']['codiceFiscale'],
125 4
                'omocodiaLevel' => $app['codice-fiscale-rest.constraints']['omocodiaLevel'],
126 4
            ));
127
            
128
            $data = array(
129 4
                'name' => $request->get('name', ''),
130 4
                'surname' => $request->get('surname', ''),
131 4
                'gender' => $request->get('gender', ''),
132 4
                'birthDate' => $request->get('birthDate', ''),
133 4
                'belfioreCode' => $request->get('belfioreCode', ''),
134 4
                'codiceFiscale' => $request->get('codiceFiscale', ''),
135 4
                'omocodiaLevel' => $request->get('omocodiaLevel', Checker::ALL_OMOCODIA_LEVELS)
136 4
            );
137
            
138 4
            $errors = $app['validator']->validate($data, $constraint);
139 4
            $response = new \stdClass;
140
            
141 4
            if (count($errors) > 0) {
142 1
                $response->status = false;
143 1
                $accessor = PropertyAccess::createPropertyAccessor();
144 1
                $responseErrors = array();
145
                
146 1
                foreach ($errors as $error) {
147 1
                    $accessor->setValue($responseErrors, $error->getPropertyPath(), $error->getMessage());
148 1
                }
149
                
150 1
                $response->errors = $responseErrors;
151 1
            } else {
152 3
                $subject = new Subject($data);
153
                
154 3
                $checker = new Checker($subject, array(
155 3
                  'codiceFiscaleToCheck' => $data['codiceFiscale'],
156 3
                  'omocodiaLevel' => $data['omocodiaLevel']
157 3
                ));
158
                
159 3
                if ($checker->check()) {
160 2
                    $response->status = true;
161 2
                    $response->message = 'Valid codice fiscale';
162 2
                }
163
                else {
164 1
                    $response->status = false;
165 1
                    $response->message = 'Invalid codice fiscale';
166
                }
167
            }
168
            
169 4
            return new JsonResponse($response);
170 12
        })
171 12
        ->bind('apiCheck');
172
        
173 12
        return $api;
174
    }
175
}
176
177