DisqusMiddleware::appendDisqusScript()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 39
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 18
c 2
b 0
f 0
nc 3
nop 2
dl 0
loc 39
ccs 0
cts 30
cp 0
crap 12
rs 9.6666
1
<?php
2
3
namespace Yajra\Disqus;
4
5
use Closure;
6
use Illuminate\Support\Str;
7
8
class DisqusMiddleware
9
{
10
    /**
11
     * Handle an incoming request.
12
     *
13
     * @param  \Illuminate\Http\Request $request
0 ignored issues
show
Bug introduced by
The type Illuminate\Http\Request 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...
14
     * @param  \Closure $next
15
     * @return mixed
16
     */
17
    public function handle($request, Closure $next)
18
    {
19
        $response = $next($request);
20
21
        if (config('disqus.enabled', false) && ! empty(config('disqus.username'))) {
0 ignored issues
show
Bug introduced by
The function config 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

21
        if (/** @scrutinizer ignore-call */ config('disqus.enabled', false) && ! empty(config('disqus.username'))) {
Loading history...
22
            $this->appendDisqusScript($request, $response);
23
        }
24
25
        return $response;
26
    }
27
28
    /**
29
     * Append disqus script on the end of the page.
30
     *
31
     * @param  \Illuminate\Http\Request $request
32
     * @param  \Illuminate\Http\Response $response
0 ignored issues
show
Bug introduced by
The type Illuminate\Http\Response 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...
33
     * @return mixed
34
     */
35
    protected function appendDisqusScript($request, $response)
36
    {
37
        $content = $response->getContent();
38
39
        if (! Str::contains($content, '<div id="disqus_thread"></div>')) {
40
            return;
41
        }
42
43
        $uri = $request->getRequestUri();
44
        $pageUrl = url($uri);
0 ignored issues
show
Bug introduced by
The function url 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

44
        $pageUrl = /** @scrutinizer ignore-call */ url($uri);
Loading history...
45
        $pageId = 'route'.implode('.', explode('/', $uri));
46
        $username = config('disqus.username');
0 ignored issues
show
Bug introduced by
The function config 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

46
        $username = /** @scrutinizer ignore-call */ config('disqus.username');
Loading history...
47
48
        $disqusHtml = <<<CDATA
49
<script>
50
     var disqus_config = function () {
51
         this.page.url = '$pageUrl';
52
         this.page.identifier = '$pageId';
53
     };
54
55
    (function() {
56
        var d = document, s = d.createElement('script');
57
58
        s.src = '//$username.disqus.com/embed.js';
59
60
        s.setAttribute('data-timestamp', +new Date());
61
        (d.head || d.body).appendChild(s);
62
    })();
63
</script>
64
<noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript" rel="nofollow">comments powered by Disqus.</a></noscript>
65
CDATA;
66
67
        $bodyPosition = strripos($content, '</body>');
68
69
        if (false !== $bodyPosition) {
70
            $content = substr($content, 0, $bodyPosition).$disqusHtml.substr($content, $bodyPosition);
71
        }
72
73
        $response->setContent($content);
74
    }
75
}
76