<commit_before><?php

namespace App\Cleaner;

use Doctrine\Common\Persistence\ObjectManager;
use Liip\ImagineBundle\Imagine\Cache\CacheManager;
use Symfony\Component\Finder\Finder;

/**
 * Created by PhpStorm.
 * User: guillaume
 * Date: 23/11/2016
 * Time: 21:11.
 */
class ImageCleaner
{
    /**
     * @var ObjectManager
     */
    private $entityManager;

    /**
     * @var CacheManager
     */
    private $cacheManager;

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

    public function __construct(ObjectManager $entityManager, CacheManager $cacheManager, $webDir)
    {
        $this->entityManager = $entityManager;
        $this->webDir        = $webDir;
        $this->cacheManager  = $cacheManager;
    }

    public function clean()
    {
        $result = $this
            ->entityManager
            ->createQuery('SELECT a.path, a.systemPath FROM App:Agenda a WHERE a.path IS NOT NULL OR a.systemPath IS NOT NULL')
            ->getScalarResult();

        $paths = \array_unique(\array_filter(\array_merge(\array_column($result, 'path'), \array_column($result, 'systemPath'))));
        $this->cleanPaths($paths, ['thumbs_evenement', 'thumb_evenement'], '/uploads/documents');

        $result = $this
            ->entityManager
            ->createQuery('SELECT u.path, u.systemPath FROM App:User u WHERE u.path IS NOT NULL OR u.systemPath IS NOT NULL')
            ->getScalarResult();

        $paths = \array_unique(\array_filter(\array_merge(\array_column($result, 'path'), \array_column($result, 'systemPath'))));
        $this->cleanPaths($paths, ['thumb_user_large', 'thumb_user_evenement', 'thumb_user', 'thumb_user_menu', 'thumb_user_50', 'thumb_user_115'], '/uploads/users');
    }

    protected function cleanPaths(array $paths, array $filters, $uri_prefix)
    {
        $finder = new Finder();
        $files  = $finder->in($this->webDir . $uri_prefix);
        foreach ($files as $file) {
            /*
             * @var \SplFileObject
             */
            if (!$file->getFilename()) {
                continue;
            }

            if (!\in_array($file->getFilename(), $paths)) {
                $path = $uri_prefix . '/' . $file->getFilename();
                foreach ($filters as $filter) {
                    if ($this->cacheManager->isStored($path, $filter)) {
                        $this->cacheManager->remove($path, $filter);
                    }
                }
            }
        }
    }
}<commit_msg>Fixed image cleaner<commit_after><?php

namespace App\Cleaner;

use Doctrine\Common\Persistence\ObjectManager;
use Liip\ImagineBundle\Imagine\Cache\CacheManager;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;

/**
 * Created by PhpStorm.
 * User: guillaume
 * Date: 23/11/2016
 * Time: 21:11.
 */
class ImageCleaner
{
    /**
     * @var ObjectManager
     */
    private $entityManager;

    /**
     * @var CacheManager
     */
    private $cacheManager;

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

    public function __construct(ObjectManager $entityManager, CacheManager $cacheManager, $webDir)
    {
        $this->entityManager = $entityManager;
        $this->webDir        = $webDir;
        $this->cacheManager  = $cacheManager;
    }

    public function clean()
    {
        $result = $this
            ->entityManager
            ->createQuery('SELECT a.path, a.systemPath FROM App:Agenda a WHERE a.path IS NOT NULL OR a.systemPath IS NOT NULL')
            ->getScalarResult();

        $paths = \array_unique(\array_filter(\array_merge(\array_column($result, 'path'), \array_column($result, 'systemPath'))));
        $this->cleanPaths($paths, ['thumbs_evenement', 'thumb_evenement'], '/uploads/documents');

        $result = $this
            ->entityManager
            ->createQuery('SELECT u.path, u.systemPath FROM App:User u WHERE u.path IS NOT NULL OR u.systemPath IS NOT NULL')
            ->getScalarResult();

        $paths = \array_unique(\array_filter(\array_merge(\array_column($result, 'path'), \array_column($result, 'systemPath'))));
        $this->cleanPaths($paths, ['thumb_user_large', 'thumb_user_evenement', 'thumb_user', 'thumb_user_menu', 'thumb_user_50', 'thumb_user_115'], '/uploads/users');
    }

    protected function cleanPaths(array $paths, array $filters, $uri_prefix)
    {
        $fs = new Filesystem();
        $finder = new Finder();
        $files  = $finder->files()->in($this->webDir . $uri_prefix);
        foreach ($files as $file) {
            /** @var \SplFileObject $file */
            if (\in_array($file->getFilename(), $paths)) {
                continue;
            }

            //Orphan file
            $path = ltrim(str_replace($this->webDir, '', $file->getPathname()), DIRECTORY_SEPARATOR);
            foreach ($filters as $filter) {
                if ($this->cacheManager->isStored($path, $filter)) {
                    $this->cacheManager->remove($path, $filter);
                }
            }

            $fs->remove($file->getPathname());
        }
    }
}
{'fraction_non_alphanumeric': 0.14541871921182267, 'fraction_numerical': 0.006699507389162562, 'mean_word_length': 2.337278106508876, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 46, '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': '5368412', 'n_tokens_mistral': 1522, 'n_tokens_neox': 1459, 'n_words': 304}