Completed
Push — master ( de9b37...0fd0c6 )
by Sam
10s
created

SyncContentsCommand::getTotalQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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