<commit_before><?php
/**
 * @author    <NAME> <<EMAIL>>
 * @copyright Copyright (c) 2015, Darvin Studio
 * @link      https://www.darvin-studio.ru
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Darvin\ImageBundle\EventListener;

use Darvin\ImageBundle\Entity\Image\AbstractImage;
use Darvin\Utils\Event\CloneEvent;
use Darvin\Utils\EventListener\AbstractOnFlushListener;
use Doctrine\ORM\Event\OnFlushEventArgs;
use Liip\ImagineBundle\Imagine\Cache\CacheManager;
use Liip\ImagineBundle\Imagine\Filter\FilterManager;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Vich\UploaderBundle\Storage\StorageInterface;

/**
 * Image event listener
 */
class ImageListener extends AbstractOnFlushListener
{
    /**
     * @var \Liip\ImagineBundle\Imagine\Cache\CacheManager
     */
    private $imagineCacheManager;

    /**
     * @var \Liip\ImagineBundle\Imagine\Filter\FilterManager
     */
    private $imagineFilterManager;

    /**
     * @var \Vich\UploaderBundle\Storage\StorageInterface
     */
    private $uploaderStorage;

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

    /**
     * @param \Liip\ImagineBundle\Imagine\Cache\CacheManager   $imagineCacheManager  Imagine cache manager
     * @param \Liip\ImagineBundle\Imagine\Filter\FilterManager $imagineFilterManager Imagine filter manager
     * @param \Vich\UploaderBundle\Storage\StorageInterface    $uploaderStorage      Uploader storage
     * @param string                                           $cacheDir             Cache directory
     */
    public function __construct(
        CacheManager $imagineCacheManager,
        FilterManager $imagineFilterManager,
        StorageInterface $uploaderStorage,
        $cacheDir
    ) {
        $this->imagineCacheManager = $imagineCacheManager;
        $this->imagineFilterManager = $imagineFilterManager;
        $this->uploaderStorage = $uploaderStorage;
        $this->cacheDir = $cacheDir;
    }

    /**
     * @param \Darvin\Utils\Event\CloneEvent $event Event
     */
    public function postClone(CloneEvent $event)
    {
        $original = $event->getOriginal();

        if (!$original instanceof AbstractImage) {
            return;
        }

        /** @var \Darvin\ImageBundle\Entity\Image\AbstractImage $clone */
        $clone = $event->getClone();

        $pathname = $this->uploaderStorage->resolvePath($original, AbstractImage::PROPERTY_FILE);
        $tmpPathname = $this->generateTmpPathname();

        $fs = new Filesystem();
        $fs->copy($pathname, $tmpPathname, true);

        $file = new UploadedFile($tmpPathname, $original->getFilename(), null, null, null, true);

        $clone
            ->setFile($file)
            ->setName($original->getName());
    }

    /**
     * {@inheritdoc}
     */
    public function onFlush(OnFlushEventArgs $args)
    {<commit_msg>Catch file not found exception in the ImageListener::postClone().<commit_after><?php
/**
 * @author    <NAME> <<EMAIL>>
 * @copyright Copyright (c) 2015, Darvin Studio
 * @link      https://www.darvin-studio.ru
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Darvin\ImageBundle\EventListener;

use Darvin\ImageBundle\Entity\Image\AbstractImage;
use Darvin\Utils\Event\CloneEvent;
use Darvin\Utils\EventListener\AbstractOnFlushListener;
use Doctrine\ORM\Event\OnFlushEventArgs;
use Liip\ImagineBundle\Imagine\Cache\CacheManager;
use Liip\ImagineBundle\Imagine\Filter\FilterManager;
use Symfony\Component\Filesystem\Exception\FileNotFoundException;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Vich\UploaderBundle\Storage\StorageInterface;

/**
 * Image event listener
 */
class ImageListener extends AbstractOnFlushListener
{
    /**
     * @var \Liip\ImagineBundle\Imagine\Cache\CacheManager
     */
    private $imagineCacheManager;

    /**
     * @var \Liip\ImagineBundle\Imagine\Filter\FilterManager
     */
    private $imagineFilterManager;

    /**
     * @var \Vich\UploaderBundle\Storage\StorageInterface
     */
    private $uploaderStorage;

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

    /**
     * @param \Liip\ImagineBundle\Imagine\Cache\CacheManager   $imagineCacheManager  Imagine cache manager
     * @param \Liip\ImagineBundle\Imagine\Filter\FilterManager $imagineFilterManager Imagine filter manager
     * @param \Vich\UploaderBundle\Storage\StorageInterface    $uploaderStorage      Uploader storage
     * @param string                                           $cacheDir             Cache directory
     */
    public function __construct(
        CacheManager $imagineCacheManager,
        FilterManager $imagineFilterManager,
        StorageInterface $uploaderStorage,
        $cacheDir
    ) {
        $this->imagineCacheManager = $imagineCacheManager;
        $this->imagineFilterManager = $imagineFilterManager;
        $this->uploaderStorage = $uploaderStorage;
        $this->cacheDir = $cacheDir;
    }

    /**
     * @param \Darvin\Utils\Event\CloneEvent $event Event
     */
    public function postClone(CloneEvent $event)
    {
        $original = $event->getOriginal();

        if (!$original instanceof AbstractImage) {
            return;
        }

        /** @var \Darvin\ImageBundle\Entity\Image\AbstractImage $clone */
        $clone = $event->getClone();

        $pathname = $this->uploaderStorage->resolvePath($original, AbstractImage::PROPERTY_FILE);
        $tmpPathname = $this->generateTmpPathname();

        $fs = new Filesystem();

        try {
            $fs->copy($pathname, $tmpPathname, true);
        } catch (FileNotFoundException $ex) {
            $event->setClone(null);

            return;
        }

        $file = new UploadedFile($tmpPathname, $original->getFilename(), null, null, null, true);

        $clone
            ->setFile($file)
            ->setName($original->getName());
    }

    /**
     * {@inheritdoc}
     */
    public function onFlush(OnFlushEventArgs $args)
    {
{'fraction_non_alphanumeric': 0.11204121056020605, 'fraction_numerical': 0.00128783000643915, 'mean_word_length': 2.8257389162561575, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 38, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2738672', 'n_tokens_mistral': 1757, 'n_tokens_neox': 1710, 'n_words': 337}