1 | <?php |
||
9 | class PreviewManager |
||
10 | { |
||
11 | /** |
||
12 | * @var Crawler |
||
13 | */ |
||
14 | private $crawler; |
||
15 | |||
16 | /** |
||
17 | * @var Cache |
||
18 | */ |
||
19 | private $cache; |
||
20 | |||
21 | /** |
||
22 | * Url object |
||
23 | * @var Url |
||
24 | */ |
||
25 | private $url; |
||
26 | |||
27 | /** |
||
28 | * @var HttpInterface |
||
29 | */ |
||
30 | protected $http; |
||
31 | |||
32 | |||
33 | 99 | public function __construct(HttpInterface $http, CacheItemPoolInterface $cache = null) |
|
40 | |||
41 | /** |
||
42 | * Instantiate class with dependencies |
||
43 | * @param CacheItemPoolInterface $cache |
||
44 | * @return static |
||
45 | */ |
||
46 | 6 | public static function create(CacheItemPoolInterface $cache = null) |
|
51 | |||
52 | /** |
||
53 | * @param string $url |
||
54 | * @return Preview |
||
55 | * @throws \Exception |
||
56 | */ |
||
57 | 57 | public function fetch($url = null) |
|
58 | { |
||
59 | 57 | if ($url instanceof Url) { |
|
60 | 3 | $this->url = $url; |
|
61 | 3 | } |
|
62 | 57 | if (is_string($url)) { |
|
63 | 48 | $this->url = new Url($url); |
|
64 | 45 | } |
|
65 | 54 | if ($this->url === null) { |
|
66 | 3 | throw new \Exception('no url provided'); |
|
67 | } |
||
68 | |||
69 | 51 | $body = $this->http->get($this->url->original); |
|
70 | |||
71 | 51 | if ($body === false) { |
|
72 | 3 | throw new \Exception('failed to load page'); |
|
73 | } |
||
74 | |||
75 | 48 | return $this->getPreview($body); |
|
76 | } |
||
77 | |||
78 | 3 | public function findUrl($text) |
|
84 | |||
85 | 6 | public function findOrFetch($url) |
|
93 | |||
94 | 3 | public function cache(Preview $preview, $expiresAt = null) |
|
98 | /** |
||
99 | * returns an instance of Preview |
||
100 | * @return Preview |
||
101 | */ |
||
102 | 48 | private function getPreview($body) |
|
114 | } |
||
115 |