<?php

namespace CouponsPlus\App\Coupon\Tree\ItemSetsToDiscount;

use CouponsPlus\App\Coupon\Tree\ItemSetsToDiscount\ItemsSetToDiscountCollection;
use CouponsPlus\App\Coupon\Tree\Abilities\ItemSetToDiscountCollectionHandler;

class ItemSetToDiscountCollectionHandlerComposite implements ItemSetToDiscountCollectionHandler
{
    /**
     * @var array<ItemSetToDiscountCollectionHandler>
     */
    protected array $itemsSetToDiscountHandlers;

    /**
     * @var array<ItemSetToDiscountCollectionHandler> $itemsSetToDiscountHandlers
     */
    public function __construct(array $itemsSetToDiscountHandlers)
    {
        $this->itemsSetToDiscountHandlers = $itemsSetToDiscountHandlers;
    }

    /**
     * It is possible for a handler to throw an exception.
     * In such case, the other handlers will not be executed. This is by design.
     */
    public function handle(ItemsSetToDiscountCollection $itemsSetToDiscount): void
    {
        foreach ($this->itemsSetToDiscountHandlers as $itemsSetToDiscountHandler) {
            $itemsSetToDiscountHandler->handle($itemsSetToDiscount);
        }
    }}