Completed
Push — master ( 79fd8a...81e819 )
by Christian
05:05
created

TranslationRepository::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 12
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 18
ccs 13
cts 13
cp 1
crap 1
rs 9.8666
1
<?php
2
3
namespace Omatech\Mage\Core\Repositories;
4
5
use Omatech\Lars\BaseRepository;
6
use Illuminate\Support\Facades\DB;
7
use Omatech\Mage\Core\Models\LanguageLine;
8
use Omatech\Mage\Core\Events\Translations\TranslationCreated;
9
use Omatech\Mage\Core\Events\Translations\TranslationDeleted;
10
use Omatech\Mage\Core\Events\Translations\TranslationUpdated;
11
use Omatech\Mage\Core\Domains\Translations\Contracts\TranslationInterface;
12
use Omatech\Mage\Core\Domains\Translations\Contracts\AllTranslationInterface;
13
use Omatech\Mage\Core\Domains\Translations\Contracts\CreateTranslationInterface;
14
use Omatech\Mage\Core\Domains\Translations\Contracts\DeleteTranslationInterface;
15
use Omatech\Mage\Core\Domains\Translations\Contracts\ExistsTranslationInterface;
16
use Omatech\Mage\Core\Domains\Translations\Contracts\UniqueTranslationInterface;
17
use Omatech\Mage\Core\Domains\Translations\Contracts\UpdateTranslationInterface;
18
19
class TranslationRepository extends BaseRepository implements
20
    AllTranslationInterface,
21
    CreateTranslationInterface,
22
    DeleteTranslationInterface,
23
    UpdateTranslationInterface,
24
    ExistsTranslationInterface,
25
    UniqueTranslationInterface
