Completed
Push — master ( 369134...92f766 )
by Davide
02:27
created

CodiceFiscaleControllerProvider   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 155
Duplicated Lines 15.48 %

Coupling/Cohesion

Components 0
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
c 2
b 0
f 0
lcom 0
cbo 8
dl 24
loc 155
ccs 106
cts 106
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B connect() 24 133 5
A generateResponseFromErrors() 0 13 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
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 = $this->generateResponseFromErrors($errors);
47 3
            } 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 4
            return new JsonResponse($response);
59 12
        })
60 12
        ->bind('apiCalculate');
61
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 2
                $response->status = true;
93 1
                $response->codiciFiscali = $codiciFiscali;
94
            }
95
            
96 4
            return new JsonResponse($response);
97 12
        })
98 12
        ->bind('apiCalculateAll');
99
100
        //Check if the given parameters are ok for the given parameters.
101 12
        $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
                }
139
                else {
140 1
                    $response->status = false;
141 1
                    $response->message = 'Invalid codice fiscale';
142
                }
143
            }
144
            
145 4
            return new JsonResponse($response);
146 12
        })
147 12
        ->bind('apiCheck');
148
        
149 12
        return $api;
150
    }
151
    
152
    /**
153
     * Generate response from the given ConstraintViolationList.
154
     * @param $errors The ConstraintViolationList instance.
155
     * @return Returns the response from the given errors.
156
     */
157 7
    private function generateResponseFromErrors(\Symfony\Component\Validator\ConstraintViolationList $errors){
158 7
        $response = new \stdclass;
159 7
        $response->status = false;
160 7
        $accessor = PropertyAccess::createPropertyAccessor();
161 7
        $responseErrors = array();
162
        
163 7
        foreach ($errors as $error) {
164 7
            $accessor->setValue($responseErrors, $error->getPropertyPath(), $error->getMessage());
165 7
        }
166
        
167 7
        $response->errors = $responseErrors;
168 7
        return $response;
169
    }
170
}
171
172