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