FactoryGallery::validateInteger()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 6
rs 10
1
<?php declare(strict_types=1);
2
3
namespace One;
4
5
use One\Model\Gallery;
6
7
/**
8
 * FactoryGallery Class
9
 *
10
 * @method create
11
 * @method createGallery
12
 * @method validateUrl
13
 * @method validateInteger
14
 * @method validateString
15
 * @method checkData
16
 */
17
class FactoryGallery
18
{
19
    /**
20
     * function Create attachment Gallery
21
     */
22
    public static function create(array $data): \One\Model\Gallery
23
    {
24
        $body = self::validateString((string) self::checkData($data, 'body', ''));
25
        $order = self::validateInteger((int) self::checkData($data, 'order', null));
26
        $photo = self::validateUrl((string) self::checkData($data, 'photo', ''));
27
        $source = self::validateUrl((string) self::checkData($data, 'source', ''));
28
        $lead = self::validateString((string) self::checkData($data, 'lead', ''));
29
        return self::createGallery($body, $order, $photo, $source, $lead);
30
    }
31
32
    /**
33
     * Make Sure Url in string with correct url format
34
     * @throws \Exception
35
     */
36
    private static function validateUrl(string $string): string
37
    {
38
        if (filter_var($string, FILTER_VALIDATE_URL) === false) {
39
            throw new \Exception("Invalid url : ${string}");
40
        }
41
        return $string;
42
    }
43
44
    /**
45
     * functionality to check whether a variable is set or not.
46
     *
47
     * @param mixed $key
48
     * @param string $default
49
     * @return mixed
50
     */
51
    private static function checkData(array $data, $key, $default = '')
52
    {
53
        return $data[$key] ?? $default;
54
    }
55
56
    /**
57
     * functionality validity for int variables
58
     * @param mixed $var
59
     * @throws \Exception
60
     */
61
    private static function validateInteger($var): int
62
    {
63
        if (filter_var($var, FILTER_VALIDATE_INT) === false) {
64
            throw new \Exception('The variable must be a integer :' . $var);
65
        }
66
        return $var;
67
    }
68
69
    /**
70
     * functionality validity for string variables
71
     * @param mixed $var
72
     * @throws \Exception
73
     */
74
    private static function validateString($var): string
75
    {
76
        if (gettype($var) === 'string') {
77
            return $var;
78
        }
79
        throw new \Exception('The variable type must String :' . $var);
80
    }
81
82
    /**
83
     * Create Gallery Object
84
     */
85
    private static function createGallery(String $body, int $order, String $photo, String $source, string $lead): \One\Model\Gallery
86
    {
87
        return new Gallery(
88
            $body,
89
            $order,
90
            $photo,
91
            $source,
92
            $lead
93
        );
94
    }
95
}
96