SyncContentsCommand::handle()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
cc 4
nc 4
nop 0
1
<?php
2
3
namespace Digia\Lumen\ContentfulSync\Console\Commands;
4
5
use Contentful\Delivery\Query;
6
use Digia\JsonHelpers\JsonEncoder;
7
8
/**
9
 * Class SyncContentsCommand
10
 * @package Digia\Lumen\ContentfulSync\Console\Commands
11
 */
12
class SyncContentsCommand extends AbstractSyncCommand
13
{
14
15
    /**
16
     * @var string
17
     */
18
    protected $signature = 'contentful:contents:sync 
19
                            {contentTypes?* : The content types, e.g. "article" or "brand". You can specify multiple content types. Omit to synchronize all content types.}
20
                            {--batchSize=100 : The number of items to request from Contentful in one batch. Defaults to 100.} 
21
                            {--ignoreErrors : Whether to ignore errors when synchronizing, useful to get around circular references.}
22
                            {--ignoreExisting : Whether to ignore existing entries, i.e. only synchronize new entries.}';
23
24
    /**
25
     * @var string
26
     */
27
    protected $description = 'Synchronizes content from Contentful';
28
29
    /**
30
     * @var boolean
31
     */
32
    protected $ignoreErrors;
33
34
    /**
35
     * @inheritdoc
36
     */
37
    protected function getQuery(?string $contentType = null): Query
38
    {
39
        $query = new Query();
40
        $query->setSkip($this->skip);
41
        $query->setLimit($this->batchSize);
42
        $query->setContentType($contentType);
43
44
        return $query;
45
    }
46
47
    /**
48
     * @inheritdoc
49
     */
50
    public function handle()
51
    {
52
        parent::handle();
53
54
        // Parse options and arguments
55
        $this->ignoreErrors = (bool)$this->option('ignoreErrors');
56
        $contentTypes       = $this->argument('contentTypes');
57
58
        // Synchronize all content types in a particular order unless specific content types were specified
59
        if (!\is_array($contentTypes) || empty($contentTypes)) {
60
            $contentTypes = $this->contentTypes;
61
        }
62
63
        foreach ($contentTypes as $contentType) {
64
            // Reset counters before each content type
65
            $this->resetCounters();
66
67
            $this->synchronizeContentType($contentType);
68
        }
69
    }
70
71
    /**
72
     * @param string $contentType
73
     *
74
     * @throws \Throwable
75
     */
76
    protected function synchronizeContentType(string $contentType): void
77
    {
78
        $this->info('Synchronizing content of type "' . $contentType . '"...');
79
80
        $this->output->progressStart($this->getClient()->getEntries($this->getTotalQuery($contentType))->getTotal());
81
82
        do {
83
            $entries = $this->getClient()->getEntries($this->getQuery($contentType));
84
85
            // Process the current batch
86
            foreach ($entries as $entry) {
87
                // Optionally catch exceptions, depending on whether errors should be ignored
88
                try {
89
                    $this->contentfulSyncService->publishEntry(
90
                        $contentType,
91
                        JsonEncoder::encode($entry),
92
                        $this->ignoreExisting
93
                    );
94
95
                    $this->numSynchronized++;
96
97
                    $this->output->progressAdvance();
98
                } catch (\Throwable $e) {
99
                    $this->error("  Failed to synchronize {$entry->getId()}: {$e->getMessage()}");
100
101
                    if (!$this->ignoreErrors) {
102
                        throw $e;
103
                    }
104
                }
105
            }
106
107
            // Move on to the next batch
108
            $this->skip += $entries->getLimit();
109
        } while ($this->skip < $entries->getTotal());
110
111
        $this->output->progressFinish();
112
113
        $this->info("Done, synchronized {$this->numSynchronized} entries");
114
    }
115
}
116