1. Go to this page and download the library: Download nawasara/sync library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
nawasara / sync example snippets
use Nawasara\Sync\Concerns\HasSyncStatus;
use Nawasara\Sync\Concerns\TracksLastSync;
use Nawasara\Sync\Contracts\SyncedRepository;
use Nawasara\Sync\Jobs\AbstractSyncJob;
// 1. Snapshot model
class WhmEmailAccount extends Model
{
use HasSyncStatus;
public function computeContentHash(): string
{
return hash('sha256', json_encode([
'email' => $this->email,
'quota_mb' => $this->quota_mb,
// … fields you want to detect drift on
]));
}
}
// 2. Repository
class WhmEmailAccountRepository implements SyncedRepository
{
use TracksLastSync;
public function list(array $filters = [], int $perPage = 25) { /* ... */ }
public function create(array $data): ?SyncJob { /* dispatch CreateWhmEmailJob */ }
public function update($id, array $data): ?SyncJob { /* dispatch ChangeWhmEmail*Job */ }
public function delete($id): ?SyncJob { /* dispatch DeleteWhmEmailJob */ }
public function syncNow(): ?SyncJob { /* dispatch SyncWhmEmailsJob */ }
public function lastSyncedAt(): ?Carbon
{
return $this->lastSuccessfulSyncAt('whm', 'sync_emails');
}
}
// 3. Job
class ChangeWhmEmailQuotaJob extends AbstractSyncJob
{
protected function service(): string { return 'whm'; }
protected function action(): string { return 'quota_email'; }
protected function targetType(): ?string { return 'WhmEmailAccount'; }
protected function targetId(): ?string { return $this->payload['email']; }
protected function execute(): array
{
// call external API…
// update snapshot row…
return ['quota_mb' => $newQuota];
}
}