Split FetchConversionRates job up into 2 jobs
ADD
app/Jobs/FetchConversionRate.php
+<?php
+
+namespace App\Jobs;
+
+use App\Repositories\ConversionRateRepository;
+use App\Repositories\CurrencyRepository;
+use Exception;
+use GuzzleHttp\Client;
+use Illuminate\Bus\Queueable;
+use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Foundation\Bus\Dispatchable;
+use Illuminate\Queue\InteractsWithQueue;
+use Illuminate\Queue\SerializesModels;
+use Symfony\Component\DomCrawler\Crawler;
+
+class FetchConversionRate implements ShouldQueue
+{
+ use Dispatchable;
+ use InteractsWithQueue;
+ use Queueable;
+ use SerializesModels;
+
+ protected $baseCurrencyId;
+
+ private $currencyRepository;
+ private $conversionRateRepository;
+
+ public function __construct(int $baseCurrencyId)
+ {
+ $this->baseCurrencyId = $baseCurrencyId;
+ }
+
+ public function handle(
+ CurrencyRepository $currencyRepository,
+ ConversionRateRepository $conversionRateRepository
+ ): void {
+ $this->currencyRepository = $currencyRepository;
+ $this->conversionRateRepository = $conversionRateRepository;
+
+ $baseCurrency = $this->currencyRepository->getById($this->baseCurrencyId);
+
+ if (!$baseCurrency || !$baseCurrency->iso) {
+ return;
+ }
+
+ $client = new Client();
+
+ foreach ($this->currencyRepository->getAll() as $targetCurrency) {
+ if (!$targetCurrency->iso || $baseCurrency->iso === $targetCurrency->iso) {
+ continue;
+ }
+
+ $url = 'https://www.valutafx.com/' . $baseCurrency->iso . '-' . $targetCurrency->iso . '.htm';
+
+ try {
+ $response = $client->request('GET', $url);
+ } catch (Exception $e) {
+ continue;
+ }
+
+ $crawler = new Crawler($response->getBody()->__toString());
+
+ $result = $crawler->filter('.converter-result > .rate-value')->first()->text();
+ $rate = str_replace(',', '', $result); // Get rid of any potential thousands separators
+
+ $this->conversionRateRepository->createOrUpdate(
+ $baseCurrency->id,
+ $targetCurrency->id,
+ $rate
+ );
+ }
+ }
+}

MODIFY
app/Jobs/FetchConversionRates.php
app/Jobs/FetchConversionRates.php
namespace App\Jobs;
-use App\Repositories\ConversionRateRepository;
use App\Repositories\CurrencyRepository;
-use Exception;
-use GuzzleHttp\Client;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
-use Symfony\Component\DomCrawler\Crawler;
class FetchConversionRates implements ShouldQueue
{
@@ -21,51 +17,13 @@ class FetchConversionRates implements ShouldQueue
use SerializesModels;
private $currencyRepository;
- private $conversionRateRepository;
- public function __construct()
+ public function handle(CurrencyRepository $currencyRepository): void
{
- //
- }
-
- public function handle(
- CurrencyRepository $currencyRepository,
- ConversionRateRepository $conversionRateRepository
- ): void {
$this->currencyRepository = $currencyRepository;
- $this->conversionRateRepository = $conversionRateRepository;
-
- $client = new Client();
-
- foreach ($this->currencyRepository->getAll() as $baseCurrency) {
- if (!$baseCurrency->iso) {
- continue;
- }
-
- foreach ($this->currencyRepository->getAll() as $targetCurrency) {
- if (!$targetCurrency->iso || $baseCurrency->iso === $targetCurrency->iso) {
- continue;
- }
- $url = 'https://www.valutafx.com/' . $baseCurrency->iso . '-' . $targetCurrency->iso . '.htm';
-
- try {
- $response = $client->request('GET', $url);
- } catch (Exception $e) {
- continue;
- }
-
- $crawler = new Crawler($response->getBody()->__toString());
-
- $result = $crawler->filter('.converter-result > .rate-value')->first()->text();
- $rate = str_replace(',', '', $result); // Get rid of any potential thousands separators
-
- $this->conversionRateRepository->createOrUpdate(
- $baseCurrency->id,
- $targetCurrency->id,
- $rate
- );
- }
+ foreach ($this->currencyRepository->getAll() as $currency) {
+ FetchConversionRate::dispatch($currency->id);
}
}
}

MODIFY
app/Repositories/CurrencyRepository.php
app/Repositories/CurrencyRepository.php
@@ -7,6 +7,11 @@ use Illuminate\Database\Eloquent\Collection;
class CurrencyRepository
{
+ public function getById(int $id): ?Currency
+ {
+ return Currency::find($id);
+ }
+
public function getAll()
{
return Currency::all();

Split FetchConversionRates job up into 2 jobs