Completed
Push — master ( 24903b...567875 )
by Stephen
15s queued 11s
created

BlogController::preview()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use Wink\WinkPost;
6
7
class BlogController extends Controller
8
{
9
    /**
10
     * Show blog homepage.
11
     *
12
     * @return View
13
     */
14
    public function index()
15
    {
16
        $data = [
17
            'posts' => WinkPost::with('tags')
18
                        ->live()
19
                        ->orderBy('publish_date', 'DESC')
20
                        ->simplePaginate(12)
21
        ];
22
23
        return view('index', compact('data'));
24
    }
25
26
    /**
27
     * Show about blog.
28
     *
29
     * @return View
30
     */
31
    public function about()
32
    {
33
        return view('about');
34
    }
35
36
    /**
37
     * Show a post given a slug.
38
     *
39
     * @param string $slug
40
     * @return View
41
     */
42
    public function findPostBySlug(string $slug)
43
    {
44
        $posts = WinkPost::with('tags')->live()->get();
45
46
        $post = $posts->firstWhere('slug', $slug);
47
48
        if (optional($post)->published) {
49
            $next = $posts->sortByDesc('publish_date')
50
                ->firstWhere('publish_date', '>', optional($post)->publish_date);
51
            $prev = $posts->sortByDesc('publish_date')
52
                ->firstWhere('publish_date', '<', optional($post)->publish_date);
53
54
            $data = [
55
                'author' => $post->author,
56
                'post' => $post,
57
                'meta' => $post->meta,
58
                'next' => $next,
59
                'prev' => $prev,
60
            ];
61
62
            return view('post', compact('data'));
63
        }
64
65
        abort(404);
66
    }
67
68
    public function preview($postSlug)
69
    {
70
        $post = WinkPost::with('tags')->where('slug', $postSlug)->first();
71
72
        $data = [
73
            'author' => $post->author,
74
            'post' => $post,
75
            'meta' => $post->meta,
76
            'next' => null,
77
            'prev' => null,
78
        ];
79
80
        return view('post', compact('data'));
81
    }
82
83
    /**
84
     * Update indexed articles.
85
     *
86
     * @return string
87
     */
88
    public function updateIndexedArticles()
89
    {
90
        $data = collect(WinkPost::live()
91
            ->orderBy('publish_date', 'DESC')
92
            ->get())->map(function ($item, $key) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
93
                return [
94
                    'title' => $item->title,
95
                    'link' => post_url($item->slug),
96
                    'snippet' => $item->excerpt
97
                ];
98
            });
99
100
        $file_path = public_path('index.json');
101
102
        file_put_contents($file_path, $data->toJson());
103
104
        return 'Indexed articles updated for live search';
105
    }
106
}
107