<?php

namespace App\Classes\Modules\Orders\Services;


class GeneratesUniqueOrderNumber
{

    /** @var ChecksIfOrderNumberExists */
    private $checksIfOrderNumberExists;

    /**
     * GeneratesUniqueOrderNumber constructor.
     * @param ChecksIfOrderNumberExists $checksIfOrderNumberExists
     */
    public function __construct(ChecksIfOrderNumberExists $checksIfOrderNumberExists)
    {
        $this->checksIfOrderNumberExists = $checksIfOrderNumberExists;
    }


    /**
     * @return int
     */
    public function execute(): int {

        $number = mt_rand(100000001, 999999999);
        return !$this->checksIfOrderNumberExists->execute($number) ? $number : self::execute();

    }

}