<file_sep><?php

namespace App\Services;

use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;

class Reddit
{
    private $accessToken;

    public function __construct()
    {
        $this->accessToken = $this->getAccessToken();
    }

    public function posts($subreddit = 'battlestations')
    {
        if (Cache::has("posts.{$subreddit}")) {
            $posts = Cache::get("posts.{$subreddit}");

            return $posts;
        }

        $query = Http::withToken($this->accessToken)
            ->baseUrl('https://oauth.reddit.com')
            ->acceptJson()
            ->get("/r/{$subreddit}/hot");
        $body = $query->object();
        $posts = collect($body->data->children)->pluck('data');
        Cache::put("posts.{$subreddit}", $posts, now()->addHour());

        return $posts;
    }

    public function getAccessToken()
    {
        $response = Http::withBasicAuth(config('reddit-api.app_id'), config('reddit-api.app_secret'))
            ->withHeaders([
                'User-Agent' => 'Stations App by /u/fgoni',
            ])
            ->asForm()
            ->acceptJson()
            ->post('https://www.reddit.com/api/v1/access_token', [
                'grant_type' => 'client_credentials',
            ]);

        return $response->object()->access_token;
    }
}
<file_sep><?php

//use CodeWizz\RedditAPI\RedditAPI;
use App\Services\Reddit;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function (Reddit $reddit) {
    $posts = $reddit->posts()->filter(function($post){
        return str_contains($post->url, 'i.redd.it');
    });
    return view('battlestations', [
        'posts' => $posts,
    ]);
});

Route::get('{subreddit}', function (Reddit $reddit, $subreddit) {
    $posts = $reddit->posts($subreddit)->filter(function($post){
        return str_contains($post->url, 'i.redd.it');
    });
    return view('battlestations', [
        'posts' => $posts,
    ]);
});
