<?php
/**
* Created by PhpStorm.
* User: sergey
* Date: 10/16/14
* Time: 5:34 PM
*/
namespace Sindrive\ContentBundle\Listener;
use Sindrive\ContentBundle\ImageOptim\Process;
use Symfony\Component\EventDispatcher\GenericEvent;
use Symfony\Component\Filesystem\Filesystem;
class OptimizeListener
{
/**
* @var Process
*/
private $optimizer;
private $filesystem;
public function __construct(Process $optimizer, Filesystem $filesystem)
{
$this->optimizer = $optimizer;
$this->filesystem = $filesystem;
}
public function onCreate(GenericEvent $event)
{
$this->optimizer->process($event->getArgument('image'));
foreach ($event->getArgument('thumbs') as $thumb) {
$this->optimizer->process($thumb, false);
}
}
public function onDelete(GenericEvent $event)
{
$imagePath = $event->getArgument('image');
$filename = pathinfo($imagePath, \PATHINFO_FILENAME);
$imagePath = str_replace($filename . '.', $filename . '-orig.', $imagePath);
$this->filesystem->remove($imagePath);
}
}