TranslatableObserver::updated()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace BBSLab\NovaTranslation\Models\Observers;
4
5
use BBSLab\NovaTranslation\Models\Contracts\IsTranslatable;
6
use BBSLab\NovaTranslation\Models\Locale;
7
use BBSLab\NovaTranslation\Models\Translation;
8
use BBSLab\NovaTranslation\NovaTranslation;
9
10
class TranslatableObserver
11
{
12
    /**
13
     * Handle the Translatable "created" event.
14
     *
15
     * @param  \BBSLab\NovaTranslation\Models\Contracts\IsTranslatable  $translatable
16
     * @return void
17
     * @throws \Exception
18
     */
19
    public function created(IsTranslatable $translatable)
20
    {
21
        $translation = $translatable->upsertTranslationEntry(
22
            ($currentLocale = NovaTranslation::currentLocale())->getKey(),
23
            $translatable->getKey()
24
        );
25
26
        if (! in_array(get_class($translatable), NovaTranslation::translatableModels())) {
27
            return;
28
        }
29
30
        $attributes = $translatable->only(
31
            $translatable->getOnCreateTranslatable()
32
        );
33
34
        $translatable::withoutEvents(function () use ($translatable, $translation, $currentLocale, $attributes) {
35
            NovaTranslation::otherLocales($currentLocale)->each(function (Locale $locale) use (
36
                $translatable, $translation, $attributes
37
            ) {
38
                /** @var \BBSLab\NovaTranslation\Models\Contracts\IsTranslatable $model */
39
                $model = $translatable->query()->create($attributes);
40
                $model->upsertTranslationEntry(
41
                    $locale->getkey(), $translatable->getKey(), $translation->translation_id
42
                );
43
            });
44
        });
45
    }
46
47
    /**
48
     * Handle the Translatable "updated" event.
49
     *
50
     * @param  \BBSLab\NovaTranslation\Models\Contracts\IsTranslatable  $translatable
51
     * @return void
52
     */
53
    public function updated(IsTranslatable $translatable)
54
    {
55
        $attributes = $translatable->only(
56
            $translatable->getNonTranslatable()
57
        );
58
59
        $translatable::withoutEvents(function () use ($translatable, $attributes) {
60
            $translatable->translations->each(function (Translation $translation) use ($attributes) {
0 ignored issues
show
Bug introduced by
Accessing translations on the interface BBSLab\NovaTranslation\M...ontracts\IsTranslatable suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
61
                $translation->translatable->update($attributes);
62
            });
63
        });
64
    }
65
66
    /**
67
     * Handle the Translatable "deleted" event.
68
     *
69
     * @param  \BBSLab\NovaTranslation\Models\Contracts\IsTranslatable  $translatable
70
     * @return void
71
     * @throws \Exception
72
     */
73
    public function deleted(IsTranslatable $translatable)
74
    {
75
        $translatable->load('translations');
76
        $translatable->translation->delete();
0 ignored issues
show
Bug introduced by
Accessing translation on the interface BBSLab\NovaTranslation\M...ontracts\IsTranslatable suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
77
78
        if (! in_array(get_class($translatable), NovaTranslation::translatableModels())) {
79
            return;
80
        }
81
82
        // Prevent deleted translation to delete other translations again.
83
        if ($translatable->isDeletingTranslation()) {
84
            return;
85
        }
86
87
        $translatable->translations->each(function (Translation $translation) {
0 ignored issues
show
Bug introduced by
Accessing translations on the interface BBSLab\NovaTranslation\M...ontracts\IsTranslatable suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
88
            $translatable = $translation->translatable;
89
            $translatable->deletingTranslation();
90
            $translatable->delete();
91
        });
92
    }
93
}
94