Completed
Push — master ( 7e98cc...2d9b54 )
by ARCANEDEV
10s
created

Profile::getUrl()   A

Complexity

Conditions 5
Paths 16

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
nc 16
nop 3
dl 0
loc 15
ccs 10
cts 10
cp 1
crap 5
rs 9.4555
c 0
b 0
f 0
1
<?php namespace Arcanedev\Gravatar;
2
3
use Arcanedev\Gravatar\Exceptions\InvalidProfileFormatException;
4
5
/**
6
 * Class     Profile
7
 *
8
 * @package  Arcanedev\Gravatar
9
 * @author   ARCANEDEV <[email protected]>
10
 */
11
class Profile
12
{
13
    /* -----------------------------------------------------------------
14
     |  Traits
15
     | -----------------------------------------------------------------
16
     */
17
18
    use Concerns\HashEmail;
19
20
    /* -----------------------------------------------------------------
21
     |  Constants
22
     | -----------------------------------------------------------------
23
     */
24
25
    const BASE_URL   = 'http://www.gravatar.com/';
26
    const SECURE_URL = 'https://www.gravatar.com/';
27
28
    /* -----------------------------------------------------------------
29
     |  Properties
30
     | -----------------------------------------------------------------
31
     */
32
33
    /**
34
     * Profile's format.
35
     *
36
     * @var string
37
     */
38
    protected $format;
39
40
    /**
41
     * Supported format.
42
     *
43
     * @var array
44
     */
45
    protected static $supportedFormat = ['json', 'xml', 'php', 'vcf', 'qr'];
46
47
    /* -----------------------------------------------------------------
48
     |  Getters & Setters
49
     | -----------------------------------------------------------------
50
     */
51
52
    /**
53
     * Get the profile's format.
54
     *
55
     * @return string|null
56
     */
57 12
    public function getFormat()
58
    {
59 12
        return $this->format;
60
    }
61
62
    /**
63
     * Set the profile's format.
64
     *
65
     * @param  string  $format
66
     *
67
     * @return \Arcanedev\Gravatar\Profile
68
     */
69 16
    public function setFormat($format = null)
70
    {
71 16
        if ( ! is_null($format)) {
72 16
            self::checkFormat($format);
73 12
            $this->format = $format;
74
        }
75
76 12
        return $this;
77
    }
78
79
    /* -----------------------------------------------------------------
80
     |  Main Methods
81
     | -----------------------------------------------------------------
82
     */
83
84
    /**
85
     * Build the profile URL based on the provided email address.
86
     *
87
     * @param  string  $email
88
     * @param  array   $params
89
     * @param  bool    $secure
90
     *
91
     * @return string
92
     */
93 12
    public function getUrl($email = null, array $params = [], $secure = true)
94
    {
95 12
        $url  = $secure ? static::SECURE_URL : static::BASE_URL;
96 12
        $url .= is_null($email)
97 4
            ? str_repeat('0', 32)
98 12
            : static::hashEmail($email);
99
100 12
        if ($this->hasFormat())
101 8
            $url .= ".{$this->getFormat()}";
102
103 12
        if ( ! empty($params))
104 4
            $url .= '?'.http_build_query($params);
105
106 12
        return $url;
107
    }
108
109
    /**
110
     * Get the profile data.
111
     *
112
     * @param  string      $email
113
     * @param  mixed|null  $default
114
     *
115
     * @return array|mixed
116
     */
117 4
    public function get($email, $default = null)
118
    {
119 4
        $this->setFormat('php');
120
121 4
        $data = unserialize(
122 4
            file_get_contents($this->getUrl($email))
123
        );
124
125 4
        return (is_array($data) && isset($data['entry']))
126 3
            ? $data
127 4
            : $default;
128
    }
129
130
    /* -----------------------------------------------------------------
131
     |  Check Methods
132
     | -----------------------------------------------------------------
133
     */
134
135
    /**
136
     * Check if the format is not null.
137
     *
138
     * @return bool
139
     */
140 12
    public function hasFormat()
141
    {
142 12
        return ! is_null($this->format);
143
    }
144
145
    /**
146
     * Check the format.
147
     *
148
     * @param  string  $format
149
     */
150 16
    private static function checkFormat(&$format)
151
    {
152 16
        $format = strtolower($format);
153
154 16
        if ( ! in_array($format, static::$supportedFormat)) {
155 4
            throw InvalidProfileFormatException::make($format, static::$supportedFormat);
156
        }
157 12
    }
158
}
159