|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Distilleries\Contentful\Commands\Import; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Carbon; |
|
6
|
|
|
use Illuminate\Console\Command; |
|
7
|
|
|
use Illuminate\Support\Collection; |
|
8
|
|
|
use Illuminate\Support\Facades\DB; |
|
9
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
|
10
|
|
|
use Distilleries\Contentful\Api\ManagementApi; |
|
11
|
|
|
|
|
12
|
|
|
class ImportPublish extends Command |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* {@inheritdoc} |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $signature = 'contentful:import-publish'; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* {@inheritdoc} |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $description = 'Publish imported entries in Contentful'; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Management API implementation. |
|
26
|
|
|
* |
|
27
|
|
|
* @var \Distilleries\Contentful\Api\ManagementApi |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $api; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* ImportPublish command constructor. |
|
33
|
|
|
* |
|
34
|
|
|
* @param \Distilleries\Contentful\Api\ManagementApi $managementApi |
|
35
|
|
|
* @return void |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct(ManagementApi $managementApi) |
|
38
|
|
|
{ |
|
39
|
|
|
parent::__construct(); |
|
40
|
|
|
|
|
41
|
|
|
$this->api = $managementApi; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Execute the console command. |
|
46
|
|
|
* |
|
47
|
|
|
* @return void |
|
48
|
|
|
*/ |
|
49
|
|
|
public function handle() |
|
50
|
|
|
{ |
|
51
|
|
|
$this->warn('Publish imported Contentful entries...'); |
|
52
|
|
|
|
|
53
|
|
|
$importedEntries = DB::table('import_entries')->get(); |
|
54
|
|
|
|
|
55
|
|
|
$this->publish($importedEntries); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Publish given imported entries collection. |
|
60
|
|
|
* |
|
61
|
|
|
* @param \Illuminate\Support\Collection $importedEntries |
|
62
|
|
|
* @return void |
|
63
|
|
|
*/ |
|
64
|
|
|
private function publish(Collection $importedEntries) |
|
65
|
|
|
{ |
|
66
|
|
|
$bar = $this->output->createProgressBar(count($importedEntries)); |
|
67
|
|
|
|
|
68
|
|
|
foreach ($importedEntries as $importedEntry) { |
|
69
|
|
|
if (empty($importedEntry->published_at)) { |
|
70
|
|
|
try { |
|
71
|
|
|
if ($importedEntry->contentful_type === 'asset') { |
|
72
|
|
|
$this->api->publishAsset($importedEntry->contentful_id, $importedEntry->version + 1); |
|
73
|
|
|
} else { |
|
74
|
|
|
$this->api->publishEntry($importedEntry->contentful_id, $importedEntry->version); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
DB::table('import_entries')->where('contentful_id', '=', $importedEntry->contentful_id)->update(['published_at' => Carbon::now()]); |
|
78
|
|
|
} catch (GuzzleException $e) { |
|
79
|
|
|
DB::table('import_entries')->where('contentful_id', '=', $importedEntry->contentful_id)->update(['version' => $importedEntry->version + 1]); |
|
80
|
|
|
|
|
81
|
|
|
$this->error($e->getMessage()); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
$bar->advance(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$bar->finish(); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|