Completed
Push — develop ( f3a4fe...df0b60 )
by Tom
03:55
created

DumpCommand::execute()   C

Complexity

Conditions 7
Paths 24

Size

Total Lines 47
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 47
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 28
nc 24
nop 2
1
<?php
2
/**
3
 * *
4
 *  * Copyright © Elias Kotlyar - All rights reserved.
5
 *  * See LICENSE.md bundled with this module for license details.
6
 */
7
8
namespace N98\Magento\Command\Media;
9
10
use Magento\Framework\App\Filesystem\DirectoryList;
11
use N98\Magento\Command\AbstractMagentoCommand;
12
use Symfony\Component\Console\Input\InputArgument;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Input\InputOption;
15
use Symfony\Component\Console\Output\OutputInterface;
16
use Symfony\Component\Finder\Finder;
17
use Symfony\Component\Finder\SplFileInfo;
18
use ZipArchive;
19
20
class DumpCommand extends AbstractMagentoCommand
21
{
22
    /**
23
     * @var \Magento\Framework\Filesystem
24
     */
25
    private $filesystem;
26
27
    protected function configure()
28
    {
29
        $this
30
            ->setName('media:dump')
31
            ->addOption('strip', '', InputOption::VALUE_NONE, 'Excludes image cache')
32
            ->addArgument('filename', InputArgument::OPTIONAL, 'Dump filename')
33
            ->setDescription('Creates an archive with content of media folder.')
34
        ;
35
    }
36
37
    /**
38
     * @param \Magento\Framework\Filesystem $filesystem
39
     */
40
    public function inject(\Magento\Framework\Filesystem $filesystem)
41
    {
42
        $this->filesystem = $filesystem;
43
    }
44
45
    /**
46
     * @param InputInterface  $input
47
     * @param OutputInterface $output
48
     *
49
     * @return int|null|void
50
     */
51
    protected function execute(InputInterface $input, OutputInterface $output)
52
    {
53
        $commandConfig = $this->getCommandConfig();
54
55
        $mediaDirectoryReader = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA);
56
57
        $this->detectMagento($output);
58
        $finder = Finder::create()
59
            ->files()
60
            ->followLinks()
61
            ->in($mediaDirectoryReader->getAbsolutePath());
62
63
        if ($input->getOption('strip')) {
64
            $finder->exclude($commandConfig['strip']['folders']);
65
        }
66
67
        $filename = (string) $input->getArgument('filename');
68
69
        if (is_dir($filename)) { // support for dot dir
70
            $filename = realpath($filename);
71
            $filename .= '/';
72
        }
73
74
        if (empty($filename) || is_dir($filename)) {
75
            $filename .= 'media_' . date('Ymd_his') . '.zip';
76
        }
77
78
        $zip = new ZipArchive();
79
        $zip->open($filename, ZIPARCHIVE::CREATE);
80
        $zip->addEmptyDir('media');
81
        $lastFolder = '';
82
83
        foreach ($finder as $file) {
84
            /* @var $file SplFileInfo */
85
            $currentFolder = pathinfo($file->getRelativePathname(), PATHINFO_DIRNAME);
86
            if ($currentFolder != $lastFolder) {
87
                $output->writeln(
88
                    sprintf('<info>Compress directory:</info> <comment>media/%s</comment>', $currentFolder)
89
                );
90
            }
91
            $zip->addFile($file->getPathname(), 'media' . DIRECTORY_SEPARATOR . $file->getRelativePathname());
92
93
            $lastFolder = $currentFolder;
94
        }
95
96
        $zip->close();
97
    }
98
}
99