<commit_before><?php

namespace LizardsAndPumpkins\Product\ProductImage;

use LizardsAndPumpkins\Context\Context;
use LizardsAndPumpkins\Context\ContextBuilder\ContextLocale;
use LizardsAndPumpkins\Product\ProductImage\Exception\InvalidImageFileNameException;
use LizardsAndPumpkins\Product\ProductImage\Exception\InvalidImageVariantCodeException;
use LizardsAndPumpkins\Utils\FileStorage\StorageAgnosticFileUri;
use LizardsAndPumpkins\Utils\ImageStorage\Image;
use LizardsAndPumpkins\Utils\ImageStorage\ImageStorage;

class TwentyOneRunProductImageFileLocator implements ProductImageFileLocator
{
    const ORIGINAL = 'original';
    const LARGE = 'large';
    const MEDIUM = 'medium';
    const SMALL = 'small';
    const SEARCH_AUTOSUGGESTION = 'search-autosuggestion';

    private $imageVariantCodes = [
        self::ORIGINAL,
        self::LARGE,
        self::MEDIUM,
        self::SMALL,
        self::SEARCH_AUTOSUGGESTION,
    ];

    /**
     * @var ImageStorage
     */
    private $imageStorage;

    public function __construct(ImageStorage $imageStorage)
    {
        $this->imageStorage = $imageStorage;
    }

    /**
     * @param string $imageFileName
     * @param string $imageVariantCode
     * @param Context $context
     * @return Image
     */
    public function get($imageFileName, $imageVariantCode, Context $context)
    {
        $this->validateImageFileName($imageFileName);
        $this->validateImageVariantCode($imageVariantCode);

        $imageIdentifier = $this->buildIdentifier($imageFileName, $imageVariantCode);
        return $this->isImageFileAvailable($imageFileName, $imageIdentifier) ?
            $this->imageStorage->getFileReference($imageIdentifier) :
            $this->getPlaceholder($imageVariantCode, $context);
    }

    /**
     * @param string $imageFileName
     * @param string $imageVariantCode
     * @return StorageAgnosticFileUri
     */
    private function buildIdentifier($imageFileName, $imageVariantCode)
    {
        return $this->createIdentifierForString(sprintf('product/%s/%s', $imageVariantCode, $imageFileName));
    }

    /**
     * @param string $identifier
     * @return StorageAgnosticFileUri
     */
    private function createIdentifierForString($identifier)
    {
        return StorageAgnosticFileUri::fromString($identifier);
    }

    /**
     * @param string $imageVariantCode
     * @param Context $context
     * @return Image
     */
    public function getPlaceholder($imageVariantCode, Context $context)
    {
        $localeCode = $context->getValue(ContextLocale::CODE);
        $identifier = sprintf('product/placeholder/%s/placeholder-image-%s.jpg', $imageVariantCode, $localeCode);
        $placeholderIdentifier = $this->createIdentifierForString($identifier);
        return $this->imageStorage->getFileReference($placeholderIdentifier);
    }

    /**
     * @param string $imageVariantCode
     * @return string
     */
    private function getInvalidTypeStringRepresentation($imageVariantCode)
    {
        if (is_string($imageVariantCode)) {
            return $imageVariantCode;
        }
        if (is_object($imageVariantCode)) {
            return get_class($imageVariantCode);
        }
        return gettype($imageVariantCode);
    }

    /**
     * @param string $imageFileName
     */
    private function validateImageFileName($imageFileName)
    {
        if (!is_string($imageFileName)) {
            throw new InvalidImageFileNameException(sprintf(
                'The image file name must be a string, got "%s"',
                $this->getInvalidTypeStringRepresentation($imageFileName)
            ));
        }
    }

    /**
     * @param string $imageVariantCode
     */
    private function validateImageVariantCode($imageVariantCode)
    {
        if (!in_array($imageVariantCode, $this->imageVariantCodes)) {
            throw new InvalidImageVariantCodeException(sprintf(
                'The image variant code must be one of %s, got "%s"',
                implode(', ', $this->imageVariantCodes),
                $this->getInvalidTypeStringRepresentation($imageVariantCode)
            ));
        }
    }

    /**
     * @return string[]
     */
    public function getVariantCodes()
    {
        return $this->imageVariantCodes;
    }

    /**
     * @param string $imageFileName
     * @param StorageAgnosticFileUri $imageIdentifier
     * @return bool
     */
    private function isImageFileAvailable($imageFileName, StorageAgnosticFileUri $imageIdentifier)
    {
        return !$this->isEmpty($imageFileName) && $this->imageStorage->contains($imageIdentifier);
    }

    /**
     * @param string $imageFileName
     * @return bool
     */
    private function isEmpty($imageFileName)
    {
        return trim($imageFileName) === '';
    }
}
<commit_msg>Issue #653: Inline isEmpty method<commit_after><?php

