Imbo\Image\ImagePreparation::generateImageIdentifier PHP Method

generateImageIdentifier() private method

Using the configured image identifier generator, attempt to generate a unique image identifier for the given image until we either have found a unique ID or we hit the maximum allowed attempts.
private generateImageIdentifier ( Imbo\EventManager\EventInterface $event, Image $image ) : string
$event Imbo\EventManager\EventInterface The current event
$image Imbo\Model\Image The event to generate the image identifier for
return string
    private function generateImageIdentifier(EventInterface $event, Image $image)
    {
        $database = $event->getDatabase();
        $config = $event->getConfig();
        $user = $event->getRequest()->getUser();
        $imageIdentifierGenerator = $config['imageIdentifierGenerator'];
        if (is_callable($imageIdentifierGenerator) && !$imageIdentifierGenerator instanceof GeneratorInterface) {
            $imageIdentifierGenerator = $imageIdentifierGenerator();
        }
        if ($imageIdentifierGenerator->isDeterministic()) {
            return $imageIdentifierGenerator->generate($image);
        }
        // Continue generating image identifiers until we get one that does not already exist
        $maxAttempts = 100;
        $attempts = 0;
        do {
            $imageIdentifier = $imageIdentifierGenerator->generate($image);
            $attempts++;
        } while ($attempts < $maxAttempts && $database->imageExists($user, $imageIdentifier));
        // Did we reach our max attempts limit?
        if ($attempts === $maxAttempts) {
            $e = new ImageException('Failed to generate unique image identifier', 503);
            $e->setImboErrorCode(Exception::IMAGE_IDENTIFIER_GENERATION_FAILED);
            // Tell the client it's OK to retry later
            $event->getResponse()->headers->set('Retry-After', 1);
            throw $e;
        }
        return $imageIdentifier;
    }