GravatarResponse::__construct()   B
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 8.7857
c 0
b 0
f 0
cc 6
nc 5
nop 2
1
<?php
2
/**
3
 * GravatarExtension.php
4
 *
5
 * @copyright      More in license.md
6
 * @license        https://www.ipublikuj.eu
7
 * @author         Adam Kadlec <[email protected]>
8
 * @package        iPublikuj:Gravatar!
9
 * @subpackage     Application
10
 * @since          1.0.0
11
 *
12
 * @date           02.03.15
13
 */
14
15
declare(strict_types = 1);
16
17
namespace IPub\Gravatar\Application;
18
19
use Nette;
20
use Nette\Utils;
21
use Nette\Http;
22
23
use IPub\Gravatar;
24
use IPub\Gravatar\Exceptions;
25
26
/**
27
 * Gravatar image response
28
 *
29
 * @package        iPublikuj:Gravatar!
30
 * @subpackage     Application
31
 *
32
 * @author         Adam Kadlec <[email protected]>
33
 */
34
final class GravatarResponse implements Nette\Application\IResponse
35
{
36
	/**
37
	 * Implement nette smart magic
38
	 */
39
	use Nette\SmartObject;
40
41
	/**
42
	 * @var Utils\Image
43
	 */
44
	private $image;
45
46
	/**
47
	 * @var string
48
	 */
49
	private $type;
50
51
	/**
52
	 * @param string $email
53
	 * @param int $size 1-512
54
	 *
55
	 * @throws Utils\ImageException
56
	 */
57
	public function __construct(string $email, int $size)
58
	{
59
		// Set user email address
60
		if (!Utils\Validators::isEmail($email)) {
61
			throw new Exceptions\InvalidArgumentException('Inserted email is not valid email address');
62
		}
63
64
		if ($size < 1 || $size > 512) {
65
			throw new Exceptions\InvalidArgumentException(sprintf('Unsupported size "%s", Gravatar expects "1 - 512".', $size));
66
		}
67
68
		$path = $this->createUrl($email, $size, '404');
69
70
		if (!$sock = @fsockopen('gravatar.com', 80, $errorNo, $error)) {
71
			return NULL;
72
		}
73
74
		fputs($sock, "HEAD " . $path . " HTTP/1.0\r\n\r\n");
75
		$header = fgets($sock, 128);
76
		fclose($sock);
77
78
		if (strpos($header, '404')) {
79
			throw new Exceptions\InvalidStateException('Gravatar image could not be loaded from the server.');
80
		}
81
82
		$path = $this->createUrl($email, $size);
83
84
		$img = @file_get_contents($path);
85
86
		$this->image = Utils\Image::fromString($img);
87
		$this->type = Utils\Image::JPEG;
88
	}
89
90
91
	/**
92
	 * Returns Nette\Utils\Image instance
93
	 *
94
	 * @return Utils\Image
95
	 */
96
	public function getImage() : Utils\Image
97
	{
98
		return $this->image;
99
	}
100
101
102
	/**
103
	 * Returns the type of a image
104
	 *
105
	 * @return string
106
	 */
107
	public function getType() : string
108
	{
109
		return $this->type;
110
	}
111
112
113
	/**
114
	 * Sends response to output
115
	 *
116
	 * @param Http\IRequest $httpRequest
117
	 * @param Http\IResponse $httpResponse
118
	 *
119
	 * @return void
120
	 */
121
	public function send(Http\IRequest $httpRequest, Http\IResponse $httpResponse) : void
122
	{
123
		echo $this->image->send($this->type, 85);
124
	}
125
126
127
	/**
128
	 * @param string $email
129
	 * @param int $size
130
	 * @param string $defaultImage
131
	 *
132
	 * @return string
133
	 */
134
	private function createUrl(string $email, int $size, string $defaultImage = NULL) : string
135
	{
136
		// Tack the email hash onto the end.
137
		$emailHash = hash('md5', strtolower(trim($email)));
138
139
		// Start building the URL, and deciding if we're doing this via HTTPS or HTTP.
140
		$url = new Http\Url(Gravatar\Gravatar::HTTPS_URL . $emailHash);
141
142
		// Time to figure out our request params
143
		$params = [];
144
		$params['s'] = $size;
145
		$params['r'] = 'g';
146
		$params['d'] = $defaultImage ?: 'mm';
147
148
		// Add query params
149
		$url->appendQuery($params);
150
151
		// And we're done
152
		return $url->getAbsoluteUrl();
153
	}
154
}
155