GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

TwigGravatar::https()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
class TwigGravatar extends \Twig_Extension {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
	public $baseUrl = "http://www.gravatar.com/";
5
	public $httpsUrl = "https://secure.gravatar.com/";
6
7
	public $filterPrefix = "gr";
8
9
	private $filterOptions = array("is_safe" => array("html"));
10
11
	private $defaults = array(
12
		"404", "mm", "identicon", "monsterid", "wavatar", "retro", "blank"
13
	);
14
	private $ratings = array(
15
		"g", "pg", "r", "x"
16
	);
17
18
	/**
19
	 * {@inheritdoc}
20
	 */
21
	public function getFilters(){
22
        return array(
23
            new \Twig_SimpleFilter($this->filterPrefix . 'Avatar', array($this, 'avatar'), $this->filterOptions),
24
            new \Twig_SimpleFilter($this->filterPrefix . 'Https', array($this, 'https'), $this->filterOptions),
25
            new \Twig_SimpleFilter($this->filterPrefix . 'Size', array($this, 'size'), $this->filterOptions),
26
            new \Twig_SimpleFilter($this->filterPrefix . 'Default', array($this, 'def'), $this->filterOptions),
27
            new \Twig_SimpleFilter($this->filterPrefix . 'Rating', array($this, 'rating'), $this->filterOptions)
28
        );
29
    }
30
31
	/**
32
	 * Get a Gravatar Avatar URL
33
	 * @param  string $email Gravatar Email address
34
	 * @return string        Gravatar Avatar URL
35
	 * @throws \InvalidArgumentException If $email is invalid
36
	 */
37
	public function avatar($email) {
38
		if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
39
			return $this->baseUrl . "avatar/" . $this->generateHash($email);
40
		} else {
41
			throw new InvalidArgumentException("The avatar filter must be passed a valid Email address");
42
		}
43
	}
44
45
	/**
46
	 * Change a Gravatar URL to its Secure version
47
	 * @param  string $value URL to convert
48
	 * @return string        Converted URL
49
	 * @throws \InvalidArgumentException If $value isn't an existing Gravatar URL
50
	 */
51
	public function https($value) {
52
		if (strpos($value, $this->baseUrl) === false) {
53
			throw new InvalidArgumentException("You can only convert existing Gravatar URLs to HTTPS");
54
		}
55
		else {
56
			return str_replace($this->baseUrl, $this->httpsUrl, $value);
57
		}
58
	}
59
60
	/**
61
	 * Change the Size of a Gravatar URL
62
	 * @param  string  $value
63
	 * @param  integer $px
64
	 * @return string Sized Gravatar URL
65
	 */
66
	public function size($value, $px = 100) {
67
		if (!is_numeric($px) || $px < 0 || $px > 2048) {
68
			throw new InvalidArgumentException("You must pass the size filter a valid number between 0 and 2048");
69
		}
70
		else if (strpos($value, $this->baseUrl) === false
71
			&& strpos($value, $this->httpsUrl) === false) {
72
			throw new InvalidArgumentException("You must pass the size filter an existing Gravatar URL");
73
		}
74
		else {
75
			return $this->query($value, array("size" => $px));
76
		}
77
	}
78
79
	/**
80
	 * Specify a default Image for when there is no matching Gravatar image.
81
	 * @param string  $value
82
	 * @param string  $default Defaults to Mystery Man
83
	 * @param boolean $force   Always load the default image
84
	 * @return string          Gravatar URL with a default image.
85
	 */
86
	public function def($value, $default = "mm", $force = false) {
87
		if (strpos($value, $this->baseUrl) === false && strpos($value, $this->httpsUrl) === false) {
88
			throw new InvalidArgumentException("You can only a default to existing Gravatar URLs");
89
		}
90
		else if (!filter_var($default, FILTER_VALIDATE_URL) && !in_array($default, $this->defaults)) {
91
			throw new InvalidArgumentException("Default must be a URL or valid default");
92
		}
93
		else if (!is_bool($force)) {
94
			throw new InvalidArgumentException("The force option for a default must be boolean");
95
		}
96
		else {
97
			if (filter_var($default, FILTER_VALIDATE_URL)) $default = urlencode($default);
98
			$force = ($force ? "y" : "n");
99
			return $this->query($value, array("default" => $default, "forcedefault" => $force));
100
		}
101
	}
102
103
	/**
104
	 * Specify the maximum rating for an avatar
105
	 * @param  string $value
106
	 * @param  string $rating Expects g,pg,r or x
107
	 * @return string Gravatar URL with a rating specified
108
	 */
109
	public function rating($value, $rating = "g") {
110
		if (strpos($value, $this->baseUrl) === false && strpos($value, $this->httpsUrl) === false) {
111
			throw new InvalidArgumentException("You can only add a rating to an existing Gravatar URL");
112
		}
113
		else if (!in_array(strtolower($rating), $this->ratings)) {
114
			throw new InvalidArgumentException("Rating must be g,pg,r or x");
115
		}
116
		else {
117
			return $this->query($value, array("rating" => $rating));
118
		}
119
	}
120
121
122
	/**
123
	 * Generate the Hashed email address
124
	 * @param  string $email
125
	 * @return string        Hashed email address
126
	 */
127
	public function generateHash($email) {
128
		return md5(strtolower(trim($email)));
129
	}
130
131
	/**
132
	 * Generate the query string
133
	 * @param  string $string
134
	 * @param  array  $addition Array of what parameters to add
135
	 * @return string
136
	 */
137
	private function query($string, array $addition) {
138
		foreach ($addition as $name => $value) {
139
			$string .= (strpos($string, "?") === FALSE ? "?" : "&") . $name . "=" . $value;
140
		}
141
		return $string;
142
	}
143
144
	/**
145
	 * Get the Extension name
146
	 * @return string
147
	 */
148
	public function getName() {
149
		return 'TwigGravatar';
150
	}
151
}