Passed
Pull Request — master (#43)
by Yasin
01:41
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(self::checkData($data, 'body', ''));
0 ignored issues
show
Bug Best Practice introduced by
The method One\FactoryGallery::validateString() is not static, but was called statically. ( Ignorable by Annotation )

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

27
        /** @scrutinizer ignore-call */ 
28
        $body = self::validateString(self::checkData($data, 'body', ''));
Loading history...
Bug Best Practice introduced by
The method One\FactoryGallery::checkData() is not static, but was called statically. ( Ignorable by Annotation )

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

27
        $body = self::validateString(self::/** @scrutinizer ignore-call */ checkData($data, 'body', ''));
Loading history...
Bug introduced by
self::checkData($data, 'body', '') of type array is incompatible with the type string expected by parameter $var of One\FactoryGallery::validateString(). ( Ignorable by Annotation )

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

27
        $body = self::validateString(/** @scrutinizer ignore-type */ self::checkData($data, 'body', ''));
Loading history...
28
        $order = self::validateInteger(self::checkData($data, 'order', null));
0 ignored issues
show
Bug introduced by
self::checkData($data, 'order', null) of type array is incompatible with the type integer expected by parameter $var of One\FactoryGallery::validateInteger(). ( Ignorable by Annotation )

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

28
        $order = self::validateInteger(/** @scrutinizer ignore-type */ self::checkData($data, 'order', null));
Loading history...
Bug Best Practice introduced by
The method One\FactoryGallery::validateInteger() is not static, but was called statically. ( Ignorable by Annotation )

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

28
        /** @scrutinizer ignore-call */ 
29
        $order = self::validateInteger(self::checkData($data, 'order', null));
Loading history...
29
        $photo = self::validateUrl(self::checkData($data, 'photo', ''));
0 ignored issues
show
Bug Best Practice introduced by
The method One\FactoryGallery::validateUrl() is not static, but was called statically. ( Ignorable by Annotation )

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

29
        /** @scrutinizer ignore-call */ 
30
        $photo = self::validateUrl(self::checkData($data, 'photo', ''));
Loading history...
Bug introduced by
self::checkData($data, 'photo', '') of type array is incompatible with the type string expected by parameter $string of One\FactoryGallery::validateUrl(). ( Ignorable by Annotation )

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

29
        $photo = self::validateUrl(/** @scrutinizer ignore-type */ self::checkData($data, 'photo', ''));
Loading history...
30
        $source = self::validateUrl(self::checkData($data, 'source', ''));
31
        $lead = self::validateString(self::checkData($data, 'lead', ''));
32
        return self::createGallery($body, $order, $photo, $source, $lead);
0 ignored issues
show
Bug Best Practice introduced by
The method One\FactoryGallery::createGallery() is not static, but was called statically. ( Ignorable by Annotation )

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

32
        return self::/** @scrutinizer ignore-call */ createGallery($body, $order, $photo, $source, $lead);
Loading history...
33
    }
34
35
    /**
36
     * Make Sure Url in string with correct url format
37
     *
38
     * @param String $string
39
     * @return string
40
     */
41
    private 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 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 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 function validateString($var)
81
    {
82
        if (is_string($var) == false) {
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) == false is always false.
Loading history...
83
            throw new \Exception("The variable must be a string :" . $var);
84
        }
85
        return $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
    public 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