aboutsummaryrefslogtreecommitdiff
path: root/lib/Favicon/DataAccess.php
blob: 4c1a29541ea761f905aa59b47f4631ecf9bc198c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php

namespace Favicon;

/**
 * DataAccess is a wrapper used to read/write data locally or remotly
 * Aside from SOLID principles, this wrapper is also useful to mock remote resources in unit tests
 * Note: remote access warning are silenced because we don't care if a website is unreachable
 **/
class DataAccess {
	public function retrieveUrl($url) {
		$this->set_context();
		return @file_get_contents($url);
	}

	public function retrieveHeader($url) {
		$this->set_context();
		$headers = @get_headers($url, 1);
		return is_array($headers) ? array_change_key_case($headers) : array();
	}

	public function saveCache($file, $data) {
		file_put_contents($file, $data);
	}

	public function readCache($file) {
		return file_get_contents($file);
	}

	private function set_context() {
		stream_context_set_default(
			array(
				'http' => array(
					'method' => 'GET',
					'timeout' => 10,
					'header' => "User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:20.0; Favicon; +https://github.com/ArthurHoaro/favicon) Gecko/20100101 Firefox/32.0\r\n",
				)
			)
		);
	}
}