<?php


namespace CouponsPlus\App\Coupon\Tree\Data\Transformers;

use CouponsPlus\App\Coupon\Tree\Data\Transformers\Abilities\TransformationStrategy;
use CouponsPlus\App\Coupon\Tree\Data\Transformers\Abilities\DataTransformationMatcher;

class FirstTransformerStrategy implements TransformationStrategy
{
    /**
     * @param \CouponsPlus\Original\Collections\Collection $transformers
     * @param object $data
     * @return object
     * @throws \RuntimeException When no matching transformer is found
     */
    public function execute($transformers, $data)
    {
        $transformer = $transformers->find(function (DataTransformationMatcher $transformerMatcher) use ($data) {
            return $transformerMatcher->canTransform($data);
        });

        if (!$transformer) {
            throw new \RuntimeException('No matching transformer found for the given data');
        }

        return $transformer->transform($data);
    }
}
