Completed
Push — master ( ddf2b7...f0be57 )
by Maxime
02:38
created

SyncData   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 139
Duplicated Lines 17.27 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 24
loc 139
ccs 0
cts 70
cp 0
rs 10
c 0
b 0
f 0
wmc 14
lcom 1
cbo 6

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A handle() 0 16 2
A setNewRelease() 0 7 1
A syncAssets() 12 12 3
A saveAssets() 0 12 2
A syncEntries() 12 12 3
A saveEntries() 0 12 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Distilleries\Contentful\Commands\Sync;
4
5
use Distilleries\Contentful\Models\Release;
6
use Illuminate\Console\Command;
7
use Illuminate\Support\Facades\DB;
8
use Distilleries\Contentful\Api\SyncApi;
9
use GuzzleHttp\Exception\GuzzleException;
10
11
class SyncData extends Command
12
{
13
    use Traits\SyncTrait;
14
15
    /**
16
     * {@inheritdoc}
17
     */
18
    protected $signature = 'contentful:sync-data {--preview}';
19
20
    /**
21
     * {@inheritdoc}
22
     */
23
    protected $description = 'Synchronize Contentful entries and assets';
24
25
    /**
26
     * Contentful Sync API implementation.
27
     *
28
     * @var \Distilleries\Contentful\Api\SyncApi
29
     */
30
    protected $api;
31
32
    /**
33
     * SyncData command constructor.
34
     *
35
     * @param  \Distilleries\Contentful\Api\SyncApi  $api
36
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
37
     */
38
    public function __construct(SyncApi $api)
39
    {
40
        parent::__construct();
41
42
        $this->api = $api;
43
    }
44
45
    /**
46
     * Execute the console command.
47
     *
48
     * @return void
49
     */
50
    public function handle(Release $release)
51
    {
52
        if ($this->option('preview')) {
53
            use_contentful_preview();
54
        }
55
56
        $this->setNewRelease($release);
57
        $this->line('Clean previous synced data');
58
        DB::table('sync_entries')->truncate();
59
60
        $this->line('Syncing assets...');
61
        $this->syncAssets();
62
63
        $this->line('Syncing entries...');
64
        $this->syncEntries();
65
    }
66
67
68
    protected function setNewRelease(Release $release)
69
    {
70
        (new $release)->where('current', true)->update(['current' => false]);
71
72
        $release->current = true;
73
        $release->save();
74
    }
75
76
    /**
77
     * Synchronize assets via Sync API and store into DB for further use.
78
     *
79
     * @return void
80
     */
81 View Code Duplication
    protected function syncAssets()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
82
    {
83
        try {
84
            $assets = $this->api->syncInitial('Asset');
85
            while (! empty($assets)) {
86
                $this->saveAssets($assets);
87
                $assets = $this->api->syncNext();
88
            }
89
        } catch (GuzzleException $e) {
90
            $this->error($e->getMessage());
91
        }
92
    }
93
94
    /**
95
     * Save given Contentful assets.
96
     *
97
     * @param  array  $assets
98
     * @return void
99
     */
100
    protected function saveAssets(array $assets)
101
    {
102
        DB::transaction(function () use ($assets) {
103
            foreach ($assets as $asset) {
104
                DB::table('sync_entries')->insert([
105
                    'contentful_id' => $asset['sys']['id'],
106
                    'contentful_type' => 'asset',
107
                    'payload' => json_encode($asset),
108
                ]);
109
            }
110
        });
111
    }
112
113
    /**
114
     * Synchronize entries via Sync API and store into DB for further use.
115
     *
116
     * @return void
117
     */
118 View Code Duplication
    protected function syncEntries()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
119
    {
120
        try {
121
            $entries = $this->api->syncInitial('Entry');
122
            while (! empty($entries)) {
123
                $this->saveEntries($entries);
124
                $entries = $this->api->syncNext();
125
            }
126
        } catch (GuzzleException $e) {
127
            $this->error($e->getMessage());
128
        }
129
    }
130
131
    /**
132
     * Save given Contentful entries.
133
     *
134
     * @param  array  $entries
135
     * @return void
136
     */
137
    protected function saveEntries(array $entries)
138
    {
139
        DB::transaction(function () use ($entries) {
140
            foreach ($entries as $entry) {
141
                DB::table('sync_entries')->insert([
142
                    'contentful_id' => $entry['sys']['id'],
143
                    'contentful_type' => $entry['sys']['contentType']['sys']['id'],
144
                    'payload' => json_encode($entry),
145
                ]);
146
            }
147
        });
148
    }
149
}
150