Completed
Push — master ( e1d7be...0cde1c )
by Davide
03:19
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 79.22%

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 61
cts 77
cp 0.7922
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
        $api = $app['controllers_factory'];
21
22
        //Calculate the codice fiscale by the given parameters.
23
        $api->get('/calculate', function (Application $app, Request $request) {
24
            $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
            ));
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
            );
41
42
            $errors = $app['validator']->validate($data, $constraint);
43
            $response = new \stdClass();
44
45 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
                $response = $this->generateResponseFromErrors($errors);
47
            } else {
48
                $subject = new Subject($data);
49
50
                $calculator = new Calculator($subject, array(
51 1
                    'omocodiaLevel' => $data['omocodiaLevel'],
52
                ));
53
                $codiceFiscale = $calculator->calculate();
54
55 1
                $response->status = true;
56 1
                $response->codiceFiscale = $codiceFiscale;
57 4
            }
58
59
            return new JsonResponse($response);
60
        })
61
        ->bind('apiCalculate');
62
63
        //Calculate all the codice fiscale by the given parameters.
64
        $api->get('/calculateAll', function (Application $app, Request $request) {
65
            $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
            ));
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
            );
80
81
            $errors = $app['validator']->validate($data, $constraint);
82
            $response = new \stdClass();
83
84 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
                $response = $this->generateResponseFromErrors($errors);
86
            } else {
87
                $subject = new Subject($data);
88
89
                $calculator = new Calculator($subject);
90
                $codiciFiscali = $calculator->calculateAllPossibilities();
91
92 1
                $response->status = true;
93 1
                $response->codiciFiscali = $codiciFiscali;
94 3
            }
95
96
            return new JsonResponse($response);
97
        })
98
        ->bind('apiCalculateAll');
99
100
        //Check if the given parameters are ok for the given parameters.
101 4
        $api->get('/check', function (Application $app, Request $request) {
102
            $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
            ));
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 3
                'omocodiaLevel' => $request->get('omocodiaLevel', Checker::ALL_OMOCODIA_LEVELS),
120
            );
121
122
            $errors = $app['validator']->validate($data, $constraint);
123
            $response = new \stdClass();
124
125
            if (count($errors) > 0) {
126
                $response = $this->generateResponseFromErrors($errors);
127
            } else {
128
                $subject = new Subject($data);
129
130
                $checker = new Checker($subject, array(
131 3
                  'codiceFiscaleToCheck' => $data['codiceFiscale'],
132 3
                  'omocodiaLevel' => $data['omocodiaLevel'],
133
                ));
134
135
                if ($checker->check()) {
136 2
                    $response->status = true;
137 2
                    $response->message = 'Valid codice fiscale';
138
                } else {
139 1
                    $response->status = false;
140 1
                    $response->message = 'Invalid codice fiscale';
141 2
                }
142 1
            }
143
144
            return new JsonResponse($response);
145
        })
146
        ->bind('apiCheck');
147
148 13
        return $api;
149 13
    }
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
    {
160
        $response = new \stdclass();
161 8
        $response->status = false;
162
        $accessor = PropertyAccess::createPropertyAccessor();
163 8
        $responseErrors = array();
164
165
        foreach ($errors as $error) {
166
            $accessor->setValue($responseErrors, $error->getPropertyPath(), $error->getMessage());
167
        }
168
169 8
        $response->errors = $responseErrors;
170
171 8
        return $response;
172 8
    }
173
}
174