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

LocaleDeleted::deleteTranslation()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 2
nc 2
nop 1
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