<?php

namespace App\Classes\Modules\Companies\Services;


class GeneratesUniqueAccountNumber
{

    /** @var ChecksIfCompanyAccountNumberExists */
    private $accountNumberExists;

    /**
     * GeneratesUniqueAccountNumber constructor.
     * @param ChecksIfCompanyAccountNumberExists $accountNumberExists
     */
    public function __construct(ChecksIfCompanyAccountNumberExists $accountNumberExists)
    {
        $this->accountNumberExists = $accountNumberExists;
    }

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

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

    }

}