Test Failed
Push — main ( 97a8b8...676696 )
by Davide
11:43 queued 11s
created

GlossaryService::getBySlug()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
1
<?php
2
3
namespace App\Services;
4
5
use App\Helpers\ImageHelpers;
6
use App\Http\Requests\GlossaryStoreRequest;
7
use App\Models\Glossary;
8
use App\Repositories\GlossaryRepositoryInterface;
9
10
class GlossaryService
11
{
12
    private GlossaryRepositoryInterface $glossaryRepository;
13
    private GlossaryVariantService $glossaryVariantService;
14
15
    /**
16
     * GlossaryService constructor.
17
     *
18
     * @param  GlossaryRepositoryInterface  $glossaryRepository
19
     * @param  GlossaryVariantService  $glossaryVariantService
20 17
     */
21
    public function __construct(
22
        GlossaryRepositoryInterface $glossaryRepository,
23 17
        GlossaryVariantService $glossaryVariantService
24 17
    ) {
25
        $this->glossaryRepository = $glossaryRepository;
26
        $this->glossaryVariantService = $glossaryVariantService;
27
    }
28
29
    /**
30
     * Create a glossary term
31
     *
32
     * @param \App\Http\Requests\GlossaryStoreRequest $request
33 1
     *
34
     * @return \App\Models\Glossary
35 1
     */
36 1
    public function createGlossary(GlossaryStoreRequest $request): Glossary
37
    {
38 1
        $glossary = $this->glossaryRepository->store($request->all());
39
40
        $this->glossaryVariantService->createGlossaryVariant($glossary);
41
42
        ImageHelpers::storeImages($glossary, $request, 'introimage');
43
44
        return $glossary;
45
    }
46
47
    /**
48
     * Update the glossary
49 1
     *
50
     * @param \App\Http\Requests\GlossaryStoreRequest $request
51 1
     * @param int $glossaryId
52
     *
53 1
     * @return \App\Models\Glossary
54 1
     */
55
    public function updateGlossary(GlossaryStoreRequest $request, int $glossaryId): Glossary
56 1
    {
57
        $glossary = $this->glossaryRepository->update($request->all(), $glossaryId);
58
59
        ImageHelpers::storeImages($glossary, $request, 'introimage');
60
        ImageHelpers::deleteImages($glossary, $request, 'introimage');
61
62
        return $glossary;
63
    }
64
65
    /**
66 1
     * Return the glossary from the database
67
     *
68 1
     * @param int $glossaryId
69
     *
70
     * @return \App\Models\Glossary
71
     */
72
    public function getById(int $glossaryId): Glossary
73
    {
74
        return $this->glossaryRepository->getById($glossaryId);
75
    }
76
77
    public function getBySlug(string $glossarySlug): Glossary
78
    {
79 1
        return $this->glossaryRepository->getBySlug($glossarySlug);
80
    }
81 1
82
    /**
83
     * Get all the glossary terms
84
     *
85
     * @param int|null $recordsPerPage
86
     * @param array|null $searchParameters
87
     *
88
     * @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Contracts\Pagination\LengthAwarePaginator
89 1
     */
90
    public function getGlossaries(int $recordsPerPage = null, array $searchParameters = null)
91 1
    {
92 1
        return $this->glossaryRepository->getAll($recordsPerPage, $searchParameters);
93
    }
94
95
    /**
96
     * Delete the glossary terms from the database
97
     *
98
     * @param int $glossaryId
99
     */
100
    public function deleteGlossary(int $glossaryId): void
101
    {
102
        $this->glossaryRepository->delete($glossaryId);
103
    }
104
105 2
    /**
106
     * Check in the text if there is any published Glossary Term variant,
107 2
     * and make them hoverable to show the tooltip.
108 2
     *
109
     * It adds also the hidden contents to show in the tooltips
110 2
     * when the terms are hovered.
111 1
     *
112
     * @param string $text
113 1
     *
114 1
     * @return string
115
     */
116
    public function markGlossaryTerms(string $text): string
117 1
    {
118
        $glossaryTermsWithVariants = $this->glossaryRepository->getAllWithVariants();
0 ignored issues
show
Bug introduced by
The method getAllWithVariants() does not exist on App\Repositories\GlossaryRepositoryInterface. Did you maybe mean getAll()? ( Ignorable by Annotation )

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

118
        /** @scrutinizer ignore-call */ 
119
        $glossaryTermsWithVariants = $this->glossaryRepository->getAllWithVariants();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
119
120 2
        $count = 1;
121
        $glossaryTermPresent = false;
122
        foreach ($glossaryTermsWithVariants as $glossaryTermId => $glossaryTerm) {
123
            foreach ($glossaryTerm->variants as $variant) {
124
                $currentLanguageVariantTerm = $variant->getTranslation('term', app()->getLocale());
0 ignored issues
show
introduced by
The method getLocale() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

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

124
                $currentLanguageVariantTerm = $variant->getTranslation('term', app()->/** @scrutinizer ignore-call */ getLocale());
Loading history...
125
126
                $text = $this->replaceGlossaryVariant($currentLanguageVariantTerm, $glossaryTerm->slug, $text, $count);
127
128
                if (self::variantIsPresent($text, $currentLanguageVariantTerm)) {
0 ignored issues
show
Bug Best Practice introduced by
The method App\Services\GlossaryService::variantIsPresent() is not static, but was called statically. ( Ignorable by Annotation )

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

128
                if (self::/** @scrutinizer ignore-call */ variantIsPresent($text, $currentLanguageVariantTerm)) {
Loading history...
129
                    $glossaryTermPresent = true;
130
                }
131 3
            }
132
133 3
            if ($glossaryTermPresent) {
134 2
                $text = $this->attachTermDescription($glossaryTerm, $text);
135
            }
136 2
137
            $count++;
138
            $glossaryTermPresent = false;
139
        }
140
141
        return $text;
142
    }
143
144
    /**
145
     * Check if the term is present in the text
146
     *
147
     * @param string $text
148 2
     * @param string $term
149
     *
150
     * @return bool
151 2
     */
152
    public function variantIsPresent(string $text, string $term): bool
153 2
    {
154 2
        if (strpos($text, $term) !== false) {
155 2
            return true;
156 2
        }
157 2
        return false;
158
    }
159 2
160
    /**
161
     * Replace glossary term
162
     *
163 2
     * @param string $currentLanguageVariantTerm
164
     * @param int $glossaryTermId
165
     * @param string $text
166
     * @param int $count
167
     *
168
     * @return string
169
     */
170
    public function replaceGlossaryVariant(string $currentLanguageVariantTerm, string $glossarySlug, string $text, int &$count): string
171
    {
172
        //$pattern = "/\b$glossaryTerm->term\b/";
173
        $pattern = "~<a .*?</a>(*SKIP)(*F)|\b$currentLanguageVariantTerm\b~";
174 2
175
        $text = preg_replace_callback(
176 2
            $pattern,
177 2
            function ($matches) use ($currentLanguageVariantTerm, $glossarySlug, $count) {
0 ignored issues
show
Unused Code introduced by
The parameter $matches is not used and could be removed. ( Ignorable by Annotation )

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

177
            function (/** @scrutinizer ignore-unused */ $matches) use ($currentLanguageVariantTerm, $glossarySlug, $count) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
178 2
                $glossaryTermTemplate = "<a href='/glossaryTerms/".$glossarySlug."' class='text-red-700 has-glossary-term glossary-term-".$count."' data-termFoundId='".$count."' data-definitionId='".$glossarySlug."'>".$currentLanguageVariantTerm."</a>";
179
                return $glossaryTermTemplate;
180
                $count++;
0 ignored issues
show
Unused Code introduced by
PostIncNode is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
181 2
            },
182 2
            $text
183 2
        );
184 2
185 2
        return $text;
186 2
    }
187 2
188 2
    /**
189 2
     * Attach the term tooltip content to the end of the text
190 2
     *
191 2
     * @param \App\Models\Glossary $glossaryTerm
192 2
     * @param string $text
193 2
     *
194 2
     * @return string
195
     */
196 2
    public function attachTermDescription(Glossary $glossaryTerm, string $text): string
197 2
    {
198
        $termTooltipContent = "<div class='tooltip-painter' id='glossary-definition-" . $glossaryTerm->slug . "' style='display:none'>";
199
        $termTooltipContent .= "<div class='photo'>";
200
        if($glossaryTerm->hasMedia('introimage')){
201
            $termTooltipContent .= "<img src='".$glossaryTerm->getMedia('introimage')[0]->getUrl('thumb')."' alt=''/>";
202
        }
203
        $termTooltipContent .= "</div>";
204
        $termTooltipContent .= "<div class='content p-2 overflow-auto'>";
205
        $termTooltipContent .= "<div class='padder'>";
206
        $termTooltipContent .= "<div class='title font-bold'>" . ucfirst($glossaryTerm->term) . "</div>";
207
        $termTooltipContent .= "<div class='description w-72 py-2'>";
208
        $termTooltipContent .= $glossaryTerm->definition;
209
        $termTooltipContent .= "<br>";
210
        $termTooltipContent .= "<div class='mt-2'>";
211
        $termTooltipContent .= "<a href='" . route('glossary.show',$glossaryTerm->slug) . "'>Read more</a>";  // route('glossary.show',$glossaryTerm->id)
212
        $termTooltipContent .= "</div>";
213
        $termTooltipContent .= "</div>";
214
        $termTooltipContent .=  "</div>";
215
        $termTooltipContent .=  "</div>";
216
        $termTooltipContent .= "</div>";
217
218
        $ret = $text . $termTooltipContent;
219
        return $ret;
220
    }
221
222
    /**
223
     * Return the title question to show in the Glossary show view
224
     *
225
     * @param \App\Models\Glossary $glossaryTerm
226
     *
227
     * @return string
228
     */
229
    public function getGlossaryTermTitleQuestion(Glossary $glossaryTerm): string
230
    {
231
        $ret = "";
232
233
        switch ($glossaryTerm->question_type) {
234
            case 1:
235
                $ret .= __('glossary.what_is');
236
                break;
237
            case 2:
238
                $ret .= __('glossary.what_does_it_mean');
239
                break;
240
        }
241
        $ret .= " {$glossaryTerm->term} ?" ;
242
243
        return $ret;
244
    }
245
246
}
247