Skip to content

Commit bd18896

Browse files
committed
fix: improve theme builder to reduce cyclomatic complexity
1 parent 492cfd6 commit bd18896

File tree

3 files changed

+136
-64
lines changed

3 files changed

+136
-64
lines changed

src/Service/ThemeBuilder/HyvaThemes/Builder.php

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,29 @@ public function build(string $themePath, SymfonyStyle $io, OutputInterface $outp
6464
return false;
6565
}
6666

67-
// Generate Hyva Configuration
67+
if (!$this->generateHyvaConfig($io, $isVerbose)) {
68+
return false;
69+
}
70+
71+
if (!$this->buildTheme($themePath, $io, $isVerbose)) {
72+
return false;
73+
}
74+
75+
// Deploy static content
76+
$themeCode = basename($themePath);
77+
if (!$this->staticContentDeployer->deploy($themeCode, $io, $output, $isVerbose)) {
78+
return false;
79+
}
80+
81+
// Clean cache using the dedicated service
82+
return $this->cacheCleaner->clean($io, $isVerbose);
83+
}
84+
85+
/**
86+
* Generate Hyva configuration
87+
*/
88+
private function generateHyvaConfig(SymfonyStyle $io, bool $isVerbose): bool
89+
{
6890
try {
6991
if ($isVerbose) {
7092
$io->text('Generating Hyvä configuration...');
@@ -73,12 +95,18 @@ public function build(string $themePath, SymfonyStyle $io, OutputInterface $outp
7395
if ($isVerbose) {
7496
$io->success('Hyvä configuration generated successfully.');
7597
}
98+
return true;
7699
} catch (\Exception $e) {
77100
$io->error('Failed to generate Hyvä configuration: ' . $e->getMessage());
78101
return false;
79102
}
103+
}
80104

81-
// Build Hyva theme
105+
/**
106+
* Build the Hyva theme
107+
*/
108+
private function buildTheme(string $themePath, SymfonyStyle $io, bool $isVerbose): bool
109+
{
82110
$tailwindPath = rtrim($themePath, '/') . '/web/tailwind';
83111
if (!$this->fileDriver->isDirectory($tailwindPath)) {
84112
$io->error("Tailwind directory not found in: $tailwindPath");
@@ -97,26 +125,13 @@ public function build(string $themePath, SymfonyStyle $io, OutputInterface $outp
97125
if ($isVerbose) {
98126
$io->success('Hyvä theme build completed successfully.');
99127
}
128+
chdir($currentDir);
129+
return true;
100130
} catch (\Exception $e) {
101131
$io->error('Failed to build Hyvä theme: ' . $e->getMessage());
102132
chdir($currentDir);
103133
return false;
104134
}
105-
106-
chdir($currentDir);
107-
108-
// Deploy static content
109-
$themeCode = basename($themePath);
110-
if (!$this->staticContentDeployer->deploy($themeCode, $io, $output, $isVerbose)) {
111-
return false;
112-
}
113-
114-
// Clean cache using the dedicated service
115-
if (!$this->cacheCleaner->clean($io, $isVerbose)) {
116-
return false;
117-
}
118-
119-
return true;
120135
}
121136

122137
public function autoRepair(string $themePath, SymfonyStyle $io, OutputInterface $output, bool $isVerbose): bool

src/Service/ThemeBuilder/MagentoStandard/Builder.php

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,33 @@ public function build(string $themePath, SymfonyStyle $io, OutputInterface $outp
8282
public function autoRepair(string $themePath, SymfonyStyle $io, OutputInterface $output, bool $isVerbose): bool
8383
{
8484
// Check for node_modules in root
85+
if (!$this->installNodeModulesIfMissing($io, $isVerbose)) {
86+
return false;
87+
}
88+
89+
// Check for grunt
90+
if (!$this->installGruntIfMissing($io, $isVerbose)) {
91+
return false;
92+
}
93+
94+
// Check for outdated packages
95+
if ($isVerbose) {
96+
$this->checkOutdatedPackages($io);
97+
}
98+
99+
return true;
100+
}
101+
102+
/**
103+
* Install Node modules if they're missing
104+
*/
105+
private function installNodeModulesIfMissing(SymfonyStyle $io, bool $isVerbose): bool
106+
{
85107
if (!$this->fileDriver->isDirectory('node_modules')) {
86108
if ($isVerbose) {
87109
$io->warning('Node modules not found in root directory. Running npm ci...');
88110
}
111+
89112
try {
90113
if ($this->fileDriver->isExists('package-lock.json')) {
91114
$this->shell->execute('npm ci --quiet');
@@ -95,47 +118,63 @@ public function autoRepair(string $themePath, SymfonyStyle $io, OutputInterface
95118
}
96119
$this->shell->execute('npm install --quiet');
97120
}
121+
98122
if ($isVerbose) {
99123
$io->success('Node modules installed successfully.');
100124
}
125+
126+
return true;
101127
} catch (\Exception $e) {
102128
$io->error('Failed to install node modules: ' . $e->getMessage());
103129
return false;
104130
}
105131
}
106132

107-
// Check for grunt
133+
return true;
134+
}
135+
136+
/**
137+
* Install Grunt if it's missing
138+
*/
139+
private function installGruntIfMissing(SymfonyStyle $io, bool $isVerbose): bool
140+
{
108141
try {
109142
$this->shell->execute('which grunt');
143+
return true;
110144
} catch (\Exception $e) {
111145
if ($isVerbose) {
112146
$io->warning('Grunt not found globally. Installing grunt...');
113147
}
148+
114149
try {
115150
$this->shell->execute('npm install -g grunt-cli --quiet');
151+
116152
if ($isVerbose) {
117153
$io->success('Grunt installed successfully.');
118154
}
155+
156+
return true;
119157
} catch (\Exception $e) {
120158
$io->error('Failed to install grunt: ' . $e->getMessage());
121159
return false;
122160
}
123161
}
162+
}
124163

125-
// Check for outdated packages
126-
if ($isVerbose) {
127-
try {
128-
$outdated = $this->shell->execute('npm outdated --json');
129-
if ($outdated) {
130-
$io->warning('Outdated packages found:');
131-
$io->writeln($outdated);
132-
}
133-
} catch (\Exception $e) {
134-
// Ignore errors from npm outdated as it returns non-zero when packages are outdated
164+
/**
165+
* Check for outdated packages and report them
166+
*/
167+
private function checkOutdatedPackages(SymfonyStyle $io): void
168+
{
169+
try {
170+
$outdated = $this->shell->execute('npm outdated --json');
171+
if ($outdated) {
172+
$io->warning('Outdated packages found:');
173+
$io->writeln($outdated);
135174
}
175+
} catch (\Exception $e) {
176+
// Ignore errors from npm outdated as it returns non-zero when packages are outdated
136177
}
137-
138-
return true;
139178
}
140179

141180
public function watch(string $themePath, SymfonyStyle $io, OutputInterface $output, bool $isVerbose): bool

src/Service/ThemeBuilder/TailwindCSS/Builder.php

Lines changed: 52 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -111,51 +111,69 @@ public function autoRepair(string $themePath, SymfonyStyle $io, OutputInterface
111111

112112
// Check for node_modules directory
113113
if (!$this->fileDriver->isDirectory($tailwindPath . '/node_modules')) {
114-
if ($isVerbose) {
115-
$io->warning('Node modules not found in tailwind directory. Installing npm dependencies ...');
116-
}
117-
118-
$currentDir = getcwd();
119-
chdir($tailwindPath);
120-
121-
try {
122-
if ($this->fileDriver->isExists($tailwindPath . '/package-lock.json')) {
123-
$this->shell->execute('npm ci --quiet');
124-
} else {
125-
if ($isVerbose) {
126-
$io->warning('No package-lock.json found, running npm install...');
127-
}
128-
$this->shell->execute('npm install --quiet');
129-
}
130-
if ($isVerbose) {
131-
$io->success('Node modules installed successfully.');
132-
}
133-
} catch (\Exception $e) {
134-
$io->error('Failed to install node modules: ' . $e->getMessage());
135-
chdir($currentDir);
114+
if (!$this->installNodeModules($tailwindPath, $io, $isVerbose)) {
136115
return false;
137116
}
138-
139-
chdir($currentDir);
140117
}
141118

142119
// Check for outdated packages
143120
if ($isVerbose) {
144-
$currentDir = getcwd();
145-
chdir($tailwindPath);
146-
try {
147-
$outdated = $this->shell->execute('npm outdated --json');
148-
if ($outdated) {
149-
$io->warning('Outdated packages found:');
150-
$io->writeln($outdated);
121+
$this->checkOutdatedPackages($tailwindPath, $io);
122+
}
123+
124+
return true;
125+
}
126+
127+
/**
128+
* Install Node modules in the specified path
129+
*/
130+
private function installNodeModules(string $tailwindPath, SymfonyStyle $io, bool $isVerbose): bool
131+
{
132+
if ($isVerbose) {
133+
$io->warning('Node modules not found in tailwind directory. Installing npm dependencies ...');
134+
}
135+
136+
$currentDir = getcwd();
137+
chdir($tailwindPath);
138+
139+
try {
140+
if ($this->fileDriver->isExists($tailwindPath . '/package-lock.json')) {
141+
$this->shell->execute('npm ci --quiet');
142+
} else {
143+
if ($isVerbose) {
144+
$io->warning('No package-lock.json found, running npm install...');
151145
}
152-
} catch (\Exception $e) {
153-
// Ignore errors from npm outdated as it returns non-zero when packages are outdated
146+
$this->shell->execute('npm install --quiet');
147+
}
148+
if ($isVerbose) {
149+
$io->success('Node modules installed successfully.');
154150
}
155151
chdir($currentDir);
152+
return true;
153+
} catch (\Exception $e) {
154+
$io->error('Failed to install node modules: ' . $e->getMessage());
155+
chdir($currentDir);
156+
return false;
156157
}
158+
}
157159

158-
return true;
160+
/**
161+
* Check for outdated packages and report them
162+
*/
163+
private function checkOutdatedPackages(string $tailwindPath, SymfonyStyle $io): void
164+
{
165+
$currentDir = getcwd();
166+
chdir($tailwindPath);
167+
try {
168+
$outdated = $this->shell->execute('npm outdated --json');
169+
if ($outdated) {
170+
$io->warning('Outdated packages found:');
171+
$io->writeln($outdated);
172+
}
173+
} catch (\Exception $e) {
174+
// Ignore errors from npm outdated as it returns non-zero when packages are outdated
175+
}
176+
chdir($currentDir);
159177
}
160178

161179
public function getName(): string

0 commit comments

Comments
 (0)