Passed
Push — master ( c4128f...b4201c )
by Charis
01:36
created

FactoryArticle::createArticle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 11
dl 0
loc 14
rs 9.8666
c 0
b 0
f 0

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
namespace One;
3
4
use One\Model\Article;
5
6
/**
7
 * FactoryArticle Class
8
 *
9
 * @method create
10
 * @method createArticle
11
 * @method validateArray
12
 * @method validateUrl
13
 * @method validateInteger
14
 * @method validateString
15
 * @method checkData
16
 *
17
 */
18
class FactoryArticle
19
{
20
21
    /**
22
     * Create Article from array
23
     *
24
     * @return object Article
25
     */
26
    public static function create($data)
27
    {
28
        $data = self::validateArray($data);
29
        $title = self::validateString((string) self::checkData($data, 'title', ''));
30
        $body = self::validateString((string) self::checkData($data, 'body', ''));
31
        $source = self::validateUrl((string) self::checkData($data, 'source', ''));
32
        $uniqueId = self::validateString((string) self::checkData($data, 'unique_id', ''));
33
        $typeId = self::validateInteger((int) self::checkData($data, 'type_id', ''));
34
        $categoryId = self::validateInteger((int) self::checkData($data, 'category_id', ''));
35
        $reporter = self::validateString((string) self::checkData($data, 'reporter', ''));
36
        $lead = self::validateString((string) self::checkData($data, 'lead', ''));
37
        $tags = self::validateString((string) self::checkData($data, 'tags', ''));
38
        $publishedAt = self::validateString((string) self::checkData($data, 'published_at', ''));
39
        $identifier = self::validateInteger((int) self::checkData($data, 'identifier', null));
40
        return self::createArticle($title, $body, $source, $uniqueId, $typeId, $categoryId, $reporter, $lead, $tags, $publishedAt, $identifier);
41
    }
42
43
    /**
44
     * functionality to check whether a variable is set or not.
45
     *
46
     * @param array $parts
47
     * @return array
48
     */
49
    private static function checkData($data, $key, $default = '')
50
    {
51
        return isset($data[$key]) ? $data[$key] : $default;
52
    }
53
54
    /**
55
     * Create Article Object
56
     *
57
     * @param String $title
58
     * @param string $body
59
     * @param string $source
60
     * @param string $uniqueId
61
     * @param int $typeId
62
     * @param int $categoryId
63
     * @param string $reporter
64
     * @param string $lead
65
     * @param string $tags
66
     * @param string $publishedAt
67
     * @param int $identifier
68
     * @return Article Object
69
     */
70
    public static function createArticle($title, $body, $source, $uniqueId, $typeId, $categoryId, $reporter, $lead, $tags, $publishedAt, $identifier)
71
    {
72
        return new Article(
73
            $title,
74
            $body,
75
            $source,
76
            $uniqueId,
77
            $typeId,
78
            $categoryId,
79
            $reporter,
80
            $lead,
81
            $tags,
82
            $publishedAt,
83
            $identifier
84
        );
85
    }
86
87
    /**
88
     * functionality validity for array variables
89
     *
90
     * @param array $var
91
     * @return array
92
     */
93
    private static function validateArray($var)
94
    {
95
        if (gettype($var) === "array") {
96
            return $var;
97
        }
98
        throw new \Exception("The variable type must Array :");
99
    }
100
101
    /**
102
     * Make Sure Url in string with correct url format
103
     *
104
     * @param String $string
105
     * @return string
106
     */
107
    private static function validateUrl($var)
108
    {
109
        if (filter_var($var, FILTER_VALIDATE_URL) === false) {
110
            throw new \Exception("Invalid url : $var");
111
        }
112
        return $var;
113
    }
114
115
    /**
116
     * functionality validity for int variables
117
     *
118
     * @param int $var
119
     * @return int
120
     */
121
    private static function validateInteger($var)
122
    {
123
        if (filter_var($var, FILTER_VALIDATE_INT) === false) {
124
            throw new \Exception("The variable type must Integer :" . $var);
125
        }
126
        return $var;
127
    }
128
129
    /**
130
     * functionality validity for string variables
131
     *
132
     * @param String $var
133
     * @return String
134
     */
135
    private static function validateString($var)
136
    {
137
        if (gettype($var) === "string") {
138
            return $var;
139
        }
140
        throw new \Exception("The variable type must String :" . $var);
141
    }
142
}
143