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

LocaleCreated   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

3 Methods

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