Completed
Push — master ( a14a07...30e145 )
by Stephen
01:17
created

BlogController::findPostBySlug()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 24
rs 9.536
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
11
    /**
12
     * Show blog homepage.
13
     *
14
     * @return View
15
     */
16
    public function index()
17
    {
18
        $data = [
19
            'posts'  => WinkPost::with('tags')
20
                ->live()
21
                ->orderBy('publish_date', 'DESC')
22
                ->simplePaginate(12)
23
        ];
24
25
        return view('index', compact('data'));
26
    }
27
28
29
    /**
30
     * Show about blog.
31
     *
32
     * @return View
33
     */
34
    public function about()
35
    {
36
        return view('about');
37
    }
38
39
40
    /**
41
     * Show a post given a slug.
42
     *
43
     * @param string $slug
44
     * @return View
45
     */
46
    public function findPostBySlug(string $slug)
47
    {
48
        $posts = WinkPost::with('tags')
49
            ->live()->get();
50
51
        $post = $posts->firstWhere('slug', $slug);
52
53
        if (optional($post)->published) {
54
            $next = $posts->sortBy('published_at')->firstWhere('published_at', '>', optional($post)->published_at);
55
            $prev = $posts->sortBy('published_at')->firstWhere('published_at', '<', optional($post)->published_at);
56
57
            $data = [
58
                'author' => $post->author,
59
                'post'   => $post,
60
                'meta'   => $post->meta,
61
                'next'   => $next,
62
                'prev'   => $prev,
63
            ];
64
65
            return view('post', compact('data'));
66
        }
67
68
        abort(404);
69
    }
70
71
72
73
    /**
74
     * Update indexed articles.
75
     *
76
     * @return string
77
     */
78
    public function updateIndexedArticles()
79
    {
80
        $data = collect(WinkPost::live()
81
            ->orderBy('publish_date', 'DESC')
82
            ->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...
83
            return [
84
                "title" => $item->title,
85
                "link" => post_url($item->slug),
86
                "snippet" => $item->excerpt
87
            ];
88
        });
89
90
        $file_path = public_path('index.json');
91
92
        file_put_contents($file_path, $data->toJson());
93
94
        return "Indexed articles updated for live search";
95
    }
96
}
97