Passed
Pull Request — master (#43)
by Yasin
01:37
created

FactoryArticle::validateUrl()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
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
     * @param array $data
25
     * @return object Article
26
     */
27
    public static function create($data)
28
    {
29
        $data = self::validateArray($data);
30
        $title = self::validateString((string) self::checkData($data, 'title', ''));
31
        $body = self::validateString(self::checkData($data, 'body', ''));
0 ignored issues
show
Bug introduced by
self::checkData($data, 'body', '') of type array is incompatible with the type string expected by parameter $var of One\FactoryArticle::validateString(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

31
        $body = self::validateString(/** @scrutinizer ignore-type */ self::checkData($data, 'body', ''));
Loading history...
32
        $source = self::validateUrl(self::checkData($data, 'source', ''));
33
        $uniqueId = self::validateString(self::checkData($data, 'unique_id', ''));
34
        $typeId = self::validateInteger((int) self::checkData($data, 'type_id', ''));
35
        $categoryId = self::validateInteger((int) self::checkData($data, 'category_id', ''));
36
        $reporter = self::validateString(self::checkData($data, 'reporter', ''));
37
        $lead = self::validateString(self::checkData($data, 'lead', ''));
38
        $tags = self::validateString(self::checkData($data, 'tags', ''));
39
        $publishedAt = self::checkData($data, 'published_at', '');
40
        $identifier = self::checkData($data, 'identifier', '');
41
        return self::createArticle($title, $body, $source, $uniqueId, $typeId, $categoryId, $reporter, $lead, $tags, $publishedAt, $identifier);
0 ignored issues
show
Bug introduced by
$identifier of type array is incompatible with the type integer expected by parameter $identifier of One\FactoryArticle::createArticle(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

41
        return self::createArticle($title, $body, $source, $uniqueId, $typeId, $categoryId, $reporter, $lead, $tags, $publishedAt, /** @scrutinizer ignore-type */ $identifier);
Loading history...
Bug introduced by
$publishedAt of type array is incompatible with the type string expected by parameter $publishedAt of One\FactoryArticle::createArticle(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

41
        return self::createArticle($title, $body, $source, $uniqueId, $typeId, $categoryId, $reporter, $lead, $tags, /** @scrutinizer ignore-type */ $publishedAt, $identifier);
Loading history...
42
    }
43
44
    /**
45
     * functionality to check whether a variable is set or not.
46
     *
47
     * @param array $parts
48
     * @return array
49
     */
50
    private static function checkData($data, $key, $default = '')
51
    {
52
        return isset($data[$key]) ? $data[$key] : $default;
53
    }
54
55
    /**
56
     * Create Article Object
57
     *
58
     * @param String $title
59
     * @param string $body
60
     * @param string $source
61
     * @param int $uniqueId
62
     * @param int $typeId
63
     * @param int $categoryId
64
     * @param string $reporter
65
     * @param string $lead
66
     * @param string $tags
67
     * @param string $publishedAt
68
     * @param int $identifier
69
     * @return Article Object
70
     */
71
    public static function createArticle($title, $body, $source, $uniqueId, $typeId, $categoryId, $reporter, $lead, $tags, $publishedAt, $identifier)
72
    {
73
        return new Article(
74
            $title,
75
            $body,
76
            $source,
77
            $uniqueId,
78
            $typeId,
79
            $categoryId,
80
            $reporter,
81
            $lead,
82
            $tags,
83
            $publishedAt,
84
            $identifier
85
        );
86
    }
87
88
    /**
89
     * functionality validity for array variables
90
     *
91
     * @param int $var
92
     * @return int
93
     */
94
    private static function validateArray(array $var)
95
    {
96
        if (is_array($var) === false) {
0 ignored issues
show
introduced by
The condition is_array($var) === false is always false.
Loading history...
97
            throw new \Exception("The variable type must Array :");
98
        }
99
        return $var;
100
    }
101
102
    /**
103
     * Make Sure Url in string with correct url format
104
     *
105
     * @param String $string
106
     * @return string
107
     */
108
    private static function validateUrl($var)
109
    {
110
        if (filter_var($var, FILTER_VALIDATE_URL) === false) {
111
            throw new \Exception("Invalid url : $var");
112
        }
113
        return $var;
114
    }
115
    private static function parseUrl($string)
0 ignored issues
show
Unused Code introduced by
The method parseUrl() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
116
    {
117
        return parse_url($string);
118
    }
119
120
    /**
121
     * functionality validity for int variables
122
     *
123
     * @param int $var
124
     * @return int
125
     */
126
    private static function validateInteger($var)
127
    {
128
        if (filter_var($var, FILTER_VALIDATE_INT) === false) {
129
            throw new \Exception("The variable type must Integer :" . $var);
130
        }
131
        return $var;
132
    }
133
134
    /**
135
     * functionality validity for string variables
136
     *
137
     * @param String $var
138
     * @return String
139
     */
140
    private static function validateString($var)
141
    {
142
        if (is_string($var) === false) {
0 ignored issues
show
introduced by
The condition is_string($var) === false is always false.
Loading history...
143
            throw new \Exception("The variable type must String :" . $var);
144
        }
145
        return $var;
146
    }
147
}
148