Passed
Pull Request — master (#43)
by Yasin
01:32
created

FactoryPhoto   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 75
rs 10
c 0
b 0
f 0
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A createPhoto() 0 7 1
A create() 0 10 1
A checkData() 0 3 2
A validateString() 0 6 2
A validateUrl() 0 6 2
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
29
        $description = self::validateString((string) self::checkData($data, 'description', ''));
30
31
        $information = self::validateString((string) self::checkData($data, 'information', ''));
32
33
        return self::createPhoto($url, $ratio, $description, $information);
34
    }
35
36
    /**
37
     * Make Sure Url in string with correct url format
38
     *
39
     * @param String $string
40
     * @return string
41
     */
42
    private static function validateUrl($url)
43
    {
44
        if (filter_var($url, FILTER_VALIDATE_URL) === false) {
45
            throw new \Exception("Invalid url : $url");
46
        }
47
        return $url;
48
    }
49
50
    /**
51
     * functionality to check whether a variable is set or not.
52
     *
53
     * @param array $parts
54
     * @return array
55
     */
56
    private static function checkData($data, $key, $default = '')
57
    {
58
        return isset($data[$key]) ? $data[$key] : $default;
59
    }
60
61
    /**
62
     * functionality validity for string variables
63
     *
64
     * @param String $var
65
     * @return String
66
     */
67
    private static function validateString($var)
68
    {
69
        if (is_string($var)) {
0 ignored issues
show
introduced by
The condition is_string($var) is always true.
Loading history...
70
            return $var;
71
        }
72
        throw new \Exception("The variable must be a string :" . $var);
73
    }
74
75
    /**
76
     * Create Photo Object
77
     *
78
     * @param String $url
79
     * @param String $ratio
80
     * @param String $description
81
     * @return Uri Photo
82
     */
83
    private static function createPhoto($url, $ratio, $description, $information)
84
    {
85
        return new Photo(
0 ignored issues
show
Bug Best Practice introduced by
The expression return new One\Model\Pho..., (string)$information) returns the type One\Model\Photo which is incompatible with the documented return type One\Uri.
Loading history...
86
            $url,
87
            $ratio,
88
            $description,
89
            (string) $information
90
        );
91
    }
92
}
93