Completed
Push — master ( 3324f7...5d1949 )
by Andreas
8s
created

CfpFactory   B

Complexity

Total Complexity 37

Size/Duplication

Total Lines 202
Duplicated Lines 29.7 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 4
Bugs 1 Features 2
Metric Value
wmc 37
c 4
b 1
f 2
lcom 1
cbo 1
dl 60
loc 202
rs 8.6

15 Methods

Rating   Name   Duplication   Size   Complexity  
B createCfp() 0 36 4
A setName() 7 7 2
A setDateCfpStart() 7 7 2
A setDateCfpEnd() 7 7 2
A setTimezone() 7 7 2
A setUri() 8 8 2
A setEventUri() 8 8 2
A setDateEventStart() 0 8 2
A setDateEventEnd() 0 8 2
A setIconUri() 0 8 2
A setDescription() 8 8 2
A setLocation() 8 8 2
C setGeolocation() 0 36 7
A setTags() 0 10 2
A setSource() 0 8 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
 * Copyright (c) 2016-2016} Andreas Heigl<[email protected]>
4
 * Permission is hereby granted, free of charge, to any person obtaining a copy
5
 * of this software and associated documentation files (the "Software"), to deal
6
 * in the Software without restriction, including without limitation the rights
7
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
 * copies of the Software, and to permit persons to whom the Software is
9
 * furnished to do so, subject to the following conditions:
10
 * The above copyright notice and this permission notice shall be included in
11
 * all copies or substantial portions of the Software.
12
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
18
 * THE SOFTWARE.
19
 *
20
 * @author    Andreas Heigl<[email protected]>
21
 * @copyright 2016-2016 Andreas Heigl
22
 * @license   http://www.opensource.org/licenses/mit-license.php MIT-License
23
 * @version   0.0
24
 * @since     18.02.2016
25
 * @link      http://github.com/heiglandreas/callingallpapers
26
 */
27
28
namespace Callingallpapers\Api\Service;
29
30
use Callingallpapers\Api\Entity\Cfp;
31
use DateTimeImmutable;
32
33
class CfpFactory
34
{
35
36
    /**
37
     * Create a CFP from unfiltered values.
38
     * This method takes an array of unfiltered values, checks for required
39
     * values and validates resp. sanitizes those values before injecting them
40
     * into a new CfP
41
     *
42
     * @param array $params
43
     *
44
     * @return Cfp
45
     */
46
    public function createCfp(array $params)
47
    {
48
        $requiredFields = ['name', 'dateCfpStart', 'dateCfpEnd', 'uri', 'eventUri', 'timezone'];
49
        $missingFields = [];
50
51
        foreach ($requiredFields as $field) {
52
            if (! isset($params[$field])) {
53
                $missingFields[] = $field;
54
            }
55
        }
56
57
        if ($missingFields) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $missingFields of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
58
            throw new \UnexpectedValueException(sprintf(
59
                'The following fields are missing: "%1$s"',
60
                implode(', ', $missingFields)
61
            ), 400);
62
        }
63
        $cfp = new Cfp();
64
65
        self::setName($cfp, $params);
66
        self::setDateCfpStart($cfp, $params);
67
        self::setDateCfpEnd($cfp, $params);
68
        self::setTimezone($cfp, $params);
69
        self::setUri($cfp, $params);
70
        self::setEventUri($cfp, $params);
71
        self::setDateEventStart($cfp, $params);
72
        self::setDateEventEnd($cfp, $params);
73
        self::setIconUri($cfp, $params);
74
        self::setDescription($cfp, $params);
75
        self::setLocation($cfp, $params);
76
        self::setGeolocation($cfp, $params);
77
        self::setTags($cfp, $params);
78
        self::setSource($cfp, $params);
79
80
        return $cfp;
81
    }
82
83 View Code Duplication
    public static function setName(Cfp $cfp, array $array)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
84
    {
85
        if (! isset($array['name'])) {
86
            throw new \InvalidArgumentException('Name has to be specified');
87
        }
88
        $cfp->setName(filter_var($array['name'], FILTER_SANITIZE_STRING));
89
    }
90
91 View Code Duplication
    public static function setDateCfpStart(Cfp $cfp, array $array)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
92
    {
93
        if (! isset($array['dateCfpStart'])) {
94
            throw new \InvalidArgumentException('CFP-StartDate has to be specified');
95
        }
96
        $cfp->setDateCfpStart(new DateTimeImmutable($array['dateCfpStart']));
97
    }
98
99 View Code Duplication
    public static function setDateCfpEnd(Cfp $cfp, array $array)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
100
    {
101
        if (! isset($array['dateCfpEnd'])) {
102
            throw new \InvalidArgumentException('CFP-EndDate has to be specified');
103
        }
104
        $cfp->setDateCfpEnd(new DateTimeImmutable($array['dateCfpEnd']));
105
    }
