PHP code example of wizcodepl / laravel-pipe

1. Go to this page and download the library: Download wizcodepl/laravel-pipe 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/ */

    

wizcodepl / laravel-pipe example snippets


'drivers' => [
    'acme' => App\Pipes\Acme\AcmeDriver::class,
],

class MapBaseData
{
    public static function run(Item $item): void
    {
        $raw = $item->get('rawData');

        $item->set('baseData', [
            'sku' => $raw['Sku'],
            'price' => $raw['Price'],
            'brand' => $raw['Brand'],
        ]);
    }
}

$item = new Item(['rawData' => $apiRecord]);

$item->get('rawData');                  // read (with optional default)
$item->set('baseData', [...]);          // write
$item->has('product');                  // boolean (true even when value is null)
$item->fail('Missing brand', [          // throws StageFailureException with context
    'sku' => $sku,
]);

class ResolveColor
{
    public static function run(Item $item): void
    {
        $attrs = $item->get('attributes', []);
        $manufacturer = (string) ($attrs['manufacturer_color'] ?? '');
        $color = self::match($manufacturer);

        if ($color === null) {
            $item->fail('Could not resolve color', [
                'manufacturer_color' => $manufacturer,
            ]);
        }

        $attrs['color'] = $color->label();
        $item->set('attributes', $attrs);
    }
}

class ApparelPipeline extends AbstractPipeline
{
    protected function onStageError(string $stage, \Throwable $e, Item $item): void
    {
        Log::channel('sync_quality')->error('acme | stage failed', [
            'stage' => $stage,
            'sku' => ($item->get('baseData') ?? [])['sku'] ?? null,
            'message' => $e->getMessage(),
            ...($e instanceof StageFailureException ? $e->context : []),
        ]);
    }
}

protected function buildFailureRecord(string $stage, \Throwable $e, Item $item): array
{
    return [
        ...parent::buildFailureRecord($stage, $e, $item),
        'sku' => ($item->get('baseData') ?? [])['sku'] ?? null,
    ];
}

#[Group('e2e')]
class AcmeSupplierSyncTest extends TestCase
{
    use RefreshDatabase;

    public function test_sync_creates_products_from_real_api(): void
    {
        $run = app(AcmeDriver::class)->run(limit: 10);

        // Strict: 0 failures means every sample record made it through
        foreach ($run->getStages() as $stage => $stats) {
            $this->assertSame(0, $stats['failed'],
                "Stage {$stage} had {$stats['failed']} failures (entered={$stats['entered']})",
            );
        }

        $this->assertGreaterThan(0, Product::count());

        // Idempotency
        $first = Product::count();
        app(AcmeDriver::class)->run(limit: 10);
        $this->assertSame($first, Product::count());
    }
}
bash
php artisan vendor:publish --tag=laravel-pipe-config
bash
php artisan make:pipe Acme --pipeline=Apparel

app/Pipes/Acme/
├── AcmeDriver.php                ← loadItems() with TODO
├── AcmePipelineResolver.php      ← resolve() with TODO
└── ApparelPipeline/
    └── ApparelPipeline.php       ← stages() with TODO
bash
php artisan pipe:run acme --limit=10
bash
php artisan make:pipe {Name} [--pipeline=Main] [--force]
bash
php artisan pipe:run {driver} [--limit=N]