AbstractComposer::cachedCategories()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 3
cp 0
cc 1
eloc 3
nc 1
nop 0
crap 2
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
    /**
23
     * Caching time.
24
     *
25
     * @var int
26
     */
27
    protected $cacheMinutes = 5;
28
29
    /* -----------------------------------------------------------------
30
     |  Main Methods
31
     | -----------------------------------------------------------------
32
     */
33
34
    /**
35
     * Get the cached posts.
36
     *
37
     * @return \Illuminate\Database\Eloquent\Collection
38
     */
39
    protected function cachedPosts()
40
    {
41
        return $this->cacheResults('posts.all', function () {
42
            return Post::all();
43
        });
44
    }
45
46
    /**
47
     * Get the cached categories.
48
     *
49
     * @return \Illuminate\Database\Eloquent\Collection
50
     */
51
    protected function cachedCategories()
52
    {
53
        return $this->cacheResults('categories.all', function () {
54
            return Category::all();
55
        });
56
    }
57
58
    /**
59
     * Get the cached tags.
60
     *
61
     * @return \Illuminate\Database\Eloquent\Collection
62
     */
63
    protected function cachedTags()
64
    {
65
        return $this->cacheResults('tags.all', function () {
66
            return Tag::all();
67
        });
68
    }
69
70
    /* -----------------------------------------------------------------
71
     |  Other Methods
72
     | -----------------------------------------------------------------
73
     */
74
75
    /**
76
     * Cache the results.
77
     *
78
     * @param  string    $name
79
     * @param  \Closure  $callback
80
     *
81
     * @return mixed
82
     */
83
    protected function cacheResults($name, Closure $callback)
84
    {
85
        return Cache::remember("blog::{$name}", $this->cacheMinutes, $callback);
86
    }
87
}
88