1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of laravel.su package. |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
declare(strict_types=1); |
10
|
|
|
|
11
|
|
|
namespace App\Console\Commands; |
12
|
|
|
|
13
|
|
|
use App\Models\Article; |
14
|
|
|
use App\Models\Bot; |
15
|
|
|
use App\Services\DataProviders\DataProviderInterface; |
16
|
|
|
use App\Services\DataProviders\ExternalArticle; |
17
|
|
|
use App\Services\DataProviders\Manager; |
18
|
|
|
use Carbon\Carbon; |
19
|
|
|
use Illuminate\Console\Command; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class DataProvidersImport. |
23
|
|
|
*/ |
24
|
|
|
class DataProvidersImport extends Command |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* The name and signature of the console command. |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $signature = 'data-providers:import'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The console command description. |
35
|
|
|
* |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $description = 'Run import articles from external services'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
private $published = []; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param Manager $manager |
47
|
|
|
* @throws \InvalidArgumentException |
48
|
|
|
*/ |
49
|
|
|
public function handle(Manager $manager) |
50
|
|
|
{ |
51
|
|
|
$a = Article::publishedByBot() |
|
|
|
|
52
|
|
|
->with('user') |
53
|
|
|
->latest('published_at') |
54
|
|
|
->first(); |
55
|
|
|
|
56
|
|
|
$articles = (array)($a ?? []); |
57
|
|
|
|
58
|
|
|
/** @var Article $article */ |
59
|
|
|
foreach ($articles as $article) { |
60
|
|
|
/** @var Bot $bot */ |
61
|
|
|
$bot = $article->user; |
62
|
|
|
|
63
|
|
|
$this->import($article->published_at, $manager->get($bot->provider)); |
|
|
|
|
64
|
|
|
|
65
|
|
|
$this->published[] = $bot->provider; |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$this->importNotPublished($manager); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param Manager $manager |
73
|
|
|
*/ |
74
|
|
|
private function importNotPublished(Manager $manager) |
75
|
|
|
{ |
76
|
|
|
foreach ($manager as $alias => $provider) { |
77
|
|
|
if (in_array($alias, $this->published, true)) { |
78
|
|
|
continue; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$this->import(Carbon::createFromTimestamp(0), $provider); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
private function import(\DateTime $time, DataProviderInterface $provider) |
86
|
|
|
{ |
87
|
|
|
/** @var ExternalArticle[] $latest */ |
88
|
|
|
$latest = $provider->getLatest($time); |
89
|
|
|
|
90
|
|
|
foreach ($latest as $external) { |
91
|
|
|
$article = new Article(); |
92
|
|
|
|
93
|
|
|
$article->user_id = 1; |
|
|
|
|
94
|
|
|
$article->user_type = Bot::class; |
|
|
|
|
95
|
|
|
|
96
|
|
|
$external->fill($article); |
97
|
|
|
|
98
|
|
|
$article->save(); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.