Issues (29)

src/FactoryHeadline.php (1 issue)

Labels
Severity
1
<?php declare(strict_types=1);
2
3
namespace One;
4
5
use One\Model\Headline;
6
7
class FactoryHeadline
8
{
9
    public static function create(array $data): \One\Model\Headline
10
    {
11
        $data = self::validateArray($data);
12
        $title = self::validateString(
13
            (string) self::checkData($data, 'title', '')
14
        );
15
        $content = self::validateString(
16
            (string) self::checkData($data, 'content', '')
17
        );
18
        $url = self::validateUrl(
19
            (string) self::checkData($data, 'url', '')
20
        );
21
        $uniqueId = self::validateString(
22
            (string) self::checkData($data, 'uniqueId', '')
23
        );
24
        $active = (bool) self::checkData($data, 'active', false);
0 ignored issues
show
false of type false is incompatible with the type string expected by parameter $default of One\FactoryHeadline::checkData(). ( Ignorable by Annotation )

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

24
        $active = (bool) self::checkData($data, 'active', /** @scrutinizer ignore-type */ false);
Loading history...
25
        $identifier = self::validateInteger(
26
            (int) self::checkData($data, 'identifier', null)
27
        );
28
29
        return self::createHeadline(
30
            $uniqueId,
31
            $title,
32
            $content,
33
            $url,
34
            $active,
35
            $identifier
36
        );
37
    }
38
39
    public static function createHeadline(
40
        string $uniqueId,
41
        string $title,
42
        string $content,
43
        string $url,
44
        bool $active,
45
        int $identifier
46
    ): Headline {
47
        return new Headline(
48
            $uniqueId,
49
            $title,
50
            $content,
51
            $url,
52
            $active,
53
            $identifier
54
        );
55
    }
56
57
    /**
58
     * functionality to check whether a variable is set or not.
59
     * @param mixed $key
60
     * @param string $default
61
     * @return mixed
62
     */
63
    private static function checkData(array $data, $key, $default = '')
64
    {
65
        return $data[$key] ?? $default;
66
    }
67
68
    /**
69
     * functionality validity for array variables
70
     */
71
    private static function validateArray(array $var): array
72
    {
73
        if (gettype($var) === 'array') {
74
            return $var;
75
        }
76
        throw new \Exception('The variable type must Array :');
77
    }
78
79
    /**
80
     * Make Sure Url in string with correct url format
81
     */
82
    private static function validateUrl(string $var): string
83
    {
84
        if (filter_var($var, FILTER_VALIDATE_URL) === false) {
85
            throw new \Exception("Invalid url : ${var}");
86
        }
87
        return $var;
88
    }
89
90
    /**
91
     * functionality validity for string variables
92
     */
93
    private static function validateString(String $var): String
94
    {
95
        if (gettype($var) === 'string') {
96
            return $var;
97
        }
98
        throw new \Exception('The variable type must String :' . $var);
99
    }
100
101
    /**
102
     * functionality validity for int variables
103
     */
104
    private static function validateInteger(int $var): int
105
    {
106
        if (filter_var($var, FILTER_VALIDATE_INT) === false) {
107
            throw new \Exception('The variable type must Integer :' . $var);
108
        }
109
        return $var;
110
    }
111
}
112
113