Skip to content

Commit

Permalink
Fix command media:clean-uploads (#1728)
Browse files Browse the repository at this point in the history
* fix clean command

* csfixer

* fix tests

* cs
  • Loading branch information
tristanbes committed Mar 26, 2020
1 parent 780f229 commit d92e5b1
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 30 deletions.
42 changes: 33 additions & 9 deletions src/Command/CleanMediaCommand.php
Expand Up @@ -13,9 +13,12 @@

namespace Sonata\MediaBundle\Command;

use Sonata\MediaBundle\Filesystem\Local;
use Sonata\MediaBundle\Model\MediaManagerInterface;
use Sonata\MediaBundle\Provider\FileProvider;
use Sonata\MediaBundle\Provider\MediaProviderInterface;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Sonata\MediaBundle\Provider\Pool;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -26,13 +29,37 @@
/**
* @final since sonata-project/media-bundle 3.21.0
*/
class CleanMediaCommand extends ContainerAwareCommand
class CleanMediaCommand extends Command
{
/**
* @var MediaProviderInterface[]|null
*/
private $providers;

/**
* @var Pool
*/
private $mediaPool;

/**
* @var Local
*/
private $filesystemLocal;

/**
* @var MediaManagerInterface
*/
private $mediaManager;

public function __construct(Local $filesystemLocal, Pool $mediaPool, MediaManagerInterface $mediaManager)
{
parent::__construct();

$this->filesystemLocal = $filesystemLocal;
$this->mediaPool = $mediaPool;
$this->mediaManager = $mediaManager;
}

/**
* {@inheritdoc}
*/
Expand All @@ -51,14 +78,13 @@ protected function execute(InputInterface $input, OutputInterface $output)
$dryRun = (bool) $input->getOption('dry-run');
$verbose = $output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE;

$pool = $this->getContainer()->get('sonata.media.pool');
$finder = Finder::create();
$filesystem = new Filesystem();
$baseDirectory = $this->getContainer()->get('sonata.media.adapter.filesystem.local')->getDirectory();
$baseDirectory = $this->filesystemLocal->getDirectory();

$output->writeln(sprintf('<info>Scanning upload directory: %s</info>', $baseDirectory));

foreach ($pool->getContexts() as $contextName => $context) {
foreach ($this->mediaPool->getContexts() as $contextName => $context) {
if (!$filesystem->exists($baseDirectory.'/'.$contextName)) {
$output->writeln(sprintf("<info>'%s' does not exist</info>", $baseDirectory.'/'.$contextName));

Expand Down Expand Up @@ -102,9 +128,7 @@ private function getProviders(): array
if (!$this->providers) {
$this->providers = [];

$pool = $this->getContainer()->get('sonata.media.pool');

foreach ($pool->getProviders() as $provider) {
foreach ($this->mediaPool->getProviders() as $provider) {
if ($provider instanceof FileProvider) {
$this->providers[] = $provider->getName();
}
Expand All @@ -116,7 +140,7 @@ private function getProviders(): array

private function mediaExists(string $filename, string $context = null): bool
{
$mediaManager = $this->getContainer()->get('sonata.media.manager.media');
$mediaManager = $this->mediaManager;

$fileParts = explode('_', $filename);

Expand Down
3 changes: 3 additions & 0 deletions src/Resources/config/command.xml
Expand Up @@ -8,6 +8,9 @@
<tag name="console.command"/>
</service>
<service id="Sonata\MediaBundle\Command\CleanMediaCommand" class="Sonata\MediaBundle\Command\CleanMediaCommand" public="true">
<argument type="service" id="sonata.media.adapter.filesystem.local"/>
<argument type="service" id="sonata.media.pool"/>
<argument type="service" id="sonata.media.manager.media"/>
<tag name="console.command"/>
</service>
<service id="Sonata\MediaBundle\Command\FixMediaContextCommand" class="Sonata\MediaBundle\Command\FixMediaContextCommand" public="true">
Expand Down
29 changes: 8 additions & 21 deletions tests/Command/CleanMediaCommandTest.php
Expand Up @@ -21,29 +21,23 @@
use Sonata\MediaBundle\Provider\FileProvider;
use Sonata\MediaBundle\Provider\Pool;
use Sonata\MediaBundle\Tests\Fixtures\FilesystemTestCase;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\DependencyInjection\Container;

/**
* @author Sullivan Senechal <soullivaneuh@gmail.com>
*/
class CleanMediaCommandTest extends FilesystemTestCase
{
/**
* @var Container
*/
protected $container;

/**
* @var Application
*/
protected $application;

/**
* @var ContainerAwareCommand
* @var Command
*/
protected $command;

Expand Down Expand Up @@ -74,26 +68,19 @@ protected function setUp(): void
{
parent::setUp();

$this->container = new Container();

$this->command = new CleanMediaCommand();
$this->command->setContainer($this->container);

$this->application = new Application();
$this->application->add($this->command);

$this->tester = new CommandTester($this->application->find('sonata:media:clean-uploads'));

$this->pool = $pool = $this->createMock(Pool::class);

$this->mediaManager = $mediaManager = $this->createMock(MediaManagerInterface::class);

$this->fileSystemLocal = $fileSystemLocal = $this->createMock(Local::class);
$this->fileSystemLocal->expects($this->once())->method('getDirectory')->willReturn($this->workspace);

$this->container->set('sonata.media.pool', $pool);
$this->container->set('sonata.media.manager.media', $mediaManager);
$this->container->set('sonata.media.adapter.filesystem.local', $fileSystemLocal);
$this->command = new CleanMediaCommand($this->fileSystemLocal, $this->pool, $this->mediaManager);

$this->application = new Application();
$this->application->add($this->command);

$this->tester = new CommandTester($this->application->find('sonata:media:clean-uploads'));
}

public function testExecuteDirectoryNotExists(): void
Expand Down

0 comments on commit d92e5b1

Please sign in to comment.