AbstractImporter   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
c 1
b 0
f 0
dl 0
loc 92
ccs 0
cts 43
cp 0
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A indexFieldsWithLocales() 0 10 3
A mapEntryLink() 0 7 1
A storeImport() 0 21 3
1
<?php
2
3
namespace Distilleries\Contentful\Import;
4
5
use Illuminate\Support\Carbon;
6
use Illuminate\Support\Facades\DB;
7
use Distilleries\Contentful\Models\Locale;
8
use Distilleries\Contentful\Api\ManagementApi;
9
10
abstract class AbstractImporter
11
{
12
    /**
13
     * Management API implementation.
14
     *
15
     * @var \Distilleries\Contentful\Api\ManagementApi
16
     */
17
    protected $api;
18
19
    /**
20
     * Importer constructor.
21
     *
22
     * @param  \Distilleries\Contentful\Api\ManagementApi  $api
23
     * @return void
24
     */
25
    public function __construct(ManagementApi $api)
26
    {
27
        $this->api = $api;
28
    }
29
30
    /**
31
     * Import given data to Contentful (API call).
32
     *
33
     * @param  array  $data
34
     * @return array
35
     * @throws \GuzzleHttp\Exception\GuzzleException
36
     */
37
    abstract public function import(array $data): array;
38
39
    /**
40
     * Store entry in imported entries table.
41
     *
42
     * @param  array  $entry
43
     * @return void
44
     */
45
    protected function storeImport(array $entry)
46
    {
47
        $importEntry = DB::table('import_entries')
48
            ->where('contentful_id', '=', $entry['sys']['id'])
49
            ->first();
50
51
        if (empty($importEntry)) {
52
            DB::table('import_entries')
53
                ->insert([
54
                    'contentful_id' => $entry['sys']['id'],
55
                    'contentful_type' => ($entry['sys']['type'] === 'Asset') ? 'asset' : $entry['sys']['contentType']['sys']['id'],
56
                    'version' => (int) $entry['sys']['version'],
57
                    'imported_at' => Carbon::now(),
58
                ]);
59
        } else {
60
            DB::table('import_entries')
61
                ->where('contentful_id', '=', $entry['sys']['id'])
62
                ->update([
63
                    'version' => (int) $entry['sys']['version'],
64
                    'imported_at' => Carbon::now(),
65
                    'published_at' => null,
66
                ]);
67
        }
68
    }
69
70
    /**
71
     * Index given data with locale to match Contentful fields structure.
72
     *
73
     * @param  array  $data
74
     * @param  string  $locale
75
     * @return array
76
     */
77
    protected function indexFieldsWithLocales(array $data, string $locale = ''): array
78
    {
79
        $indexed = [];
80
81
        $locale = ! empty($locale) ? $locale : Locale::default();
82
        foreach ($data as $field => $value) {
83
            $indexed[$field][$locale] = $value;
84
        }
85
86
        return $indexed;
87
    }
88
89
    /**
90
     * Return entry Link signature.
91
     *
92
     * @param  array  $entry
93
     * @return array
94
     */
95
    protected function mapEntryLink(array $entry): array
96
    {
97
        return [
98
            'sys' => [
99
                'id' => $entry['sys']['id'],
100
                'type' => 'Link',
101
                'linkType' => $entry['sys']['type'],
102
            ],
103
        ];
104
    }
105
}
106