RenderPlugins   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Importance

Changes 4
Bugs 4 Features 0
Metric Value
eloc 45
c 4
b 4
f 0
dl 0
loc 94
rs 10
wmc 14

4 Methods

Rating   Name   Duplication   Size   Complexity  
A renderImageGallery() 0 23 5
A renderBlocks() 0 15 3
A __construct() 0 4 1
A renderFilesGallery() 0 23 5
1
<?php
2
3
namespace Larrock\Core\Helpers\Plugins;
4
5
use LarrockBlocks;
0 ignored issues
show
Bug introduced by
The type LarrockBlocks 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 Illuminate\Database\Eloquent\Model;
7
8
/**
9
 * Плагин замены шорткатов плагинов на их данные внутри полей компонентов
10
 * Class RenderPlugins.
11
 */
12
class RenderPlugins
13
{
14
    /** @var string html-код поля модели для замены шорткатов плагинов на их данные */
15
    public $rendered_html;
16
17
    /** @var null|Model Модель контента */
18
    protected $model;
19
20
    public function __construct($html, $model = null)
21
    {
22
        $this->rendered_html = $html;
23
        $this->model = $model;
24
    }
25
26
    /**
27
     * Вставка блоков.
28
     * @return $this
29
     * @throws \Throwable
30
     */
31
    public function renderBlocks()
32
    {
33
        $re = "/{Блок\\[(?P<type>[a-zA-Z0-9-_а-яА-Я\s]+)]=(?P<name>[a-zA-Z0-9-_а-яА-Я\s]+)}/u";
34
        preg_match_all($re, $this->rendered_html, $matches);
35
        foreach ($matches['type'] as $key => $match) {
36
            $name = $matches['name'][$key];
37
            if ($matched_block = LarrockBlocks::getModel()->whereUrl($matches['name'][$key])->first()) {
38
                $this->rendered_html = preg_replace('/<p>{Блок\\['.$match.']='.$name.'}<\/p>/',
39
                    view('larrock::front.plugins.renderBlock.'.$match, ['data' => $matched_block])->render(), $this->rendered_html);
0 ignored issues
show
Unused Code introduced by
The call to view() has too many arguments starting with array('data' => $matched_block). ( Ignorable by Annotation )

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

39
                    /** @scrutinizer ignore-call */ 
40
                    view('larrock::front.plugins.renderBlock.'.$match, ['data' => $matched_block])->render(), $this->rendered_html);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
40
                $this->rendered_html = preg_replace('/{Блок\\['.$match.']='.$name.'}/',
41
                    view('larrock::front.plugins.renderBlock.'.$match, ['data' => $matched_block])->render(), $this->rendered_html);
42
            }
43
        }
44
45
        return $this;
46
    }
47
48
    /**
49
     * Вставка галерей изображений.
50
     * @return $this
51
     * @throws \Throwable
52
     */
53
    public function renderImageGallery()
54
    {
55
        $re = "/{Фото\\[(?P<type>[a-zA-Z0-9-_а-яА-Я\s]+)]=(?P<name>[a-zA-Z0-9-_а-яА-Я\s]+)}/u";
56
        preg_match_all($re, $this->rendered_html, $matches);
57
        if (isset($matches['type'][0])) {
58
            $images = $this->model->getImages;
59
            foreach ($matches['type'] as $key => $match) {
60
                $name = $matches['name'][$key];
61
                //Собираем изображения под каждую найденную галерею
62
                $matched_images['images'] = [];
63
                foreach ($images as $image) {
64
                    if ($image->getCustomProperty('gallery') === $matches['name'][$key]) {
65
                        $matched_images['images'][] = $image;
66
                    }
67
                }
68
                $this->rendered_html = preg_replace('/<p>{Фото\\['.$match.']='.$name.'}<\/p>/',
69
                    view('larrock::front.plugins.photoGallery.'.$match, $matched_images)->render(), $this->rendered_html);
0 ignored issues
show
Unused Code introduced by
The call to view() has too many arguments starting with $matched_images. ( Ignorable by Annotation )

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

69
                    /** @scrutinizer ignore-call */ 
70
                    view('larrock::front.plugins.photoGallery.'.$match, $matched_images)->render(), $this->rendered_html);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
70
                $this->rendered_html = preg_replace('/{Фото\\['.$match.']='.$name.'}/',
71
                    view('larrock::front.plugins.photoGallery.'.$match, $matched_images)->render(), $this->rendered_html);
72
            }
73
        }
74
75
        return $this;
76
    }
77
78
    /**
79
     * Вставка галерей файлов.
80
     * @return $this
81
     * @throws \Throwable
82
     */
83
    public function renderFilesGallery()
84
    {
85
        $re = "/{Файлы\\[(?P<type>[a-zA-Z0-9-_а-яА-Я\s]+)]=(?P<name>[a-zA-Z0-9-_а-яА-Я\s]+)}/u";
86
        preg_match_all($re, $this->rendered_html, $matches);
87
        if (isset($matches['type'][0])) {
88
            $files = $this->model->getFiles;
89
            foreach ($matches['type'] as $key => $match) {
90
                $name = $matches['name'][$key];
91
                //Собираем изображения под каждую найденную галерею
92
                $matched_files['files'] = [];
93
                foreach ($files as $file) {
94
                    if ($file->getCustomProperty('gallery') === $matches['name'][$key]) {
95
                        $matched_files['files'][] = $file;
96
                    }
97
                }
98
                $this->rendered_html = preg_replace('/<p>{Файлы\\['.$match.']='.$name.'}<\/p>/',
99
                    view('larrock::front.plugins.fileGallery.'.$match, $matched_files)->render(), $this->rendered_html);
0 ignored issues
show
Unused Code introduced by
The call to view() has too many arguments starting with $matched_files. ( Ignorable by Annotation )

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

99
                    /** @scrutinizer ignore-call */ 
100
                    view('larrock::front.plugins.fileGallery.'.$match, $matched_files)->render(), $this->rendered_html);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
100
                $this->rendered_html = preg_replace('/{Файлы\\['.$match.']='.$name.'}/',
101
                    view('larrock::front.plugins.fileGallery.'.$match, $matched_files)->render(), $this->rendered_html);
102
            }
103
        }
104
105
        return $this;
106
    }
107
}
108