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

FactoryPhoto::createPhoto()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 4
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
namespace One;
3
4
use One\Model\Photo;
5
6
/**
7
 * FactoryPhoto Class
8
 *
9
 * @method create
10
 * @method createPhoto
11
 * @method validateUrl
12
 * @method validateString
13
 * @method checkData
14
 */
15
class FactoryPhoto
16
{
17
18
    /**
19
     * function Create Photo Attachment
20
     *
21
     * @param String $string
22
     * @return object Uri
23
     */
24
    public static function create($data)
25
    {
26
        $url = self::validateUrl((string) self::checkData($data, 'url', ''));
27
        $ratio = self::validateString((string) self::checkData($data, 'ratio', ''));
28
        $description = self::validateString((string) self::checkData($data, 'description', ''));
29
30
        $information = self::validateString((string) self::checkData($data, 'information', ''));
31
32
        return self::createPhoto($url, $ratio, $description, $information);
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($url)
42
    {
43
        if (filter_var($url, FILTER_VALIDATE_URL) === false) {
44
            throw new \Exception("Invalid url : $url");
45
        }
46
        return $url;
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 string variables
62
     *
63
     * @param String $var
64
     * @return String
65
     */
66
    private static function validateString($var)
67
    {
68
        if (is_string($var) == true) {
0 ignored issues
show
introduced by
The condition is_string($var) == true is always true.
Loading history...
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...
69
            return $var;
70
        }
71
        throw new \Exception("The variable must be a string :" . $var);
72
    }
73
74
    /**
75
     * Create Photo Object
76
     *
77
     * @param String $url
78
     * @param String $ratio
79
     * @param String $description
80
     * @param String $information
81
     */
82
    private static function createPhoto($url, $ratio, $description, $information)
83
    {
84
        return new Photo(
85
            $url,
86
            $ratio,
87
            $description,
88
            $information
89
        );
90
    }
91
}
92