Completed
Push — develop ( 73bd0a...ccb498 )
by Tom
05:04
created

MakeThemeCommand::execute()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 45
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 2
Metric Value
c 3
b 0
f 2
dl 0
loc 45
rs 8.439
cc 6
eloc 27
nc 32
nop 2
1
<?php
2
/**
3
 * Copyright © 2016 netz98 new media GmbH. All rights reserved.
4
 * See COPYING.txt for license details.
5
 */
6
7
namespace N98\Magento\Command\Developer\Console;
8
9
use Magento\Framework\App\Filesystem\DirectoryList;
10
use Magento\Framework\Filesystem;
11
use Magento\Framework\Filesystem\Directory\WriteInterface;
12
use N98\Magento\Command\Developer\Console\Structure\ThemeNameStructure;
13
use N98\Magento\Command\Developer\Console\Util\Xml;
14
use Symfony\Component\Console\Input\InputArgument;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Output\OutputInterface;
17
18
class MakeThemeCommand extends AbstractGeneratorCommand
19
{
20
    protected function configure()
21
    {
22
        $this
23
            ->setName('make:theme')
24
            ->addArgument('area', InputArgument::REQUIRED, 'Area like "frontend"')
25
            ->addArgument('package', InputArgument::REQUIRED, 'Package like "Vendor"')
26
            ->addArgument('name', InputArgument::REQUIRED, 'Name of the theme')
27
            ->setDescription('Creates a new theme')
28
        ;
29
    }
30
31
    /**
32
     * @param InputInterface $input
33
     * @param OutputInterface $output
34
     *
35
     * @return int|void
36
     */
37
    protected function execute(InputInterface $input, OutputInterface $output)
38
    {
39
        $filesystem = $this->get(Filesystem::class);
40
        /** @var $filesystem \Magento\Framework\Filesystem */
41
        $appDirReader = $filesystem->getDirectoryRead(DirectoryList::APP);
42
        $appDirWriter = $filesystem->getDirectoryWrite(DirectoryList::APP);
43
44
        $themeName = new ThemeNameStructure(
45
            $input->getArgument('area'),
46
            $input->getArgument('package'),
47
            $input->getArgument('name')
48
        );
49
50
        $relativePath = 'design' . '/' . $themeName;
51
52
        if (!$appDirReader->isDirectory($relativePath)) {
53
            $appDirWriter->create($relativePath);
54
            $output->writeln('<info>generated </info><comment>' . $relativePath . '</comment>');
55
        }
56
57
        if (!$appDirReader->isFile($relativePath . '/registration.php')) {
58
            $this->createRegistrationFile($themeName, $appDirWriter, $relativePath);
59
        }
60
61
        if (!$appDirReader->isFile($relativePath . '/composer.json')) {
62
            $this->createComposerFile($themeName, $appDirWriter, $relativePath);
63
        }
64
65
        if (!$appDirReader->isFile($relativePath . '/theme.xml')) {
66
            $this->createThemeXmlFile(
67
                $output,
68
                $appDirWriter,
69
                $relativePath,
70
                ucfirst($input->getArgument('package')) . ' '. $input->getArgument('name')
71
            );
72
        }
73
74
        if (!$appDirReader->isFile($relativePath . '/etc/view.xml')) {
75
            $this->createViewXmlFile(
76
                $output,
77
                $appDirWriter,
78
                $relativePath
79
            );
80
        }
81
    }
82
83
    /**
84
     * @param ThemeNameStructure $themeName
85
     * @param \Magento\Framework\Filesystem\Directory\WriteInterface $appDirWriter
86
     * @param string $relativePath
87
     */
88
    private function createRegistrationFile(ThemeNameStructure $themeName, $appDirWriter, $relativePath)
89
    {
90
        $registrationFileBody = <<<FILE_BODY
91
<?php
92
93
\Magento\Framework\Component\ComponentRegistrar::register(
94
    \Magento\Framework\Component\ComponentRegistrar::THEME,
95
    '{$themeName->__toString()}',
96
    __DIR__
97
);
98
99
FILE_BODY;
100
        $appDirWriter->writeFile($relativePath . '/registration.php', $registrationFileBody);
101
    }
102
103
    /**
104
     * @param ThemeNameStructure $themeName
105
     * @param WriteInterface $appDirectoryWriter
106
     * @param string $relativePath
107
     */
108
    private function createComposerFile(ThemeNameStructure $themeName, WriteInterface $appDirectoryWriter, $relativePath)
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 121 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
109
    {
110
        $composerFileBody = $this->getHelper('twig')->render(
111
            'dev/console/make/theme/composer.json.twig',
112
            [
113
                'theme' => $themeName,
114
            ]
115
        );
116
117
        $appDirectoryWriter->writeFile(
118
            $relativePath . '/composer.json',
119
            $composerFileBody
120
        );
121
    }
122
123
    /**
124
     * @param OutputInterface $output
125
     * @param \Magento\Framework\Filesystem\Directory\WriteInterface $appDirWriter
126
     * @param string $relativePath
127
     * @param string $name
128
     */
129 View Code Duplication
    private function createThemeXmlFile(OutputInterface $output, $appDirWriter, $relativePath, $name)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
130
    {
131
        $xmlContent = <<<XML_CONTENT
132
<?xml version="1.0"?>
133
<theme xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Config/etc/theme.xsd">
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 136 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
134
    <title>$name</title>
135
    <parent>Magento/blank</parent>
136
    <media>
137
        <preview_image>media/preview.jpg</preview_image>
138
    </media>
139
</theme>
140
XML_CONTENT;
141
142
        $appDirWriter->writeFile($relativePath . '/theme.xml', $xmlContent);
143
        $output->writeln('<info>generated </info><comment>' . $relativePath . '/theme.xml</comment>');
144
    }
145
146
    /**
147
     * @param OutputInterface $output
148
     * @param \Magento\Framework\Filesystem\Directory\WriteInterface $appDirWriter
149
     * @param string $relativePath
150
     */
151 View Code Duplication
    private function createViewXmlFile(OutputInterface $output, $appDirWriter, $relativePath)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
152
    {
153
        $xmlContent = <<<XML_CONTENT
154
<?xml version="1.0"?>
155
<view xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Config/etc/view.xsd">
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 134 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
156
</view>
157
XML_CONTENT;
158
159
        $appDirWriter->writeFile($relativePath . '/etc/view.xml', $xmlContent);
160
        $output->writeln('<info>generated </info><comment>' . $relativePath . '/etc/view.xml</comment>');
161
    }
162
}
163