ModelTranslationRepository   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 39
c 0
b 0
f 0
dl 0
loc 122
rs 10
wmc 16

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getTranslation() 0 17 2
A findByEntityId() 0 9 5
A findByUrl() 0 9 5
A getEntitiesByTypeAndLang() 0 11 2
A createOrUpdate() 0 21 1
1
<?php
2
3
namespace Translation\Repositories;
4
5
use Illuminate\Database\Query\JoinClause;
6
use Illuminate\Foundation\Application;
0 ignored issues
show
Bug introduced by
The type Illuminate\Foundation\Application was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Illuminate\Support\NamespacedItemResolver;
8
use Translation\Models\ModelTranslation;
9
use Carbon\Carbon;
10
use Translation;
11
12
class ModelTranslationRepository
13
{
14
    /**
15
     * Peguei do Siravel
16
     */
17
18
    public $model;
19
20
    public function __construct(ModelTranslation $translation)
21
    {
22
        $this->model = $translation;
23
    }
24
25
    public function getTranslation($code, $type, $lang)
26
    {
27
        list($language, $country) = [
28
            Translation::getLanguageCode($lang),
29
            Translation::getCountryCode($lang)
30
        ];
31
        if ($trans = ModelTranslation::where('entity_id', $code)->where('entity_type', $type)->where('language_code', $language)->where('country_code', $country)
32
            // ->where('entity_data', 'LIKE', '%"lang":"'.$lang.'"%')
33
            ->first()
34
        ) {
35
            return $trans;
36
        }
37
38
        return ModelTranslation::where('entity_id', $code)
39
            ->where('entity_type', $type)
40
            ->where('language_code', $language)
41
            ->first();
42
    }
43
44
    /**
45
     * Create or Update an entry
46
     *
47
     * @param integer $entityId
48
     * @param string  $entityType
49
     * @param string  $lang
50
     * @param array   $payload
51
     *
52
     * @return boolean
53
     */
54
    public function createOrUpdate($entityId, $entityType, $lang, $payload)
55
    {
56
        list($language, $country) = [
57
            Translation::getLanguageCode($lang),
58
            Translation::getCountryCode($lang)
59
        ];
60
        $translation = $this->model->firstOrCreate(
61
            [
62
            'entity_id' => $entityId,
63
            'entity_type' => $entityType,
64
            'language_code' => $language,
65
            'country_code' => $country,
66
            ]
67
        );
68
69
        unset($payload['_method']);
70
        unset($payload['_token']);
71
72
        $translation->entity_data = json_encode($payload);
73
74
        return $translation->save();
75
    }
76
77
    /**
78
     * Find by URL
79
     *
80
     * @param string $url
81
     * @param string $type
82
     *
83
     * @return Object|null
84
     */
85
    public function findByUrl($url, $type)
86
    {
87
        $item = $this->model->where('entity_type', $type)->where('entity_data', 'LIKE', '%"url":"'.$url.'"%')->first();
88
89
        if ($item && ($item->data->is_published == 1 || $item->data->is_published == 'on') && $item->data->published_at <= Carbon::now(\Illuminate\Support\Facades\Config::get('app.timezone'))->format('Y-m-d H:i:s')) {
90
            return $item->data;
91
        }
92
93
        return null;
94
    }
95
96
    /**
97
     * Find an entity by its Id
98
     *
99
     * @param integer $entityId
100
     * @param string  $entityType
101
     *
102
     * @return Object|null
103
     */
104
    public function findByEntityId($entityId, $entityType)
105
    {
106
        $item = $this->model->where('entity_type', $entityType)->where('entity_id', $entityId)->first();
107
108
        if ($item && ($item->data->is_published == 1 || $item->data->is_published == 'on') && $item->data->published_at <= Carbon::now(\Illuminate\Support\Facades\Config::get('app.timezone'))->format('Y-m-d H:i:s')) {
109
            return $item->data;
110
        }
111
112
        return null;
113
    }
114
115
    /**
116
     * Get entities by type and language
117
     *
118
     * @param string $lang
119
     * @param string $type
120
     *
121
     * @return Illuminate\Support\Collection
0 ignored issues
show
Bug introduced by
The type Translation\Repositories...nate\Support\Collection was not found. Did you mean Illuminate\Support\Collection? If so, make sure to prefix the type with \.
Loading history...
122
     */
123
    public function getEntitiesByTypeAndLang($lang, $type)
124
    {
125
        $entities = collect();
126
        $collection = $this->model->where('entity_type', $type)->where('entity_data', 'LIKE', '%"lang":"'.$lang.'"%')->get();
127
128
        foreach ($collection as $item) {
129
            $instance = app($item->type)->attributes = $item->data;
0 ignored issues
show
Bug introduced by
The function app was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

129
            $instance = /** @scrutinizer ignore-call */ app($item->type)->attributes = $item->data;
Loading history...
Bug introduced by
The property type does not seem to exist on Translation\Models\ModelTranslation. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
130
            $entities->push($instance);
131
        }
132
133
        return $entities;
134
    }
135
}
136