106
107 View Code Duplication
    public static function setTimezone(Cfp $cfp, array $array)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
108
    {
109
        if (! isset($array['timezone'])) {
110
            throw new \InvalidArgumentException('Timezone has to be specified');
111
        }
112
        $cfp->setTimezone(filter_var($array['timezone'], FILTER_SANITIZE_STRING));
113
    }
114
115 View Code Duplication
    public static function setUri(Cfp $cfp, array $array)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
116
    {
117
        if (! isset($array['uri'])) {
118
            throw new \InvalidArgumentException('URI has to be specified');
119
        }
120
121
        $cfp->setUri(filter_var($array['uri'], FILTER_VALIDATE_URL));
122
    }
123
124 View Code Duplication
    public static function setEventUri(Cfp $cfp, array $array)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
125
    {
126
        if (! isset($array['eventUri'])) {
127
            throw new \InvalidArgumentException('Event-URI has to be specified');
128
        }
129
130
        $cfp->setEventUri(filter_var($array['eventUri'], FILTER_VALIDATE_URL));
131
    }
132
133
    public static function setDateEventStart(Cfp $cfp, array $array)
134
    {
135
        if (isset($array['dateEventStart'])) {
136
            $cfp->setDateEventStart(new DateTimeImmutable($array['dateEventStart']));
137
        } else {
138
            $cfp->setDateEventStart(new DateTimeImmutable('0000-00-00 00:00:00+00:00'));
139
        }
140
    }
141
142
    public static function setDateEventEnd(Cfp $cfp, array $array)
143
    {
144
        if (isset($array['dateEventEnd'])) {
145
            $cfp->setDateEventEnd(new DateTimeImmutable($array['dateEventEnd']));
146
        } else {
147
            $cfp->setDateEventEnd(new DateTimeImmutable('0000-00-00 00:00:00+00:00'));
148
        }
149
    }
150
151
    public static function setIconUri(Cfp $cfp, array $array)
152
    {
153
        if (! isset($array['iconUri'])) {
154
            return;
155
        }
156
157
        $cfp->setIconUri(filter_var($array['iconUri'], FILTER_VALIDATE_URL));
158
    }
159
160 View Code Duplication
    public static function setDescription(Cfp $cfp, array $array)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
161
    {
162
        if (! isset($array['description'])) {
163
            return;
164
        }
165
166
        $cfp->setDescription(filter_var($array['description'], FILTER_SANITIZE_STRING));
167
    }
168
169 View Code Duplication
    public static function setLocation(Cfp $cfp, array $array)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
170
    {
171
        if (! isset($array['location'])) {
172
            return;
173
        }
174
175
        $cfp->setLocation(filter_var($array['location'], FILTER_SANITIZE_STRING));
176
    }
177
178
    public static function setGeolocation(Cfp $cfp, array $array)
179
    {
180
        if (! isset($array['latitude'])) {
181
            return;
182
        }
183
184
        if (! isset($array['longitude'])) {
185
            return;
186
        }
187
188
        $latitude  = filter_var($array['latitude'],
189
            FILTER_SANITIZE_NUMBER_FLOAT,
190
            FILTER_FLAG_ALLOW_FRACTION);
191
        $longitude = filter_var($array['longitude'],
192
            FILTER_SANITIZE_NUMBER_FLOAT,
193
            FILTER_FLAG_ALLOW_FRACTION);
194
195
        if ($latitude > 90 || $latitude < - 90) {
196
            throw new \UnexpectedValueException(sprintf(
197
                'latitude has to be within a range of -90.0 to 90.0 bus is %1$f',
198
                $latitude
199
            ), 400);
200
        }
201
202
        if ($longitude > 180 || $longitude < - 180) {
203
            throw new \UnexpectedValueException(sprintf(
204
                'longitude has to be within a range of -180.0 to 180.0 but is %1$f',
205
                $longitude
206
            ), 400);
207
        }
208
209
        // TODO: Rewrite lat and long to be in the correct range
210
211
        $cfp->setLatitude($latitude);
212
        $cfp->setLongitude($longitude);
213
    }
214
215
    public static function setTags(Cfp $cfp, array $array)
216
    {
217
        if (! isset($array['tags'])) {
218
            return;
219
        }
220
221
        $cfp->setTags(array_map(function ($item) {
222
            return filter_var($item, FILTER_SANITIZE_STRING);
223
        }, $array['tags']));
224
    }
225
226
    public static function setSource(Cfp $cfp, array $array)
227
    {
228
        if (! isset($array['source'])) {
229
            return;
230
        }
231
232
        $cfp->addSource(filter_var($array['source'], FILTER_SANITIZE_STRING));
233
    }
234
}
235