Completed
Push — master ( a830fd...0551e6 )
by ARCANEDEV
08:49
created

DisqusMiddleware::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php namespace Arcanedev\LaravelDisqus\Http\Middleware;
2
3
use Arcanedev\LaravelDisqus\Contracts\Disqus;
4
use Closure;
5
6
/**
7
 * Class     DisqusMiddleware
8
 *
9
 * @package  Arcanedev\LaravelDisqus\Http\Middleware
10
 * @author   ARCANEDEV <[email protected]>
11
 */
12
class DisqusMiddleware
13
{
14
    /* ------------------------------------------------------------------------------------------------
15
     |  Properties
16
     | ------------------------------------------------------------------------------------------------
17
     */
18
    /**
19
     * The Disqus instance.
20
     *
21
     * @var  \Arcanedev\LaravelDisqus\Contracts\Disqus
22
     */
23
    protected $disqus;
24
25
    /* ------------------------------------------------------------------------------------------------
26
     |  Constructor
27
     | ------------------------------------------------------------------------------------------------
28
     */
29
    /**
30
     * DisqusMiddleware constructor.
31
     *
32
     * @param  \Arcanedev\LaravelDisqus\Contracts\Disqus  $disqus
33
     */
34 12
    public function __construct(Disqus $disqus)
35
    {
36 12
        $this->disqus = $disqus;
37 12
    }
38
39
    /* ------------------------------------------------------------------------------------------------
40
     |  Main Functions
41
     | ------------------------------------------------------------------------------------------------
42
     */
43
    /**
44
     * Handle an incoming request.
45
     *
46
     * @param  \Illuminate\Http\Request  $request
47
     * @param  \Closure                  $next
48
     *
49
     * @return mixed
50
     */
51 12
    public function handle($request, Closure $next)
52
    {
53 12
        if ($this->disqus->isEnabled())
54 12
            $this->handleDisqus($request);
55
56 12
        return $next($request);
57
    }
58
59
    /* ------------------------------------------------------------------------------------------------
60
     |  Other Functions
61
     | ------------------------------------------------------------------------------------------------
62
     */
63
    /**
64
     * Handle Disqus.
65
     *
66
     * @param  \Illuminate\Http\Request  $request
67
     */
68 12
    protected function handleDisqus($request)
69
    {
70 12
        $this->disqus->setPageUrl($request->url())
71 12
                     ->setPageId($this->getPageId($request));
72 12
    }
73
74
    /**
75
     * Get the page id.
76
     *
77
     * @param  \Illuminate\Http\Request  $request
78
     *
79
     * @return string
80
     */
81 12
    protected function getPageId($request)
82
    {
83 12
        $path = implode('.', explode('/', $request->getPathInfo()));
84
85 12
        return 'base'.($path === '.' ? $path.'home' : $path);
86
    }
87
}
88