Passed
Pull Request — master (#93)
by Yasin
03:19
created

FactoryLivestreaming::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 39
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 28
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 39
rs 9.472
1
<?php declare(strict_types=1);
2
3
namespace One;
4
5
use One\Model\Livestreaming;
6
7
class FactoryLivestreaming
8
{
9
    public static function create(array $data): \One\Model\Livestreaming
10
    {
11
        $data = self::validateArray($data);
12
        $title = self::validateString(
13
            (string) self::checkData($data, 'title', '')
14
        );
15
        $desc = self::validateString(
16
            (string) self::checkData($data, 'desc', '')
17
        );
18
        $urlLive = self::validateString(
19
            (string) self::checkData($data, 'url_live', '')
20
        );
21
        $urlThumbnail = self::validateString(
22
            (string) self::checkData($data, 'url_thumbnail', '')
23
        );
24
        $publishedAt = self::validateString(
25
            (string) self::checkData($data, 'published_at', '')
26
        );
27
        $endAt = self::validateString(
28
            (string) self::checkData($data, 'end_at', '')
29
        );
30
        $publishStatus = (bool) self::checkData($data, 'publish_status', false);
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type string expected by parameter $default of One\FactoryLivestreaming::checkData(). ( Ignorable by Annotation )

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

30
        $publishStatus = (bool) self::checkData($data, 'publish_status', /** @scrutinizer ignore-type */ false);
Loading history...
31
        $uniqueId = self::validateString(
32
            (string) self::checkData($data, 'unique_id', '')
33
        );
34
        $identifier = self::validateInteger(
35
            (int) self::checkData($data, 'identifier', null)
36
        );
37
38
        return self::createLivestreaming(
39
            $uniqueId,
40
            $title,
41
            $desc,
42
            $urlLive,
43
            $urlThumbnail,
0 ignored issues
show
Bug introduced by
$urlThumbnail of type string is incompatible with the type boolean expected by parameter $active of One\FactoryLivestreaming::createLivestreaming(). ( Ignorable by Annotation )

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

43
            /** @scrutinizer ignore-type */ $urlThumbnail,
Loading history...
44
            $publishedAt,
0 ignored issues
show
Bug introduced by
$publishedAt of type string is incompatible with the type integer expected by parameter $identifier of One\FactoryLivestreaming::createLivestreaming(). ( Ignorable by Annotation )

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

44
            /** @scrutinizer ignore-type */ $publishedAt,
Loading history...
45
            $endAt,
0 ignored issues
show
Unused Code introduced by
The call to One\FactoryLivestreaming::createLivestreaming() has too many arguments starting with $endAt. ( Ignorable by Annotation )

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

45
        return self::/** @scrutinizer ignore-call */ createLivestreaming(

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
46
            $publishStatus,
47
            $identifier
48
        );
49
    }
50
51
    public static function createLivestreaming(
52
        string $uniqueId,
53
        string $title,
54
        string $content,
0 ignored issues
show
Unused Code introduced by
The parameter $content is not used and could be removed. ( Ignorable by Annotation )

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

54
        /** @scrutinizer ignore-unused */ string $content,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
55
        string $url,
0 ignored issues
show
Unused Code introduced by
The parameter $url is not used and could be removed. ( Ignorable by Annotation )

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

55
        /** @scrutinizer ignore-unused */ string $url,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
56
        bool $active,
0 ignored issues
show
Unused Code introduced by
The parameter $active is not used and could be removed. ( Ignorable by Annotation )

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

56
        /** @scrutinizer ignore-unused */ bool $active,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
57
        int $identifier
58
    ): Livestreaming {
59
        return new Livestreaming(
60
            $uniqueId,
61
            $title,
62
            $desc,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $desc seems to be never defined.
Loading history...
63
            $urlLive,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $urlLive seems to be never defined.
Loading history...
64
            $urlThumbnail,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $urlThumbnail seems to be never defined.
Loading history...
65
            $publishedAt,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $publishedAt seems to be never defined.
Loading history...
66
            $endAt,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $endAt seems to be never defined.
Loading history...
67
            $publishStatus,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $publishStatus seems to be never defined.
Loading history...
68
            $identifier
69
        );
70
    }
71
72
    /**
73
     * functionality to check whether a variable is set or not.
74
     * @param mixed $key
75
     * @param string $default
76
     * @return mixed
77
     */
78
    private static function checkData(array $data, $key, $default = '')
79
    {
80
        return $data[$key] ?? $default;
81
    }
82
83
    /**
84
     * functionality validity for array variables
85
     */
86
    private static function validateArray(array $var): array
87
    {
88
        if (gettype($var) === 'array') {
89
            return $var;
90
        }
91
        throw new \Exception('The variable type must Array :');
92
    }
93
94
    /**
95
     * Make Sure Url in string with correct url format
96
     */
97
    private static function validateUrl(string $var): string
0 ignored issues
show
Unused Code introduced by
The method validateUrl() 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...
98
    {
99
        if (filter_var($var, FILTER_VALIDATE_URL) === false) {
100
            throw new \Exception("Invalid url : ${var}");
101
        }
102
        return $var;
103
    }
104
105
    /**
106
     * functionality validity for string variables
107
     */
108
    private static function validateString(String $var): String
109
    {
110
        if (gettype($var) === 'string') {
111
            return $var;
112
        }
113
        throw new \Exception('The variable type must String :' . $var);
114
    }
115
116
    /**
117
     * functionality validity for int variables
118
     */
119
    private static function validateInteger(int $var): int
120
    {
121
        if (filter_var($var, FILTER_VALIDATE_INT) === false) {
122
            throw new \Exception('The variable type must Integer :' . $var);
123
        }
124
        return $var;
125
    }
126
}
127
128