Completed
Push — master ( 7e54ec...8b45a6 )
by Vojta
9s
created

Reviews::getCategory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php namespace VojtaSvoboda\Reviews\Components;
2
3
use App;
4
use Cms\Classes\ComponentBase;
5
use VojtaSvoboda\Reviews\Facades\ReviewsFacade;
6
use VojtaSvoboda\Reviews\Models\Category;
7
8
class Reviews extends ComponentBase
9
{
10
    private $reviews;
11
12
    public function componentDetails()
13
    {
14
        return [
15
            'name' => 'Reviews',
16
            'description' => 'Show reviews on your page.'
17
        ];
18
    }
19
20
    public function defineProperties()
21
    {
22
        return [
23
            'categoryFilter' => [
24
                'title' => 'Category identifier',
25
                'description' => 'Show only reviews from some category by slug',
26
                'type' => 'string',
27
                'default' => '{{ :category }}',
28
            ],
29
        ];
30
    }
31
32
    public function onRun()
33
    {
34
        // category filter
35
        $category = null;
36
        if ($categorySlug = $this->property('categoryFilter')) {
37
            $category = $this->getCategory($categorySlug);
38
        }
39
        $this->page['category'] = $category;
40
        $this->page['reviews'] = $this->reviews($category);
41
    }
42
43
    /**
44
     * Get reviews.
45
     *
46
     * @param Category $category Filter by category.
47
     *
48
     * @return mixed
49
     */
50
    public function reviews($category = null)
51
    {
52
        if ($this->reviews === null) {
53
            $this->reviews = $this->getFacade()->getApprovedReviews($category);
54
        }
55
56
        return $this->reviews;
57
    }
58
59
    /**
60
     * Get category by slug.
61
     *
62
     * @param $category
63
     *
64
     * @return mixed
65
     */
66
    public function getCategory($category)
67
    {
68
        return Category::where('slug', $category)->first();
69
    }
70
71
    /**
72
     * Get Facade.
73
     *
74
     * @return ReviewsFacade
75
     */
76
    private function getFacade()
77
    {
78
        return App::make('vojtasvoboda.reviews.facade');
79
    }
80
}
81