Completed
Push — master ( e403ee...648e0f )
by C
14:40
created

ProcessDiscFolderCommand::execute()   D

Complexity

Conditions 9
Paths 10

Size

Total Lines 40
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 9

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 40
ccs 19
cts 19
cp 1
rs 4.909
cc 9
eloc 18
nc 10
nop 2
crap 9
1
<?php
2
namespace Tartana\Console\Command;
3
4
use League\Flysystem\Adapter\Local;
5
use Monolog\Logger;
6
use Symfony\Component\Console\Command\Command as SymfonyCommand;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Tartana\Component\Command\Command;
11
use Tartana\Mixins\LoggerAwareTrait;
12
use Tartana\Util;
13
14
class ProcessDiscFolderCommand extends SymfonyCommand
15
{
16
	use LoggerAwareTrait;
17
18 6
	public function __construct()
19
	{
20 6
		parent::__construct('process:disc');
21 6
	}
22
23 6
	protected function configure()
24
	{
25 6
		$this->setDescription(
26 6
				'Converts folders with the name CD and which contains mp3 files to a flat structure. This command is running in foreground!');
27
28 6
		$this->addArgument('source', InputArgument::REQUIRED, 'The folder to process.');
29 6
	}
30
31 4
	protected function execute(InputInterface $input, OutputInterface $output)
32
	{
33 4
		$source = Util::realPath($input->getArgument('source'));
34 4
		if (empty($source))
35
		{
36 1
			$this->log('Source directory no found to convert.', Logger::ERROR);
37 1
			return;
38
		}
39
40 3
		$source = new Local($source);
41
42 3
		foreach ($source->listContents('', true) as $directory)
43
		{
44 3
			if ($directory['type'] != 'dir')
45
			{
46
				// Not a directory
47 3
				continue;
48
			}
49
50 3
			$dirName = basename($directory['path']);
51 3
			if ($dirName != 'CD' && $dirName != 'Cover')
52
			{
53
				// Not meant to be processed
54 3
				continue;
55
			}
56
57 3
			foreach ($source->listContents($directory['path'], false) as $file)
58
			{
59 3
				if ($file['type'] != 'file')
60
				{
61 2
					continue;
62
				}
63 3
				$source->rename($file['path'], dirname(dirname($file['path'])) . '/' . basename($file['path']));
64
			}
65 3
			if (empty($source->listContents($directory['path'], false)))
66
			{
67 3
				$source->deleteDir($directory['path']);
68
			}
69
		}
70 3
	}
71
}
72