PHP code example of zuko / syncro-sheet

1. Go to this page and download the library: Download zuko/syncro-sheet 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/ */

    

zuko / syncro-sheet example snippets


// config/syncro-sheet.php

return [
    'defaults' => [
        'batch_size' => 1000,
        'timeout' => 600,
        'retries' => 3
    ],
    // ... other configurations
];

use Zuko\SyncroSheet\Contracts\SheetSyncable;

class Product extends Model implements SheetSyncable
{
    public function getSheetIdentifier(): string
    {
        return '1234567890-your-google-sheet-id';
    }

    public function getSheetName(): string
    {
        return 'Products';
    }

    public function toSheetRow(): array
    {
        return [
            $this->id,
            $this->name,
            $this->price,
            $this->stock,
            $this->updated_at->format('Y-m-d H:i:s')
        ];
    }

    public function getBatchSize(): ?int
    {
        return 500; // Optional, defaults to config value
    }
}

use Zuko\SyncroSheet\Services\SyncManager;

// Full sync
$syncManager = app(SyncManager::class);
$syncState = $syncManager->fullSync(Product::class);

// Partial sync
$syncState = $syncManager->partialSync(Product::class, [1, 2, 3]);

use SyncroSheet;
// or
use Zuko\SyncroSheet\Facades\SyncroSheet;

// Full sync
$syncState = SyncroSheet::fullSync(Product::class);

// Partial sync
$syncState = SyncroSheet::partialSync(Product::class, [1, 2, 3]);

// Get last sync state
$lastSync = SyncroSheet::getLastSync(Product::class);

use Zuko\SyncroSheet\Services\DataTransformer;

class ProductTransformer extends DataTransformer
{
    protected function transformRecord(SheetSyncable $record): array
    {
        return [
            'ID' => $record->id,
            'Product Name' => $record->name,
            'Price' => number_format($record->price, 2),
            'In Stock' => $record->stock > 0 ? 'Yes' : 'No',
            'Last Updated' => $record->updated_at->format('Y-m-d H:i:s')
        ];
    }
}

use Zuko\SyncroSheet\Events\SyncEvent;

Event::listen(SyncEvent::SYNC_STARTED, function ($syncState) {
    Log::info("Sync started for {$syncState->model_class}");
});

Event::listen(SyncEvent::SYNC_COMPLETED, function ($syncState) {
    Log::info("Sync completed: {$syncState->total_processed} records");
});

use Zuko\SyncroSheet\Notifications\BaseNotification;

class CustomSyncNotification extends BaseNotification
{
    protected function getMailMessage(): MailMessage
    {
        return (new MailMessage)
            ->subject('Custom Sync Notification')
            ->line('Your custom notification logic here');
    }

    protected function getSlackMessage(): SlackMessage
    {
        return (new SlackMessage)
            ->content('Custom Slack notification');
    }
}

use Zuko\SyncroSheet\Services\StateManager;

$stateManager = app(StateManager::class);

// Get last successful sync
$lastSync = $stateManager->getLastSuccessfulSync(Product::class);

// Get sync history
$syncHistory = \Zuko\SyncroSheet\Models\SyncState::where('model_class', Product::class)
    ->with('entries')
    ->latest()
    ->get();

use Zuko\SyncroSheet\Services\ErrorHandler;

try {
    $syncManager->fullSync(Product::class);
} catch (GoogleSheetsException $e) {
    // Handle Google Sheets specific errors
} catch (SyncException $e) {
    // Handle general sync errors
}

public function getBatchSize(): int
{
    return $this->hasMedia() ? 100 : 1000;
}

// config/syncro-sheet.php
'sheets' => [
    'rate_limit' => [
        'max_requests' => 100,
        'per_seconds' => 60
    ]
]

use Zuko\SyncroSheet\Notifications\SyncFailedNotification;

class SlackSyncNotifier extends Notification
{
    public function toSlack($notifiable)
    {
        // Custom Slack notification logic
    }
}
bash
php artisan vendor:publish --provider="Zuko\SyncroSheet\LaravelSyncroSheetProvider"
php artisan migrate
bash
# Full sync
php artisan sheet:sync Product

# Partial sync
php artisan sheet:sync Product --ids=1,2,3