Completed
Push — master ( 20b252...4d5a46 )
by ARCANEDEV
04:35
created

AbstractComposer   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 70
rs 10
c 0
b 0
f 0
ccs 0
cts 11
cp 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A cachedPosts() 0 6 1
A cachedCategories() 0 6 1
A cachedTags() 0 6 1
A cacheResults() 0 4 1
1
<?php namespace Arcanesoft\Blog\ViewComposers;
2
3
use Arcanesoft\Blog\Models\Category;
4
use Arcanesoft\Blog\Models\Post;
5
use Arcanesoft\Blog\Models\Tag;
6
use Closure;
7
use Illuminate\Support\Facades\Cache;
8
9
/**
10
 * Class     AbstractComposer
11
 *
12
 * @package  Arcanesoft\Blog\ViewComposers
13
 * @author   ARCANEDEV <[email protected]>
14
 */
15
abstract class AbstractComposer
16
{
17
    /* ------------------------------------------------------------------------------------------------
18
     |  Properties
19
     | ------------------------------------------------------------------------------------------------
20
     */
21
    /**
22
     * Caching time.
23
     *
24
     * @var int
25
     */
26
    protected $cacheMinutes = 5;
27
28
    /* ------------------------------------------------------------------------------------------------
29
     |  Main Functions
30
     | ------------------------------------------------------------------------------------------------
31
     */
32
    /**
33
     * Get the cached posts.
34
     *
35
     * @return \Illuminate\Database\Eloquent\Collection
36
     */
37
    protected function cachedPosts()
38
    {
39
        return $this->cacheResults('posts.all', function () {
40
            return Post::all();
41
        });
42
    }
43
44
    /**
45
     * Get the cached categories.
46
     *
47
     * @return \Illuminate\Database\Eloquent\Collection
48
     */
49
    protected function cachedCategories()
50
    {
51
        return $this->cacheResults('categories.all', function () {
52
            return Category::all();
53
        });
54
    }
55
56
    /**
57
     * Get the cached tags.
58
     *
59
     * @return \Illuminate\Database\Eloquent\Collection
60
     */
61
    protected function cachedTags()
62
    {
63
        return $this->cacheResults('tags.all', function () {
64
            return Tag::all();
65
        });
66
    }
67
68
    /* ------------------------------------------------------------------------------------------------
69
     |  Other Functions
70
     | ------------------------------------------------------------------------------------------------
71
     */
72
    /**
73
     * Cache the results.
74
     *
75
     * @param  string    $name
76
     * @param  \Closure  $callback
77
     *
78
     * @return mixed
79
     */
80
    protected function cacheResults($name, Closure $callback)
81
    {
82
        return Cache::remember("blog::{$name}", $this->cacheMinutes, $callback);
83
    }
84
}
85