<commit_before><?php

namespace Happyr\LocoBundle\Service;

use Happyr\LocoBundle\Model\Message;
use Symfony\Component\Translation\Dumper\DumperInterface;
use Symfony\Component\Translation\Loader\LoaderInterface;
use Symfony\Component\Translation\MessageCatalogue;

/**
 * @author <NAME>
 *
 * Update the locale file system with changes in the catalogue
 */
class FilesystemUpdater
{
    /**
     * @var LoaderInterface loader
     */
    private $loader;

    /**
     * @var DumperInterface dumper
     */
    private $dumper;

    /**
     * @var string targetDir
     */
    private $targetDir;

    /**
     * Filesystem constructor.
     *
     * @param LoaderInterface $loader
     * @param DumperInterface $dumper
     * @param                 $targetDir
     */
    public function __construct(LoaderInterface $loader, DumperInterface $dumper, $targetDir)
    {
        $this->loader = $loader;
        $this->dumper = $dumper;
        $this->targetDir = $targetDir;
    }

    /**
     * Update message catalogues.
     *
     * @param Message[] $messages
     */
    public function updateMessageCatalog(array $messages)
    {
        /** @var MessageCatalogue[] $catalogues */
        $catalogues = array();
        foreach ($messages as $m) {
            $key = $m->getLocale().$m->getDomain();
            if (!isset($catalogues[$key])) {
                $file = sprintf('%s/%s.%s.phps', $this->targetDir, $m->getDomain(), $m->getLocale());
                $catalogues[$key] = $this->loader->load($file, $m->getLocale(), $m->getDomain());
            }

            $translation = $m->getTranslation();
            if (empty($translation)) {
                $translation = sprintf('[%s]', $m->getId());
            }

            $catalogues[$key]->set($m->getId(), $translation, $m->getDomain());
        }

        foreach ($catalogues as $catalogue) {
            $this->dumper->dump($catalogue, ['path' => $this->targetDir]);
        }
    }
}<commit_msg>Catch exception when dumping files<commit_after><?php

namespace Happyr\LocoBundle\Service;

use Happyr\LocoBundle\Model\Message;
use Symfony\Component\Translation\Dumper\DumperInterface;
use Symfony\Component\Translation\Loader\LoaderInterface;
use Symfony\Component\Translation\MessageCatalogue;

/**
 * @author <NAME>
 *
 * Update the locale file system with changes in the catalogue
 */
class FilesystemUpdater
{
    /**
     * @var LoaderInterface loader
     */
    private $loader;

    /**
     * @var DumperInterface dumper
     */
    private $dumper;

    /**
     * @var string targetDir
     */
    private $targetDir;

    /**
     * Filesystem constructor.
     *
     * @param LoaderInterface $loader
     * @param DumperInterface $dumper
     * @param                 $targetDir
     */
    public function __construct(LoaderInterface $loader, DumperInterface $dumper, $targetDir)
    {
        $this->loader = $loader;
        $this->dumper = $dumper;
        $this->targetDir = $targetDir;
    }

    /**
     * Update message catalogues.
     *
     * @param Message[] $messages
     */
    public function updateMessageCatalog(array $messages)
    {
        /** @var MessageCatalogue[] $catalogues */
        $catalogues = array();
        foreach ($messages as $m) {
            $key = $m->getLocale().$m->getDomain();
            if (!isset($catalogues[$key])) {
                $file = sprintf('%s/%s.%s.phps', $this->targetDir, $m->getDomain(), $m->getLocale());
                $catalogues[$key] = $this->loader->load($file, $m->getLocale(), $m->getDomain());
            }

            $translation = $m->getTranslation();
            if (empty($translation)) {
                $translation = sprintf('[%s]', $m->getId());
            }

            $catalogues[$key]->set($m->getId(), $translation, $m->getDomain());
        }

        foreach ($catalogues as $catalogue) {
            try {
                $this->dumper->dump($catalogue, ['path' => $this->targetDir]);
            } catch(\ErrorException $e) {
                // Could not save file
                // TODO better error handling
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.15108958837772396, 'fraction_numerical': 0.0, 'mean_word_length': 2.175249807840123, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 47, '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': '7123083', 'n_tokens_mistral': 1198, 'n_tokens_neox': 1178, 'n_words': 239}