namespace LizardsAndPumpkins\Product\ProductImage;

use LizardsAndPumpkins\Context\Context;
use LizardsAndPumpkins\Context\ContextBuilder\ContextLocale;
use LizardsAndPumpkins\Product\ProductImage\Exception\InvalidImageFileNameException;
use LizardsAndPumpkins\Product\ProductImage\Exception\InvalidImageVariantCodeException;
use LizardsAndPumpkins\Utils\FileStorage\StorageAgnosticFileUri;
use LizardsAndPumpkins\Utils\ImageStorage\Image;
use LizardsAndPumpkins\Utils\ImageStorage\ImageStorage;

class TwentyOneRunProductImageFileLocator implements ProductImageFileLocator
{
    const ORIGINAL = 'original';
    const LARGE = 'large';
    const MEDIUM = 'medium';
    const SMALL = 'small';
    const SEARCH_AUTOSUGGESTION = 'search-autosuggestion';

    private $imageVariantCodes = [
        self::ORIGINAL,
        self::LARGE,
        self::MEDIUM,
        self::SMALL,
        self::SEARCH_AUTOSUGGESTION,
    ];

    /**
     * @var ImageStorage
     */
    private $imageStorage;

    public function __construct(ImageStorage $imageStorage)
    {
        $this->imageStorage = $imageStorage;
    }

    /**
     * @param string $imageFileName
     * @param string $imageVariantCode
     * @param Context $context
     * @return Image
     */
    public function get($imageFileName, $imageVariantCode, Context $context)
    {
        $this->validateImageFileName($imageFileName);
        $this->validateImageVariantCode($imageVariantCode);

        $imageIdentifier = $this->buildIdentifier($imageFileName, $imageVariantCode);
        return $this->isImageFileAvailable($imageFileName, $imageIdentifier) ?
            $this->imageStorage->getFileReference($imageIdentifier) :
            $this->getPlaceholder($imageVariantCode, $context);
    }

    /**
     * @param string $imageFileName
     * @param string $imageVariantCode
     * @return StorageAgnosticFileUri
     */
    private function buildIdentifier($imageFileName, $imageVariantCode)
    {
        return $this->createIdentifierForString(sprintf('product/%s/%s', $imageVariantCode, $imageFileName));
    }

    /**
     * @param string $identifier
     * @return StorageAgnosticFileUri
     */
    private function createIdentifierForString($identifier)
    {
        return StorageAgnosticFileUri::fromString($identifier);
    }

    /**
     * @param string $imageVariantCode
     * @param Context $context
     * @return Image
     */
    public function getPlaceholder($imageVariantCode, Context $context)
    {
        $localeCode = $context->getValue(ContextLocale::CODE);
        $identifier = sprintf('product/placeholder/%s/placeholder-image-%s.jpg', $imageVariantCode, $localeCode);
        $placeholderIdentifier = $this->createIdentifierForString($identifier);
        return $this->imageStorage->getFileReference($placeholderIdentifier);
    }

    /**
     * @param string $imageVariantCode
     * @return string
     */
    private function getInvalidTypeStringRepresentation($imageVariantCode)
    {
        if (is_string($imageVariantCode)) {
            return $imageVariantCode;
        }
        if (is_object($imageVariantCode)) {
            return get_class($imageVariantCode);
        }
        return gettype($imageVariantCode);
    }

    /**
     * @param string $imageFileName
     */
    private function validateImageFileName($imageFileName)
    {
        if (!is_string($imageFileName)) {
            throw new InvalidImageFileNameException(sprintf(
                'The image file name must be a string, got "%s"',
                $this->getInvalidTypeStringRepresentation($imageFileName)
            ));
        }
    }

    /**
     * @param string $imageVariantCode
     */
    private function validateImageVariantCode($imageVariantCode)
    {
        if (!in_array($imageVariantCode, $this->imageVariantCodes)) {
            throw new InvalidImageVariantCodeException(sprintf(
                'The image variant code must be one of %s, got "%s"',
                implode(', ', $this->imageVariantCodes),
                $this->getInvalidTypeStringRepresentation($imageVariantCode)
            ));
        }
    }

    /**
     * @return string[]
     */
    public function getVariantCodes()
    {
        return $this->imageVariantCodes;
    }

    /**
     * @param string $imageFileName
     * @param StorageAgnosticFileUri $imageIdentifier
     * @return bool
     */
    private function isImageFileAvailable($imageFileName, StorageAgnosticFileUri $imageIdentifier)
    {
        return trim($imageFileName) !== '' && $this->imageStorage->contains($imageIdentifier);
    }
}

{'fraction_non_alphanumeric': 0.10665113323448422, 'fraction_numerical': 0.0003177292946409659, 'mean_word_length': 3.0320239111870197, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1693321', 'n_tokens_mistral': 2544, 'n_tokens_neox': 2509, 'n_words': 452}