AbstractComposer   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

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

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
    /**
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