<commit_before><?php
declare(strict_types = 1);

/*
 * This file is part of the package bk2k/bootstrap-package.
 *
 * For the full copyright and license information, please read the
 * LICENSE file that was distributed with this source code.
 */

namespace BK2K\BootstrapPackage\Service;

use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
 * GoogleFontService
 */
class GoogleFontService
{
    /**
     * @var string
     */
    protected $tempDirectory = 'typo3temp/assets/bootstrappackage/fonts';

    /**
     * @var string
     */
    protected $googleFontApiUrl = 'fonts.googleapis.com/css';

    /**
     * @param string $file
     * @return bool|string
     * @throws \Exception
     */
    public function getCachedFile($file)
    {
        if (!$this->supports($file)) {
            return false;
        }
        if ($this->isCached($file)) {
            return $this->getCssFileCacheName($file);
        }
        return $this->cacheFile($file) ? $this->getCssFileCacheName($file) : false;
    }

    /**
     * @param string $file
     * @return bool
     */
    protected function cacheFile($file)
    {
        $content = GeneralUtility::getUrl(
            $file,
            0,
            ['User-Agent' => 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)']
        );

        if (!$content) {
            return false;
        }

        // Ensure cache directory exists
        GeneralUtility::mkdir_deep($this->tempDirectory . '/' . $this->getCacheIdentifier($file));

        // Find and Download font files
        $pattern = '%url\((.*?)\)%';
        preg_match_all($pattern, $content, $matches, PREG_SET_ORDER);
        $fontFiles = [];
        foreach ($matches as $match) {
            $fontFiles[$match[1]] = $match[1];
        }
        foreach ($fontFiles as $fontFile) {
            $localFontFile = $this->getCacheDirectory($file) . '/' . basename($fontFile);
            $fontFileContent = GeneralUtility::getUrl($fontFile);
            file_put_contents($localFontFile, $fontFileContent);
            GeneralUtility::fixPermissions($localFontFile);
            $content = str_replace($fontFile, basename($fontFile), $content);
        }

        // Save CSS File
        $cacheFile = $this->getCacheDirectory($file) . '/' . 'webfont.css';
        file_put_contents(GeneralUtility::getFileAbsFileName($cacheFile), $content);
        GeneralUtility::fixPermissions($cacheFile);

        return true;
    }

    /**
     * @param string $file
     * @return bool
     */
    protected function supports($file)
    {<commit_msg>[TASK] Replace GeneralUtility::getUrl by RequestFactory (#910)<commit_after><?php
declare(strict_types = 1);

/*
 * This file is part of the package bk2k/bootstrap-package.
 *
 * For the full copyright and license information, please read the
 * LICENSE file that was distributed with this source code.
 */

namespace BK2K\BootstrapPackage\Service;

use Psr\Http\Message\ResponseInterface;
use TYPO3\CMS\Core\Http\RequestFactory;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
 * GoogleFontService
 */
class GoogleFontService
{
    /**
     * @var string
     */
    protected $tempDirectory = 'typo3temp/assets/bootstrappackage/fonts';

    /**
     * @var string
     */
    protected $googleFontApiUrl = 'fonts.googleapis.com/css';

    /**
     * @param string $file
     * @return bool|string
     * @throws \Exception
     */
    public function getCachedFile($file)
    {
        if (!$this->supports($file)) {
            return false;
        }
        if ($this->isCached($file)) {
            return $this->getCssFileCacheName($file);
        }
        return $this->cacheFile($file) ? $this->getCssFileCacheName($file) : false;
    }

    /**
     * @param string $file
     * @return bool
     */
    protected function cacheFile($file)
    {
        /** @var RequestFactory */
        $requestFactory = GeneralUtility::makeInstance(RequestFactory::class);

        /** @var ResponseInterface */
        $response = $requestFactory->request($file, 'GET', [
            'headers' => ['User-Agent' => 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)']
        ]);

        if ($response->getStatusCode() >= 300) {
            return false;
        } else {
            $content = $response->getBody()->getContents();
        }

        // Ensure cache directory exists
        GeneralUtility::mkdir_deep($this->tempDirectory . '/' . $this->getCacheIdentifier($file));

        // Find and Download font files
        $pattern = '%url\((.*?)\)%';
        preg_match_all($pattern, $content, $matches, PREG_SET_ORDER);
        $fontFiles = [];
        foreach ($matches as $match) {
            $fontFiles[$match[1]] = $match[1];
        }
        foreach ($fontFiles as $fontFile) {
            $localFontFile = $this->getCacheDirectory($file) . '/' . basename($fontFile);
            /** @var ResponseInterface */
            $response = $requestFactory->request($fontFile);
            if ($response->getStatusCode() >= 300) {
                continue;
            } else {
                $fontFileContent = $response->getBody()->getContents();
            }
            file_put_contents($localFontFile, $fontFileContent);
            GeneralUtility::fixPermissions($localFontFile);
            $content = str_replace($fontFile, basename($fontFile), $content);
        }

        // Save CSS File
        $cacheFile = $this->getCacheDirectory($file) . '/' . 'webfont.css';
        file_put_contents(GeneralUtility::getFileAbsFileName($cacheFile), $content);
        GeneralUtility::fixPermissions($cacheFile);

        return true;
    }

    /**
     * @param string $file
     * @return bool
     */
    protected function supports($file)
    {
{'fraction_non_alphanumeric': 0.14121739130434782, 'fraction_numerical': 0.007478260869565217, 'mean_word_length': 2.3397212543554007, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3207407', 'n_tokens_mistral': 1680, 'n_tokens_neox': 1658, 'n_words': 334}