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

DisqusMiddleware   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 76
ccs 14
cts 14
cp 1
rs 10
c 1
b 0
f 0
wmc 6
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handle() 0 7 2
A handleDisqus() 0 5 1
A getPageId() 0 6 2
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