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

TranslatableObserver   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 7
dl 0
loc 85
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A created() 0 25 2
A updated() 0 13 1
A deleted() 0 23 3
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
            0
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($locale->getkey(), $translation->translation_id);
41
            });
42
        });
43
    }
44
45
    /**
46
     * Handle the Translatable "updated" event.
47
     *
48
     * @param  \BBSLab\NovaTranslation\Models\Contracts\IsTranslatable  $translatable
49
     * @return void
50
     */
51
    public function updated(IsTranslatable $translatable)
52
    {
53
        $attributes = $translatable->only(
54
            $translatable->getNonTranslatable()
55
        );
56
57
        $translatable::withoutEvents(function () use ($translatable, $attributes) {
58
            $translatable->translations()->each(function ($model) use ($translatable, $attributes) {
59
                /** @var \BBSLab\NovaTranslation\Models\Contracts\IsTranslatable $model */
60
                $model->update($attributes);
61
            });
62
        });
63
    }
64
65
    /**
66
     * Handle the Translatable "deleted" event.
67
     *
68
     * @param  \BBSLab\NovaTranslation\Models\Contracts\IsTranslatable  $translatable
69
     * @return void
70
     */
71
    public function deleted(IsTranslatable $translatable)
72
    {
73
        Translation::query()
74
            ->where('translatable_id', '=', $translatable->translation->translatable_id)
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...
75
            ->where('translatable_type', '=', $translatable->translation->translatable_type)
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...
76
            ->where('translation_id', '=', $translatable->translation->translation_id)
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
            ->where('locale_id', '=', $translatable->translation->locale_id)
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...
78
            ->delete();
79
80
        if (! in_array(get_class($translatable), NovaTranslation::translatableModels())) {
81
            return;
82
        }
83
84
        // Prevent deleted translation to delete other translations again.
85
        if ($translatable->deleting_translation) {
0 ignored issues
show
Bug introduced by
Accessing deleting_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...
86
            return;
87
        }
88
89
        $translatable->translations()->each(function (IsTranslatable $translatable) {
90
            $translatable->deleting_translation = true;
0 ignored issues
show
Bug introduced by
Accessing deleting_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...
91
            $translatable->delete();
92
        });
93
    }
94
}
95