Files
webstatement/app/Providers/WebstatementServiceProvider.php
Daeng Deni Mardaeni df097a279f refactor(webstatement): optimalkan sinkronisasi dan pembaruan data kartu ATM
- Menghapus dependensi yang tidak digunakan untuk memperingkas kode.
- Memisahkan logika pembaruan branch dan currency menjadi job terpisah `UpdateAtmCardBranchCurrencyJob`.
- Menambahkan penjadwalan job untuk pembaruan branch dan currency setelah sinkronisasi kartu selesai.
- Mengubah query database untuk sinkronisasi kartu menjadi lebih sederhana.
- Menambahkan binding `UpdateAtmCardBranchCurrencyJob` di service provider.

Refactor ini meningkatkan readability dan modularitas kode dengan memisahkan tanggung jawab tiap proses.

Signed-off-by: Daeng Deni Mardaeni <ddeni05@gmail.com>
2025-05-09 15:42:18 +07:00

148 lines
4.8 KiB
PHP

<?php
namespace Modules\Webstatement\Providers;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;
use Modules\Webstatement\Console\GenerateBiayakartuCommand;
use Modules\Webstatement\Console\GenerateBiayaKartuCsvCommand;
use Modules\Webstatement\Jobs\UpdateAtmCardBranchCurrencyJob;
use Nwidart\Modules\Traits\PathNamespace;
class WebstatementServiceProvider extends ServiceProvider
{
use PathNamespace;
protected string $name = 'Webstatement';
protected string $nameLower = 'webstatement';
/**
* Boot the application events.
*/
public function boot(): void
{
$this->registerCommands();
$this->registerCommandSchedules();
$this->registerTranslations();
$this->registerConfig();
$this->registerViews();
$this->loadMigrationsFrom(module_path($this->name, 'database/migrations'));
if (class_exists('Breadcrumbs')) {
require __DIR__ . '/../../routes/breadcrumbs.php';
}
}
/**
* Register the service provider.
*/
public function register(): void
{
$this->app->register(EventServiceProvider::class);
$this->app->register(RouteServiceProvider::class);
$this->app->bind(UpdateAtmCardBranchCurrencyJob::class);
}
/**
* Register commands in the format of Command::class
*/
protected function registerCommands(): void
{
$this->commands([
GenerateBiayakartuCommand::class,
GenerateBiayaKartuCsvCommand::class
]);
}
/**
* Register command Schedules.
*/
protected function registerCommandSchedules(): void
{
$schedule = $this->app->make(Schedule::class);
// Mengambil tanggal dan waktu dari file .env untuk biaya kartu
$biayaKartuDay = env('BIAYA_KARTU_SCHEDULER_DAY', 14); // Default ke tanggal 14
$biayaKartuTime = env('BIAYA_KARTU_SCHEDULER_TIME', '22:00'); // Default ke jam 22:00
// Mengambil tanggal dan waktu dari file .env untuk biaya kartu CSV
$biayaKartuCsvDay = env('BIAYA_KARTU_CSV_SCHEDULER_DAY', 15); // Default ke tanggal 15
$biayaKartuCsvTime = env('BIAYA_KARTU_CSV_SCHEDULER_TIME', '00:00'); // Default ke jam 00:00
// Menjadwalkan job biaya kartu menggunakan nilai dari .env
$schedule->command('webstatement:generate-biaya-kartu')
->monthlyOn((int)$biayaKartuDay, $biayaKartuTime)
->appendOutputTo(storage_path('logs/biaya-kartu-scheduler.log'));
// Menjadwalkan job biaya kartu CSV menggunakan nilai dari .env
$schedule->command('webstatement:generate-biaya-kartu-csv')
->monthlyOn((int)$biayaKartuCsvDay, $biayaKartuCsvTime)
->appendOutputTo(storage_path('logs/biaya-kartu-csv-scheduler.log'));
}
/**
* Register translations.
*/
public function registerTranslations(): void
{
$langPath = resource_path('lang/modules/'.$this->nameLower);
if (is_dir($langPath)) {
$this->loadTranslationsFrom($langPath, $this->nameLower);
$this->loadJsonTranslationsFrom($langPath);
} else {
$this->loadTranslationsFrom(module_path($this->name, 'lang'), $this->nameLower);
$this->loadJsonTranslationsFrom(module_path($this->name, 'lang'));
}
}
/**
* Register config.
*/
protected function registerConfig(): void
{
$this->publishes([module_path($this->name, 'config/config.php') => config_path($this->nameLower.'.php')], 'config');
$this->mergeConfigFrom(module_path($this->name, 'config/config.php'), $this->nameLower);
}
/**
* Register views.
*/
public function registerViews(): void
{
$viewPath = resource_path('views/modules/'.$this->nameLower);
$sourcePath = module_path($this->name, 'resources/views');
$this->publishes([$sourcePath => $viewPath], ['views', $this->nameLower.'-module-views']);
$this->loadViewsFrom(array_merge($this->getPublishableViewPaths(), [$sourcePath]), $this->nameLower);
$componentNamespace = $this->module_namespace($this->name, $this->app_path(config('modules.paths.generator.component-class.path')));
Blade::componentNamespace($componentNamespace, $this->nameLower);
}
/**
* Get the services provided by the provider.
*/
public function provides(): array
{
return [];
}
private function getPublishableViewPaths(): array
{
$paths = [];
foreach (config('view.paths') as $path) {
if (is_dir($path.'/modules/'.$this->nameLower)) {
$paths[] = $path.'/modules/'.$this->nameLower;
}
}
return $paths;
}
}