ComponentPlugin   A
last analyzed

Complexity

Total Complexity 37

Size/Duplication

Total Lines 173
Duplicated Lines 0 %

Importance

Changes 6
Bugs 3 Features 0
Metric Value
eloc 75
c 6
b 3
f 0
dl 0
loc 173
rs 9.44
wmc 37

7 Methods

Rating   Name   Duplication   Size   Complexity  
B detachRows() 0 19 7
B attachAnonsModule() 0 33 7
B attachRows() 0 18 9
A detachPlugins() 0 8 5
A attach() 0 5 1
A detach() 0 4 1
B attachSeo() 0 28 7
1
<?php
2
3
namespace Larrock\Core\Plugins;
4
5
use LarrockFeed;
0 ignored issues
show
Bug introduced by
The type LarrockFeed 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...
6
use LarrockAdminSeo;
0 ignored issues
show
Bug introduced by
The type LarrockAdminSeo 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 Larrock\Core\Models\Seo;
8
use Larrock\Core\Models\Link;
9
use Larrock\ComponentFeed\Models\Feed;
0 ignored issues
show
Bug introduced by
The type Larrock\ComponentFeed\Models\Feed 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...
10
use Larrock\Core\Helpers\MessageLarrock;
11
12
/**
13
 * Аттач/детач данных плагинов и связанных полей
14
 * Class ComponentPlugin.
15
 */