26
{
27
    /**
28
     * @return string
29
     */
30 11
    public function model(): string
31
    {
32 11
        return LanguageLine::class;
33
    }
34
35
    /**
36
     * @param $locales
37
     * @return mixed
38
     */
39 1
    public function get($locales)
40
    {
41 1
        $select = ['id', 'group', 'key'];
42
43 1
        foreach ($locales as $locale) {
44 1
            $select[] = "text->$locale as $locale";
45
        }
46
47 1
        return $this->query()
48 1
            ->select($select)
49 1
            ->paginate()
50 1
            ->toArray();
51
    }
52
53
    /**
54
     * @param int $id
55
     * @return TranslationInterface|null
56
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
57
     */
58 2
    public function find(int $id): ?TranslationInterface
59
    {
60 2
        $translation = $this->query()->find($id);
61
62 2
        if ($translation === null) {
63 1
            return null;
64
        }
65
66 1
        $translation = app()->make(TranslationInterface::class)::fromArray([
0 ignored issues
show
Bug introduced by
The method fromArray() does not exist on Omatech\Mage\Core\Domain...ts\TranslationInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Omatech\Mage\Core\Domain...ts\TranslationInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

66
        $translation = app()->make(TranslationInterface::class)::/** @scrutinizer ignore-call */ fromArray([
Loading history...
67 1
            'id'           => $translation->id,
68 1
            'key'          => $translation->group . '.' . $translation->key,
69 1
            'translations' => $translation->text,
70 1
            'sync_at'      => $translation->sync_at,
71 1
            'created_at'   => $translation->created_at,
72 1
            'updated_at'   => $translation->updated_at
73
        ]);
74
75 1
        return $translation;
76
    }
77
78
    /**
79
     * @param TranslationInterface $translation
80
     * @return bool
81
     */
82 9
    public function create(TranslationInterface $translation): bool
83
    {
84 9
        $created = $this->query()->create([
85 9
            'group' => $translation->getGroup(),
0 ignored issues
show
Bug introduced by
The method getGroup() does not exist on Omatech\Mage\Core\Domain...ts\TranslationInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Omatech\Mage\Core\Domain...ts\TranslationInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

85
            'group' => $translation->/** @scrutinizer ignore-call */ getGroup(),
Loading history...
86 9
            'key'   => $translation->getKey(),
0 ignored issues
show
Bug introduced by
The method getKey() does not exist on Omatech\Mage\Core\Domain...ts\TranslationInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Omatech\Mage\Core\Domain...ts\TranslationInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

86
            'key'   => $translation->/** @scrutinizer ignore-call */ getKey(),
Loading history...
87 9
            'text'  => $translation->getTranslations(),
0 ignored issues
show
Bug introduced by
The method getTranslations() does not exist on Omatech\Mage\Core\Domain...ts\TranslationInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Omatech\Mage\Core\Domain...ts\TranslationInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

87
            'text'  => $translation->/** @scrutinizer ignore-call */ getTranslations(),
Loading history...
88 9
            'sync_at' => $translation->getSyncAt()
0 ignored issues
show
Bug introduced by
The method getSyncAt() does not exist on Omatech\Mage\Core\Domain...ts\TranslationInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Omatech\Mage\Core\Domain...ts\TranslationInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

88
            'sync_at' => $translation->/** @scrutinizer ignore-call */ getSyncAt()
Loading history...
89
        ]);
90
91 9
        $translation->setId($created->id);
0 ignored issues
show
Bug introduced by
The method setId() does not exist on Omatech\Mage\Core\Domain...ts\TranslationInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Omatech\Mage\Core\Domain...ts\TranslationInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

91
        $translation->/** @scrutinizer ignore-call */ 
92
                      setId($created->id);
Loading history...
92 9
        $translation->setSyncAt($created->sync_at);
0 ignored issues
show
Bug introduced by
The method setSyncAt() does not exist on Omatech\Mage\Core\Domain...ts\TranslationInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Omatech\Mage\Core\Domain...ts\TranslationInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

92
        $translation->/** @scrutinizer ignore-call */ 
93
                      setSyncAt($created->sync_at);
Loading history...
93 9
        $translation->setCreatedAt($created->created_at);
0 ignored issues
show
Bug introduced by
The method setCreatedAt() does not exist on Omatech\Mage\Core\Domain...ts\TranslationInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Omatech\Mage\Core\Domain...ts\TranslationInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

93
        $translation->/** @scrutinizer ignore-call */ 
94
                      setCreatedAt($created->created_at);
Loading history...
94 9
        $translation->setUpdatedAt($created->updated_at);
0 ignored issues
show
Bug introduced by
The method setUpdatedAt() does not exist on Omatech\Mage\Core\Domain...ts\TranslationInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Omatech\Mage\Core\Domain...ts\TranslationInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

94
        $translation->/** @scrutinizer ignore-call */ 
95
                      setUpdatedAt($created->updated_at);
Loading history...
95
96 9
        event(new TranslationCreated($translation, $created->wasRecentlyCreated));
97
98 9
        return $created->wasRecentlyCreated;
99
    }
100
101
    /**
102
     * @param TranslationInterface $translation
103
     * @return bool
104
     */
105 9
    public function exists(TranslationInterface $translation): bool
106
    {
107 9
        return $this->query()
108
            ->where(static function ($q) use ($translation) {
109 9
                $q->where('group', $translation->getGroup())
110 9
                    ->where('key', $translation->getKey());
111 9
            })
112 9
            ->orWhere('id', $translation->getId())
0 ignored issues
show
Bug introduced by
The method getId() does not exist on Omatech\Mage\Core\Domain...ts\TranslationInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Omatech\Mage\Core\Domain...ts\TranslationInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

112
            ->orWhere('id', $translation->/** @scrutinizer ignore-call */ getId())
Loading history...
113 9
            ->exists();
114
    }
115
116
    /**
117
     * @param TranslationInterface $translation
118
     * @return bool
119
     */
120 3
    public function unique(TranslationInterface $translation): bool
121
    {
122 3
        return $this->query()
123 3
            ->where('group', $translation->getGroup())
124 3
            ->where('key', $translation->getKey())
125 3
            ->where('id', '!=', $translation->getId())
126 3
            ->exists();
127
    }
128
129
    /**
130
     * @param TranslationInterface $translation
131
     * @return bool
132
     */
133 1
    public function update(TranslationInterface $translation): bool
134
    {
135 1
        $updated = $this->query()->find($translation->getId());
136
137 1
        $updated->fill([
138 1
            'group' => $translation->getGroup(),
139 1
            'key' => $translation->getKey(),
140 1
            'text' => $translation->getTranslations(),
141 1
            'sync_at' => $translation->getSyncAt()
142 1
        ])->save();
143
144 1
        $translation->setId($updated->id);
145 1
        $translation->setCreatedAt($updated->created_at);
146 1
        $translation->setUpdatedAt($updated->updated_at);
147
148 1
        event(new TranslationUpdated($translation, count($updated->getChanges()) >= 1));
149
150 1
        return count($updated->getChanges()) >= 1;
151
    }
152
153
    /**
154
     * @param TranslationInterface $translation
155
     * @return bool
156
     */
157 3
    public function delete(TranslationInterface $translation): bool
158
    {
159 3
        $isDeleted = $this->query()
160 3
            ->where('id', $translation->getId())
161 3
            ->delete();
162
163 3
        $isDeleted = $isDeleted > 0;
164
165 3
        event(new TranslationDeleted($translation, $isDeleted));
166
167 3
        return $isDeleted;
168
    }
169
}
170