Passed
Pull Request — master (#43)
by Yasin
02:31
created

FactoryGallery::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
namespace One;
3
4
use One\Model\Gallery;
5
6
/**
7
 * FactoryGallery Class
8
 *
9
 * @method create
10
 * @method createGallery
11
 * @method validateUrl
12
 * @method validateInteger
13
 * @method validateString
14
 * @method checkData
15
 */
16
class FactoryGallery
17
{
18
19
    /**
20
     * function Create attachment Gallery
21
     *
22
     * @param Array $data
23
     * @return object Gallery
24
     */
25
    public static function create($data)
26
    {
27
        $body = self::validateString((string) self::checkData($data, 'body', ''));
28
        $order = self::validateInteger((int) self::checkData($data, 'order', null));
29
        $photo = self::validateUrl((string) self::checkData($data, 'photo', ''));
30
        $source = self::validateUrl((string) self::checkData($data, 'source', ''));
31
        $lead = self::validateString((string) self::checkData($data, 'lead', ''));
32
        return self::createGallery($body, $order, $photo, $source, $lead);
33
    }
34
35
    /**
36
     * Make Sure Url in string with correct url format
37
     *
38
     * @param String $string
39
     * @return string
40
     */
41
    private static function validateUrl($string)
42
    {
43
        if (filter_var($string, FILTER_VALIDATE_URL) === false) {
44
            throw new \Exception("Invalid url : $string");
45
        }
46
        return $string;
47
    }
48
49
    /**
50
     * functionality to check whether a variable is set or not.
51
     *
52
     * @param array $parts
53
     * @return array
54
     */
55
    private static function checkData($data, $key, $default = '')
56
    {
57
        return isset($data[$key]) ? $data[$key] : $default;
58
    }
59
60
    /**
61
     * functionality validity for int variables
62
     *
63
     * @param int $var
64
     * @return int
65
     */
66
    private static function validateInteger($var)
67
    {
68
        if (filter_var($var, FILTER_VALIDATE_INT) === false) {
69
            throw new \Exception("The variable must be a integer :" . $var);
70
        }
71
        return $var;
72
    }
73
74
    /**
75
     * functionality validity for string variables
76
     *
77
     * @param String $var
78
     * @return String
79
     */
80
    private static function validateString($var)
81
    {
82
        if (is_string($var) == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
introduced by
The condition is_string($var) == true is always true.
Loading history...
83
            return $var;
84
        }
85
        throw new \Exception("The variable must be a string :" . $var);
86
    }
87
88
    /**
89
     * Create Gallery Object
90
     *
91
     * @param String $body
92
     * @param int $order
93
     * @param String $photo
94
     * @param String $source
95
     * @param string $lead
96
     */
97
    private static function createGallery($body, $order, $photo, $source, $lead)
98
    {
99
        return new Gallery(
100
            $body,
101
            $order,
102
            $photo,
103
            $source,
104
            $lead
105
        );
106
    }
107
}
108