Completed
Push — master ( ca46fd...1d0b8e )
by
unknown
19:27 queued 09:34
created

LocaleDeleted   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 4
dl 0
loc 50
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handle() 0 9 1
A deleteTranslation() 0 13 2
1
<?php
2
3
namespace BBSLab\NovaTranslation\Jobs;
4
5
use BBSLab\NovaTranslation\Models\Locale;
6
use BBSLab\NovaTranslation\Models\Translation;
7
use Illuminate\Bus\Queueable;
8
use Illuminate\Contracts\Queue\ShouldQueue;
9
use Illuminate\Foundation\Bus\Dispatchable;
10
use Illuminate\Queue\InteractsWithQueue;
11
use Illuminate\Queue\SerializesModels;
12
13
class LocaleDeleted implements ShouldQueue
14
{
15
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
16
17
    /** @var \BBSLab\NovaTranslation\Models\Locale */
18
    protected $locale;
19
20
    /**
21
     * Create a new job instance.
22
     *
23
     * @param  \BBSLab\NovaTranslation\Models\Locale  $locale
24
     */
25
    public function __construct(Locale $locale)
26
    {
27
        $this->locale = $locale;
28
    }
29
30
    /**
31
     * Execute the job.
32
     *
33
     * @return void
34
     */
35
    public function handle()
36
    {
37
        Translation::query()
38
            ->where('locale_id', '=', $this->locale->getKey())
39
            ->cursor()
40
            ->each(function (Translation $translation) {
41
                $this->deleteTranslation($translation);
42
            });
43
    }
44
45
    /**
46
     * @param  \BBSLab\NovaTranslation\Models\Translation  $translation
47
     * @throws \Exception
48
     */
49
    protected function deleteTranslation(Translation $translation)
50
    {
51
        if (! empty($translation->translatable)) {
52
            $translation->translatable->delete();
53
        }
54
55
        Translation::query()
56
            ->where('translation_id', '=', $translation->translation_id)
57
            ->where('translatable_id', '=', $translation->translatable_id)
58
            ->where('translatable_type', '=', $translation->translatable_type)
59
            ->where('locale_id', '=', $translation->locale_id)
60
            ->delete();
61
    }
62
}
63