16
class ComponentPlugin
17
{
18
    /**
19
     * Сохранение связей полей и данных плагинов.
20
     * @param $event
21
     * @throws \Exception
22
     */
23
    public function attach($event)
24
    {
25
        $this->attachRows($event);
26
        $this->attachSeo($event);
27
        $this->attachAnonsModule($event);
28
    }
29
30
    /**
31
     * Сохранение данных сео-плагина.
32
     * @param $event
33
     * @throws \Exception
34
     * @return \Larrock\Core\Models\Seo|Seo|null
35
     */
36
    protected function attachSeo($event)
37
    {
38
        if ($event->model->getTable() !== 'seo' && (! empty($event->request->get('seo_title')) ||
39
        ! empty($event->request->get('seo_description')) || ! empty($event->request->get('seo_seo_keywords')))) {
40
            /** @var Seo $seo */
41
            if ($seo = LarrockAdminSeo::getModel()->whereSeoIdConnect($event->model->id)->whereSeoTypeConnect($event->component->name)->first()) {
42
                $message = 'SEO обновлено';
43
                $seo->seo_id_connect = $event->model->id;
44
                $seo->seo_type_connect = $event->component->name;
45
            } else {
46
                $message = 'SEO добавлено';
47
                $seo = LarrockAdminSeo::getModel();
48
                $seo->seo_id_connect = $event->request->get('id_connect');
49
                $seo->seo_type_connect = $event->request->get('type_connect');
50
            }
51
52
            $seo->seo_url_connect = $event->request->get('url_connect');
53
            $seo->seo_title = $event->request->get('seo_title');
54
            $seo->seo_description = $event->request->get('seo_description');
55
            $seo->seo_keywords = $event->request->get('seo_keywords');
56
            if ($seo->save()) {
57
                MessageLarrock::success($message);
58
59
                return $seo;
60
            }
61
        }
62
63
        return null;
64
    }
65
66
    /**
67
     * Создание анонса материала через модуль anonsCategory.
68
     * @param $event
69
     * @return bool|Feed|null
70
     * @throws \Exception
71
     */
72
    protected function attachAnonsModule($event)
73
    {
74
        if (! $event->request->has('_jsvalidation') && ($event->request->has('anons_merge') || ! empty($event->request->get('anons_description')))) {
75
            if (! config('larrock.feed.anonsCategory')) {
76
                MessageLarrock::danger('larrock.feed.anonsCategory не задан. Анонс создан не будет');
77
78
                return true;
79
            }
80
            /** @var Feed $anons */
81
            $anons = LarrockFeed::getModel();
82
            $anons->title = $event->request->get('title');
83
            $anons->url = 'anons_'.$event->model->id.''.random_int(1, 9999);
84
            $anons->category = LarrockFeed::getConfig()->settings['anons_category'];
85
            $anons->user_id = \Auth::id();
86
            $anons->active = 1;
87
            $anons->position = LarrockFeed::getModel()->whereCategory(LarrockFeed::getConfig()->settings['anons_category'])->max('position') + 1;
88
89
            if ($event->request->has('anons_merge')) {
90
                $original = LarrockFeed::getModel()->whereId($event->request->get('id_connect'))->first();
91
                $anons->short = '<a href="'.$original->full_url.'">'.$event->request->get('title').'</a>';
92
            } else {
93
                $anons->short = $event->request->get('anons_description');
94
            }
95
96
            if ($anons->save()) {
97
                MessageLarrock::success('Анонс добавлен');
98
99
                return $anons;
100
            }
101
            MessageLarrock::danger('Анонс не добавлен');
102
        }
103
104
        return null;
105
    }
106
107
    /**
108
     * Сохранение связей полей.
109
     * @param $event
110
     */
111
    protected function attachRows($event)
112
    {
113
        if (\is_array($event->component->rows)) {
114
            foreach ($event->component->rows as $row) {
115
                if (isset($row->modelParent, $row->modelChild)) {
116
                    $add_params = ['model_parent' => $row->modelParent, 'model_child' => $row->modelChild];
117
118
                    if ($event->request->has($row->name)) {
119
                        if (\is_array($event->request->get($row->name))) {
120
                            foreach ($event->request->get($row->name) as $value) {
121
                                $params[$value] = $add_params;
122
                            }
123
                            if (isset($params)) {
124
                                $event->model->getLink($row->modelChild)->sync($params);
125
                            }
126
                        } elseif (! \is_object($event->request->get($row->name))) {
127
                            $params[$event->request->get($row->name)] = $add_params;
128
                            $event->model->getLink($row->modelChild)->sync($params);
129
                        }
130
                    }
131
                }
132
            }
133
        }
134
    }
135
136
    /**
137
     * Удаление всех связей материала компонента.
138
     * @param $event
139
     */
140
    public function detach($event)
141
    {
142
        $this->detachRows($event);
143
        $this->detachPlugins($event);
144
    }
145
146
    /**
147
     * Удаление связей полей.
148
     * @param $event
149
     */
150
    protected function detachRows($event)
151
    {
152
        if (\is_array($event->component->rows)) {
153
            foreach ($event->component->rows as $row) {
154
                if (isset($row->modelParent, $row->modelChild)) {
155
                    //Получаем id прилинкованных данных
156
                    $getLink = $event->model->getLink($row->modelChild)->get();
157
158
                    //Удаляем связи
159
                    $event->model->linkQuery($row->modelChild)->delete();
160
161
                    //Удаляем поля в modelChild если у поля указана опция setDeleteIfNoLink и больше связей к этому материалу нет
162
                    if ($row->deleteIfNoLink) {
163
                        //Проверяем остались ли связи до modelChild от modelParent
164
                        foreach ($getLink as $link) {
165
                            $linkModel = new Link();
166
                            if (! $linkModel::whereIdChild($link->id)->whereModelChild($row->modelChild)->first()) {
167
                                $modelChild = new $row->modelChild();
168
                                $modelChild->whereId($link->id)->delete();
169
                            }
170
                        }
171
                    }
172
                }
173
            }
174
        }
175
    }
176
177
    /**
178
     * Удаление данных плагинов.
179
     * @param $event
180
     */
181
    protected function detachPlugins($event)
182
    {
183
        if (\is_array($event->component->plugins_backend)) {
184
            foreach ($event->component->plugins_backend as $key_plugin => $value_plugin) {
185
                //Detach SEO plugin
186
                if ($key_plugin === 'seo' && $seo = LarrockAdminSeo::getModel()->whereSeoIdConnect($event->model->id)
187
                        ->whereSeoTypeConnect($event->component->name)->first()) {
188
                    $seo->delete();
189
                }
190
            }
191
        }
192
    }
193
}
194