-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace App\Console\Commands; | ||
|
||
use Illuminate\Console\Command; | ||
use Illuminate\Support\Collection; | ||
use App\Jobs\CacheBitcoinBlockHeight; | ||
use Illuminate\Support\Facades\Cache; | ||
|
||
class CacheBitcoinBlockHeightCommand extends Command | ||
{ | ||
protected $signature = 'bitcoin:cache-height'; | ||
|
||
protected $description = 'Cache the Bitcoin block height'; | ||
|
||
public function handle() | ||
{ | ||
CacheBitcoinBlockHeight::dispatchSync(); | ||
|
||
$height = Collection::make( | ||
Cache::get('bitcoin_block_height', default: []) | ||
) | ||
->get('height', default: 'unknown'); | ||
|
||
$this->info('The current Bitcoin block height is '.$height); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace App\Jobs; | ||
|
||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Foundation\Queue\Queueable; | ||
use Illuminate\Support\Facades\Cache; | ||
use Illuminate\Support\Facades\Http; | ||
use Illuminate\Support\Str; | ||
|
||
class CacheBitcoinBlockHeight implements ShouldQueue | ||
{ | ||
use Queueable; | ||
|
||
protected string $apiUrl; | ||
|
||
public function __construct() | ||
{ | ||
$this->apiUrl = Str::of(config('services.blockcypher.base_url')) | ||
->append('btc/main') | ||
->toString(); | ||
} | ||
|
||
public function handle(): void | ||
{ | ||
$response = Http::acceptJson() | ||
->get($this->apiUrl); | ||
|
||
if ($response->successful()) { | ||
Cache::put('bitcoin_block_height', $response->json()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace Tests\Unit\Jobs; | ||
|
||
use Tests\TestCase; | ||
use Illuminate\Support\Collection; | ||
use Illuminate\Support\Facades\Http; | ||
use App\Jobs\CacheBitcoinBlockHeight; | ||
use Illuminate\Support\Facades\Cache; | ||
use PHPUnit\Framework\Attributes\Test; | ||
|
||
class CacheBitcoinBlockHeightTest extends TestCase | ||
{ | ||
#[Test] | ||
public function it_caches_the_bitcoin_block_height(): void | ||
{ | ||
Http::fake([ | ||
'api.blockcypher.com/*' => Http::response([ | ||
'name' => 'BTC.main', | ||
'height' => 123456, | ||
], 200), | ||
]); | ||
|
||
CacheBitcoinBlockHeight::dispatch(); | ||
|
||
$this->assertTrue(Cache::has('bitcoin_block_height')); | ||
$this->assertEquals(123456, Collection::make( | ||
Cache::get('bitcoin_block_height'))->get('height') | ||
); | ||
} | ||
} |