|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Wallhaven; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
|
6
|
|
|
use GuzzleHttp\Pool; |
|
7
|
|
|
use Wallhaven\Exceptions\DownloadException; |
|
8
|
|
|
use Wallhaven\Exceptions\WallhavenException; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Wallpaper list. |
|
12
|
|
|
* |
|
13
|
|
|
* @package Wallhaven |
|
14
|
|
|
*/ |
|
15
|
|
|
class WallpaperList implements \ArrayAccess, \IteratorAggregate, \Countable |
|
16
|
|
|
{ |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var Wallpaper[] Wallpapers. |
|
20
|
|
|
*/ |
|
21
|
|
|
private $wallpapers = []; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Download all wallpapers in list. |
|
25
|
|
|
* |
|
26
|
|
|
* @param string $directory Where to download wallpapers. |
|
27
|
|
|
* |
|
28
|
|
|
* @throws DownloadException Thrown if the download directory cannot be created. |
|
29
|
|
|
*/ |
|
30
|
|
|
public function downloadAll($directory) |
|
31
|
|
|
{ |
|
32
|
|
View Code Duplication |
if (!file_exists($directory)) { |
|
|
|
|
|
|
33
|
|
|
if (!@mkdir($directory, null, true)) { |
|
34
|
|
|
throw new DownloadException("The download directory cannot be created."); |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
$client = new Client(); |
|
39
|
|
|
|
|
40
|
|
|
$requests = []; |
|
41
|
|
|
foreach ($this->wallpapers as $w) { |
|
42
|
|
|
$url = $w->getImageUrl(true); |
|
43
|
|
|
|
|
44
|
|
|
$requests[] = $client->createRequest('GET', $url, [ |
|
|
|
|
|
|
45
|
|
|
'save_to' => $directory . '/' . basename($url) |
|
46
|
|
|
]); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$results = Pool::batch($client, $requests); |
|
50
|
|
|
|
|
51
|
|
|
// Retry with PNG |
|
52
|
|
|
$retryRequests = []; |
|
53
|
|
|
foreach ($results->getFailures() as $e) { |
|
|
|
|
|
|
54
|
|
|
// Delete failed files |
|
55
|
|
|
unlink($directory . '/' . basename($e->getRequest()->getUrl())); |
|
56
|
|
|
|
|
57
|
|
|
$urlPng = str_replace('.jpg', '.png', $e->getRequest()->getUrl()); |
|
58
|
|
|
$statusCode = $e->getResponse()->getStatusCode(); |
|
59
|
|
|
|
|
60
|
|
|
if ($statusCode == 404) { |
|
61
|
|
|
$retryRequests[] = $client->createRequest('GET', $urlPng, [ |
|
|
|
|
|
|
62
|
|
|
'save_to' => $directory . '/' . basename($urlPng) |
|
63
|
|
|
]); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
Pool::batch($client, $retryRequests); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* |
|
73
|
|
|
* @param $offset |
|
74
|
|
|
* |
|
75
|
|
|
* @return bool |
|
76
|
|
|
*/ |
|
77
|
|
|
public function offsetExists($offset) |
|
78
|
|
|
{ |
|
79
|
|
|
return isset($this->wallpapers[$offset]); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* |
|
84
|
|
|
* @param $offset |
|
85
|
|
|
* |
|
86
|
|
|
* @return null|Wallpaper |
|
87
|
|
|
*/ |
|
88
|
|
|
public function offsetGet($offset) |
|
89
|
|
|
{ |
|
90
|
|
|
return isset($this->wallpapers[$offset]) ? $this->wallpapers[$offset] : null; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* |
|
95
|
|
|
* @param $offset |
|
96
|
|
|
* @param Wallpaper $value |
|
97
|
|
|
* |
|
98
|
|
|
* @throws WallhavenException |
|
99
|
|
|
*/ |
|
100
|
|
|
public function offsetSet($offset, $value) |
|
101
|
|
|
{ |
|
102
|
|
|
if (!$value instanceof Wallpaper) { |
|
103
|
|
|
throw new WallhavenException("Not a Wallpaper object."); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
if (is_null($offset)) { |
|
107
|
|
|
$this->wallpapers[] = $value; |
|
108
|
|
|
} else { |
|
109
|
|
|
$this->wallpapers[$offset] = $value; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @param $offset |
|
115
|
|
|
*/ |
|
116
|
|
|
public function offsetUnset($offset) |
|
117
|
|
|
{ |
|
118
|
|
|
unset($this->wallpapers[$offset]); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @return \ArrayIterator |
|
123
|
|
|
*/ |
|
124
|
|
|
public function getIterator() |
|
125
|
|
|
{ |
|
126
|
|
|
return new \ArrayIterator($this->wallpapers); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @return int Wallpaper count. |
|
131
|
|
|
*/ |
|
132
|
|
|
public function count() |
|
133
|
|
|
{ |
|
134
|
|
|
return count($this->wallpapers); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* All all items from the given list to the current list. |
|
139
|
|
|
* |
|
140
|
|
|
* @param WallpaperList $wallpaperList |
|
141
|
|
|
*/ |
|
142
|
|
|
public function addAll(WallpaperList $wallpaperList) |
|
143
|
|
|
{ |
|
144
|
|
|
foreach ($wallpaperList as $wallpaper) { |
|
145
|
|
|
$this->wallpapers[] = $wallpaper; |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.