<?php
declare(strict_types=1);

namespace Autogedal\StockLog\Plugin\Cron;

use Autogedal\StockLog\Model\StockSourceContext;

/**
 * Generic around plugin injected via virtual types in di.xml onto each cron class.
 * Sets the stock-change source before the cron runs, then resets it after.
 * No cron files are modified.
 */
class SetSourcePlugin
{
    private string $source;

    public function __construct(string $source)
    {
        $this->source = $source;
    }

    public function aroundExecute(mixed $subject, callable $proceed): mixed
    {
        StockSourceContext::getInstance()->setSource($this->source);
        try {
            return $proceed();
        } finally {
            StockSourceContext::getInstance()->reset();
        }
    }
}
