Archived
feat: add helper, clean up code, remove test route
This commit is contained in:
1 parent
c963982bfb
commit
f4f7373272
6 files changed
+56
-35
No files matched your search
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace App\Helpers;
|
||||
|
||||
use App\Models\Quote;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
|
||||
class Slack
|
||||
{
|
||||
private string $url;
|
||||
|
||||
public function __construct() {
|
||||
$this->url = config('bot.webhook');
|
||||
}
|
||||
|
||||
private function send($text): \GuzzleHttp\Promise\PromiseInterface|\Illuminate\Http\Client\Response
|
||||
{
|
||||
return Http::post($this->url, ['text' => $text]);
|
||||
}
|
||||
|
||||
public function sendText(string $text): \GuzzleHttp\Promise\PromiseInterface|\Illuminate\Http\Client\Response
|
||||
{
|
||||
activity()
|
||||
->event('send')
|
||||
->log("Manually sent quote: $text");
|
||||
|
||||
return $this->send($text);
|
||||
}
|
||||
|
||||
public function sendQuote(Quote $quote): \GuzzleHttp\Promise\PromiseInterface|\Illuminate\Http\Client\Response
|
||||
{
|
||||
activity()
|
||||
->performedOn($quote)
|
||||
->event('send')
|
||||
->log("Requested quote: $quote");
|
||||
|
||||
return $this->send($quote->quote);
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Helpers\Slack;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Quote;
|
||||
use Illuminate\Http\Request;
|
||||
@@ -9,11 +10,11 @@ use Illuminate\Support\Facades\Http;
|
||||
|
||||
class WebHookController extends Controller
|
||||
{
|
||||
public function webHookSend(string $payload)
|
||||
{
|
||||
// TODO: move this to a helper class so we can reuse code between API and FE
|
||||
$data = ['text' => $payload];
|
||||
$response = Http::post(config('bot.webhook'), $data);
|
||||
|
||||
private Slack $slack;
|
||||
|
||||
public function __construct() {
|
||||
$this->slack = new Slack();
|
||||
}
|
||||
|
||||
public function sendQuote(Request $request)
|
||||
@@ -24,28 +25,13 @@ class WebHookController extends Controller
|
||||
return;
|
||||
}
|
||||
|
||||
activity()
|
||||
->event('send')
|
||||
->log("Manually sent quote: $quote");
|
||||
|
||||
$this->webHookSend($quote);
|
||||
$this->slack->sendText($quote);
|
||||
}
|
||||
|
||||
public function sendRandomQuote()
|
||||
{
|
||||
$quote = Quote::inRandomOrder()->first();
|
||||
|
||||
activity()
|
||||
->performedOn($quote)
|
||||
->event('send')
|
||||
->log("Requested quote: $quote->quote");
|
||||
|
||||
$this->webHookSend($quote->quote);
|
||||
$this->slack->sendQuote($quote);
|
||||
}
|
||||
|
||||
public function test()
|
||||
{
|
||||
var_dump('secrets');
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Helpers\Slack;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Spatie\Activitylog\LogOptions;
|
||||
@@ -22,13 +23,9 @@ class Quote extends Model
|
||||
|
||||
public function request(): void
|
||||
{
|
||||
// Send the quote
|
||||
// If success, add it to the transactions
|
||||
$slack = new Slack();
|
||||
|
||||
activity()
|
||||
->performedOn($this)
|
||||
->event('send')
|
||||
->log("Requested quote: $this->quote");
|
||||
$slack->sendQuote($this);
|
||||
}
|
||||
|
||||
public function tapActivity(Activity $activity, string $eventName)
|
||||
|
||||
Reference in new issue
Block a user