EntryHandler::unarchive()   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\EntriesRepository;
7
8
class EntryHandler
9
{
10
    /**
11
     * Entries repository implementation.
12
     *
13
     * @var \Distilleries\Contentful\Repositories\EntriesRepository
14
     */
15
    protected $entries;
16
17
    /**
18
     * EntryHandler constructor.
19
     *
20
     * @return void
21
     */
22
    public function __construct()
23
    {
24
        $this->entries = new EntriesRepository;
25
    }
26
27
    /**
28
     * Handle an incoming ContentManagementEntry 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 entry.
52
     *
53
     * @param  array  $payload
54
     * @return void
55
     * @throws \Exception
56
     */
57
    protected function auto_save($payload)
58
    {
59
        $this->upsertEntry($payload);
60
    }
61
62
    /**
63
     * Save entry.
64
     *
65
     * @param  array  $payload
66
     * @return void
67
     * @throws \Exception
68
     */
69
    protected function save($payload)
70
    {
71
        $this->upsertEntry($payload);
72
    }
73
74
    /**
75
     * Create entry.
76
     *
77
     * @param  array  $payload
78
     * @return void
79
     * @throws \Exception
80
     */
81
    protected function create($payload)
82
    {
83
        $this->upsertEntry($payload);
84
    }
85
86
    /**
87
     * Archive entry.
88
     *
89
     * @param  array  $payload
90
     * @return void
91
     * @throws \Exception
92
     */
93
    protected function archive($payload)
94
    {
95
        $this->deleteEntry($payload);
96
    }
97
98
    /**
99
     * Un-archive entry.
100
     *
101
     * @param  array  $payload
102
     * @return void
103
     * @throws \Exception
104
     */
105
    protected function unarchive($payload)
106
    {
107
        $this->upsertEntry($payload);
108
    }
109
110
    /**
111
     * Publish entry.
112
     *
113
     * @param  array  $payload
114
     * @return void
115
     * @throws \Exception
116
     */
117
    protected function publish($payload)
118
    {
119
        $this->upsertEntry($payload);
120
    }
121
122
    /**
123
     * Un-publish entry.
124
     *
125
     * @param  array  $payload
126
     * @return void
127
     * @throws \Exception
128
     */
129
    protected function unpublish($payload)
130
    {
131
        $this->deleteEntry($payload);
132
    }
133
134
    /**
135
     * Delete entry.
136
     *
137
     * @param  array  $payload
138
     * @return void
139
     * @throws \Exception
140
     */
141
    protected function delete($payload)
142
    {
143
        $this->deleteEntry($payload);
144
    }
145
146
    // --------------------------------------------------------------------------------
147
    // --------------------------------------------------------------------------------
148
    // --------------------------------------------------------------------------------
149
150
    /**
151
     * Upsert entry in DB.
152
     *
153
     * @param  array  $payload
154
     * @return void
155
     * @throws \Exception
156
     */
157
158
    protected function upsertEntry($payload)
159
    {
160
        $locales = Locale::all();
161
        $locales = is_array($locales) ? collect($locales) : $locales;
0 ignored issues
show
introduced by
The condition is_array($locales) is always false.
Loading history...
162
        $this->entries->toContentfulModel($payload, $locales);
163
    }
164
165
    /**
166
     * Delete entry from DB.
167
     *
168
     * @param  array  $payload
169
     * @return void
170
     * @throws \Exception
171
     */
172
    protected function deleteEntry($payload)
173
    {
174
        $this->entries->delete($payload);
175
    }
176
}
177