AssetHandler::save()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Distilleries\Contentful\Webhook;
4
5
use Distilleries\Contentful\Models\Locale;
6
use Distilleries\Contentful\Repositories\AssetsRepository;
7
8
class AssetHandler
9
{
10
    /**
11
     * Assets repository implementation.
12
     *
13
     * @var \Distilleries\Contentful\Repositories\AssetsRepository
14
     */
15
    protected $assets;
16
17
    /**
18
     * AssetHandler constructor.
19
     *
20
     * @return void
21
     */
22
    public function __construct()
23
    {
24
        $this->assets = new AssetsRepository;
25
    }
26
27
    /**
28
     * Handle an incoming ContentManagementAsset request.
29
     * (create, save, auto_save, archive, unarchive, publish, unpublish, delete)
30
     *
31
     * @param  string  $action
32
     * @param  array  $payload
33
     * @param  boolean  $isPreview
34
     * @return void
35
     */
36
    public function handle(string $action, array $payload, bool $isPreview)
37
    {
38
        $actionMethods = ['create', 'archive', 'unarchive', 'publish', 'unpublish', 'delete'];
39
        $actionMethods = ! empty($isPreview) ? array_merge($actionMethods, ['save', 'auto_save']) : $actionMethods;
40
41
        if (method_exists($this, $action) && in_array($action, $actionMethods)) {
42
            $this->$action($payload);
43
        }
44
    }
45
46
    // --------------------------------------------------------------------------------
47
    // --------------------------------------------------------------------------------
48
    // --------------------------------------------------------------------------------
49
50
    /**
51
     * Auto-save asset.
52
     *
53
     * @param  array  $payload
54
     * @return void
55
     */
56
    protected function auto_save($payload)
57
    {
58
        $this->upsertAsset($payload);
59
    }
60
61
    /**
62
     * Save asset.
63
     *
64
     * @param  array  $payload
65
     * @return void
66
     */
67
    protected function save($payload)
68
    {
69
        $this->upsertAsset($payload);
70
    }
71
72
    /**
73
     * Create asset.
74
     *
75
     * @param  array  $payload
76
     * @return void
77
     */
78
    protected function create($payload)
79
    {
80
        $this->upsertAsset($payload);
81
    }
82
83
    /**
84
     * Archive asset.
85
     *
86
     * @param  array  $payload
87
     * @return void
88
     */
89
    protected function archive($payload)
90
    {
91
        $this->deleteAsset($payload);
92
    }
93
94
    /**
95
     * Un-archive asset.
96
     *
97
     * @param  array  $payload
98
     * @return void
99
     */
100
    protected function unarchive($payload)
101
    {
102
        $this->upsertAsset($payload);
103
    }
104
105
    /**
106
     * Publish asset.
107
     *
108
     * @param  array  $payload
109
     * @return void
110
     */
111
    protected function publish($payload)
112
    {
113
        $this->upsertAsset($payload);
114
    }
115
116
    /**
117
     * Un-publish asset.
118
     *
119
     * @param  array  $payload
120
     * @return void
121
     */
122
    protected function unpublish($payload)
123
    {
124
        $this->deleteAsset($payload);
125
    }
126
127
    /**
128
     * Delete asset.
129
     *
130
     * @param  array  $payload
131
     * @return void
132
     */
133
    protected function delete($payload)
134
    {
135
        $this->deleteAsset($payload);
136
    }
137
138
    // --------------------------------------------------------------------------------
139
    // --------------------------------------------------------------------------------
140
    // --------------------------------------------------------------------------------
141
142
    /**
143
     * Upsert asset in DB.
144
     *
145
     * @param  array  $payload
146
     * @return void
147
     */
148
149
    protected function upsertAsset($payload)
150
    {
151
        $locales = Locale::all();
152
        $locales = is_array($locales) ? collect($locales) : $locales;
0 ignored issues
show
introduced by
The condition is_array($locales) is always false.
Loading history...
153
        $this->assets->toContentfulModel($payload, $locales);
154
    }
155
156
    /**
157
     * Delete asset from DB.
158
     *
159
     * @param  array  $payload
160
     * @return void
161
     */
162
    protected function deleteAsset($payload)
163
    {
164
        $this->assets->delete($payload['sys']['id']);
165
    }
166
}
167