|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Digia\Lumen\ContentfulSync\Console\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use Contentful\Delivery\Client\ClientInterface; |
|
6
|
|
|
use Contentful\Delivery\Query; |
|
7
|
|
|
use Digia\Lumen\ContentfulSync\Contracts\ContentfulSyncServiceContract; |
|
8
|
|
|
use Illuminate\Console\Command; |
|
9
|
|
|
use Jalle19\Laravel\LostInterfaces\Console\Command as CommandInterface; |
|
10
|
|
|
use Nord\Lumen\Contentful\ContentfulServiceContract; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class AbstractSyncCommand |
|
14
|
|
|
* @package Digia\Lumen\ContentfulSync\Console\Commands |
|
15
|
|
|
*/ |
|
16
|
|
|
abstract class AbstractSyncCommand extends Command implements CommandInterface |
|
17
|
|
|
{ |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var boolean |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $ignoreExisting; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var ContentfulServiceContract |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $contentfulService; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var ContentfulSyncServiceContract |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $contentfulSyncService; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var array |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $contentTypes; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var int |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $numSynchronized; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var int |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $skip; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var int |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $batchSize; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param null|string $contentType the content type, or null if not applicable |
|
56
|
|
|
* |
|
57
|
|
|
* @return Query the query used to fetch all entries/assets |
|
58
|
|
|
*/ |
|
59
|
|
|
abstract protected function getQuery(?string $contentType = null): Query; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param null|string $contentType the content type, or null if not applicable |
|
63
|
|
|
* |
|
64
|
|
|
* @return Query the query used to get the total number of items |
|
65
|
|
|
*/ |
|
66
|
|
|
protected function getTotalQuery(?string $contentType = null): Query |
|
67
|
|
|
{ |
|
68
|
|
|
$query = clone $this->getQuery($contentType); |
|
69
|
|
|
|
|
70
|
|
|
return $query->setSkip(0) |
|
71
|
|
|
->setLimit(1); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* AbstractSyncCommand constructor. |
|
76
|
|
|
* |
|
77
|
|
|
* @param array $contentTypes |
|
78
|
|
|
* @param ContentfulServiceContract $contentfulService |
|
79
|
|
|
* @param ContentfulSyncServiceContract $contentfulSyncService |
|
80
|
|
|
*/ |
|
81
|
|
|
public function __construct( |
|
82
|
|
|
array $contentTypes, |
|
83
|
|
|
ContentfulServiceContract $contentfulService, |
|
84
|
|
|
ContentfulSyncServiceContract $contentfulSyncService |
|
85
|
|
|
) { |
|
86
|
|
|
parent::__construct(); |
|
87
|
|
|
|
|
88
|
|
|
$this->contentTypes = $contentTypes; |
|
89
|
|
|
$this->contentfulService = $contentfulService; |
|
90
|
|
|
$this->contentfulSyncService = $contentfulSyncService; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @inheritdoc |
|
95
|
|
|
* |
|
96
|
|
|
* @throws \Throwable |
|
97
|
|
|
*/ |
|
98
|
|
|
public function handle() |
|
99
|
|
|
{ |
|
100
|
|
|
// Parse options and reset counters |
|
101
|
|
|
$this->batchSize = (int)$this->option('batchSize'); |
|
102
|
|
|
$this->ignoreExisting = (bool)$this->option('ignoreExisting'); |
|
103
|
|
|
|
|
104
|
|
|
$this->resetCounters(); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @return ClientInterface |
|
109
|
|
|
*/ |
|
110
|
|
|
protected function getClient(): ClientInterface |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->contentfulService->getClient(); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Resets the "skip" and "numSynchronized" counters |
|
117
|
|
|
*/ |
|
118
|
|
|
protected function resetCounters(): void |
|
119
|
|
|
{ |
|
120
|
|
|
$this->numSynchronized = 0; |
|
121
|
|
|
$this->skip = 0; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|