1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Propaganistas\LaravelDisposableEmail\Console; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Illuminate\Config\Repository as Config; |
7
|
|
|
use Illuminate\Console\Command; |
8
|
|
|
use Propaganistas\LaravelDisposableEmail\DisposableDomains; |
9
|
|
|
use Propaganistas\LaravelDisposableEmail\Traits\ParsesJson; |
10
|
|
|
|
11
|
|
|
class UpdateDisposableDomainsCommand extends Command |
12
|
|
|
{ |
13
|
|
|
use ParsesJson; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* The console command name. |
17
|
|
|
* For Laravel 5.0 |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $name = 'disposable:update'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The name and signature of the console command. |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $signature = 'disposable:update'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The console command description. |
32
|
|
|
* |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $description = 'Updates to the latest disposable email domains list'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The disposable domains service. |
39
|
|
|
* |
40
|
|
|
* @var \Propaganistas\LaravelDisposableEmail\DisposableDomains |
41
|
|
|
*/ |
42
|
|
|
protected $disposableDomains; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* The config service. |
46
|
|
|
* |
47
|
|
|
* @var \Illuminate\Config\Repository |
48
|
|
|
*/ |
49
|
|
|
protected $config; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* UpdateDisposableDomainsCommand constructor. |
53
|
|
|
* |
54
|
|
|
* @param \Illuminate\Config\Repository $config |
55
|
|
|
*/ |
56
|
9 |
|
public function __construct(Config $config) |
57
|
|
|
{ |
58
|
9 |
|
parent::__construct(); |
59
|
|
|
|
60
|
9 |
|
$this->config = $config; |
61
|
9 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Execute the console command. |
65
|
|
|
* |
66
|
|
|
* @param \Propaganistas\LaravelDisposableEmail\DisposableDomains $disposable |
67
|
|
|
* @return void |
68
|
|
|
*/ |
69
|
9 |
|
public function handle(DisposableDomains $disposable) |
70
|
|
|
{ |
71
|
9 |
|
$this->disposableDomains = $disposable; |
72
|
|
|
|
73
|
9 |
|
$this->line('Fetching from source...'); |
74
|
|
|
|
75
|
9 |
|
$data = $this->fetchFromSource(); |
76
|
|
|
|
77
|
9 |
|
if ($data === false) { |
78
|
3 |
|
$this->error('Aborting.'); |
79
|
|
|
|
80
|
3 |
|
return; |
81
|
|
|
} |
82
|
|
|
|
83
|
6 |
|
if (! $this->isValidData($data)) { |
84
|
|
|
$this->error('Source returned invalid JSON. Aborting.'); |
85
|
|
|
|
86
|
|
|
return; |
87
|
|
|
} |
88
|
|
|
|
89
|
6 |
|
$this->line('Saving response to storage...'); |
90
|
|
|
|
91
|
6 |
|
if (! $this->save($data)) { |
92
|
|
|
$this->error('Couldn\'t write to storage ('.$this->disposableDomains->getStoragePath().')! Aborting.'); |
93
|
|
|
|
94
|
|
|
return; |
95
|
|
|
} |
96
|
|
|
|
97
|
6 |
|
$this->info('Disposable domains list updated successfully.'); |
98
|
6 |
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Fetches from the source URL. |
102
|
|
|
* |
103
|
|
|
* @return string|bool |
104
|
|
|
*/ |
105
|
9 |
|
protected function fetchFromSource() |
106
|
|
|
{ |
107
|
9 |
|
$sourceUrl = $this->config->get('disposable-email.source'); |
108
|
|
|
|
109
|
|
|
try { |
110
|
9 |
|
$content = file_get_contents($sourceUrl); |
111
|
|
|
|
112
|
6 |
|
if ($content !== false) { |
113
|
6 |
|
return $content; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$this->error('PHP failed to interpret the source URL ('.$sourceUrl.')'); |
117
|
3 |
|
} catch (Exception $e) { |
118
|
3 |
|
$this->error('Couldn\'t reach the source ('.$sourceUrl.').'); |
119
|
|
|
} |
120
|
|
|
|
121
|
3 |
|
return false; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Determines whether the data is valid JSON. |
126
|
|
|
* |
127
|
|
|
* @param string $data |
128
|
|
|
* @return bool |
129
|
|
|
*/ |
130
|
6 |
|
protected function isValidData($data) |
131
|
|
|
{ |
132
|
6 |
|
if ($this->parseJson($data)) { |
133
|
6 |
|
return true; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return false; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Saves received data to storage. |
141
|
|
|
* |
142
|
|
|
* @param string $data |
143
|
|
|
* @return bool |
144
|
|
|
*/ |
145
|
6 |
|
protected function save($data) |
146
|
|
|
{ |
147
|
6 |
|
if (file_put_contents($this->disposableDomains->getStoragePath(), $data) === false) { |
148
|
|
|
return false; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
// Flushing the cache will force it to refill itself in the next request. |
152
|
6 |
|
$this->disposableDomains->flushCache(); |
153
|
|
|
|
154
|
6 |
|
return true; |
155
|
|
|
} |
156
|
|
|
} |