Completed
Push — master ( a97445...ea780c )
by arto
11:02
created

Configuration::configure()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 43
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 43
rs 8.439
cc 6
eloc 29
nc 32
nop 8

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/**
3
 * @author stev leibelt <[email protected]>
4
 * @since 2015-09-19
5
 */
6
namespace Net\Bazzline\Propel\Behavior\EntityInstantiator;
7
8
use InvalidArgumentException;
9
10
class Configuration
11
{
12
    /** @var string */
13
    private $className;
14
15
    /** @var null|string */
16
    private $defaultConnectionMode;
17
18
    /** @var null|string */
19
    private $defaultConnectionName;
20
21
    /** @var string */
22
    private $extends;
23
24
    /** @var string */
25
    private $filePathToOutput;
26
27
    /** @var string */
28
    private $indention;
29
30
    /** @var bool */
31
    private $isConfigured = false;
32
33
    /** @var string */
34
    private $namespace;
35
36
    /** @var bool */
37
    private $useFullyQualifiedNames;
38
39
    /**
40
     * @param string $className
41
     * @param string $indention
42
     * @param string $pathToOutput
43
     * @param null|string $namespace
44
     * @param null|string $extends
45
     * @param null|string $defaultConnectionMode
46
     * @param null|string $defaultConnectionName
47
     * @param null|bool $useFullyQualifiedNames
48
     * @throws InvalidArgumentException
49
     */
50
    public function configure(
51
        $className,
52
        $indention,
53
        $pathToOutput,
54
        $namespace = null,
55
        $extends = null,
56
        $defaultConnectionMode = null,
57
        $defaultConnectionName = null,
58
        $useFullyQualifiedNames = null
59
    ) {
60
        $this->setClassName($className);
61
62
        if (!is_null($extends)) {
63
            $this->setExtends($extends);
64
        }
65
66
        $this->setIndention($indention);
67
68
        if (!is_null($namespace)) {
69
            $this->setNamespace($namespace);
70
        }
71
72
        if (!is_null($defaultConnectionMode)) {
73
            $this->setDefaultConnectionMode($defaultConnectionMode);
74
        } else {
75
            $this->setDefaultConnectionMode('Propel::CONNECTION_WRITE');
76
        }
77
78
        if (!is_null($defaultConnectionName)) {
79
            $this->setDefaultConnectionName('\'' . $defaultConnectionName . '\'');
80
        } else {
81
            $this->setDefaultConnectionName('null');
82
        }
83
84
        if (!is_null($useFullyQualifiedNames)) {
85
            $this->setUseFullyQualifiedName($useFullyQualifiedNames);
86
        } else {
87
            $this->setUseFullyQualifiedName(false);
88
        }
89
90
        $this->tryToCreatePathNameToFileOutputOrThrowInvalidArgumentException($pathToOutput);
91
        $this->isConfigured = true;
92
    }
93
94
    /**
95
     * @return bool
96
     */
97
    public function doNotUseFullyQualifiedNames()
98
    {
99
        return ($this->useFullyQualifiedNames === false);
100
    }
101
102
    /**
103
     * @return string
104
     */
105
    public function getClassName()
106
    {
107
        return $this->className;
108
    }
109
110
    /**
111
     * @return null|string
112
     */
113
    public function getDefaultConnectionMode()
114
    {
115
        return $this->defaultConnectionMode;
116
    }
117
118
    /**
119
     * @return null|string
120
     */
121
    public function getDefaultConnectionName()
122
    {
123
        return $this->defaultConnectionName;
124
    }
125
    /**
126
     * @return null|string
127
     */
128
    public function getExtends()
129
    {
130
        return $this->extends;
131
    }
132
133
    /**
134
     * @return string
135
     */
136
    public function getIndention()
137
    {
138
        return $this->indention;
139
    }
140
141
    /**
142
     * @return null|string
143
     */
144
    public function getNamespace()
145
    {
146
        return $this->namespace;
147
    }
148
149
    /**
150
     * @return string
151
     */
152
    public function getFilePathToOutput()
153
    {
154
        return $this->filePathToOutput;
155
    }
156
157
    /**
158
     * @return bool
159
     */
160
    public function hasExtends()
161
    {
162
        return (!is_null($this->extends));
163
    }
164
165
    /**
166
     * @return bool
167
     */
168
    public function hasNamespace()
169
    {
170
        return (!is_null($this->namespace));
171
    }
172
173
    /**
174
     * @return bool
175
     */
176
    public function isConfigured()
177
    {
178
        return ($this->isConfigured);
179
    }
180
181
    /**
182
     * @return bool
183
     */
184
    public function isNotConfigured()
185
    {
186
        return (!$this->isConfigured);
187
    }
188
189
    /**
190
     * @return bool
191
     */
192
    public function useFullyQualifiedNames()
193
    {
194
        return ($this->useFullyQualifiedNames === true);
195
    }
196
197
    /**
198
     * @param string $className
199
     * @throws InvalidArgumentException
200
     */
201
    private function setClassName($className)
202
    {
203
        $this->throwInvalidArgumentExceptionIfStringIsNotValid($className);
204
        $this->className = $className;
205
    }
206
207
    /**
208
     * @param string $defaultConnectionMode
209
     * @throws InvalidArgumentException
210
     */
211
    private function setDefaultConnectionMode($defaultConnectionMode)
212
    {
213
        $this->throwInvalidArgumentExceptionIfStringIsNotValid($defaultConnectionMode);
214
        $this->defaultConnectionMode = $defaultConnectionMode;
215
    }
216
217
    /**
218
     * @param string $defaultConnectionName
219
     * @throws InvalidArgumentException
220
     */
221
    private function setDefaultConnectionName($defaultConnectionName)
222
    {
223
        $this->throwInvalidArgumentExceptionIfStringIsNotValid($defaultConnectionName);
224
        $this->defaultConnectionName = $defaultConnectionName;
225
    }
226
227
    /**
228
     * @param string $extends
229
     * @throws InvalidArgumentException
230
     */
231
    private function setExtends($extends)
232
    {
233
        $this->throwInvalidArgumentExceptionIfStringIsNotValid($extends);
234
        $this->extends = $extends;
235
    }
236
237
    /**
238
     * @param string $indention
239
     * @throws InvalidArgumentException
240
     */
241
    private function setIndention($indention)
242
    {
243
        $this->throwInvalidArgumentExceptionIfStringIsNotValid($indention);
244
        $this->indention = $indention;
245
    }
246
247
    /**
248
     * @param string $namespace
249
     * @throws InvalidArgumentException
250
     */
251
    private function setNamespace($namespace)
252
    {
253
        $this->throwInvalidArgumentExceptionIfStringIsNotValid($namespace);
254
        $this->namespace = $namespace;
255
    }
256
257
    /**
258
     * @param $useFullyQualifiedName
259
     * @throws InvalidArgumentException
260
     */
261
    private function setUseFullyQualifiedName($useFullyQualifiedName)
262
    {
263
        if (!is_bool($useFullyQualifiedName)) {
264
            throw new InvalidArgumentException(
265
                'provided variable must be a boolean "' . var_export($useFullyQualifiedName, true) . '"" given'
266
            );
267
        }
268
        $this->useFullyQualifiedNames = $useFullyQualifiedName;
269
    }
270
271
    /**
272
     * @param string $string
273
     * @throws InvalidArgumentException
274
     */
275
    private function throwInvalidArgumentExceptionIfStringIsNotValid($string)
276
    {
277
        if (!is_string($string)) {
278
            throw new InvalidArgumentException(
279
                'provided variable must be a string "' . var_export($string, true) . '"" given'
280
            );
281
        }
282
283
        if (strlen($string) < 1) {
284
            throw new InvalidArgumentException(
285
                'provided string must contain at least one character'
286
            );
287
        }
288
    }
289
290
    /**
291
     * @param string $path
292
     * @throws InvalidArgumentException
293
     */
294
    private function tryToCreatePathNameToFileOutputOrThrowInvalidArgumentException($path)
295
    {
296
        if (!is_dir($path)) {
297
            throw new InvalidArgumentException(
298
                'provided path "' . $path . '" is not a directory'
299
            );
300
        }
301
302
        if (!is_writable($path)) {
303
            throw new InvalidArgumentException(
304
                'provided path "' . $path . '" is not writable'
305
            );
306
        }
307
308
        $this->filePathToOutput = $path . DIRECTORY_SEPARATOR . $this->className . '.php';
309
    }
310
}
311