Skip to content

Commit cdbb244

Browse files
committed
refactor
1 parent 29866e4 commit cdbb244

File tree

11 files changed

+190
-104
lines changed

11 files changed

+190
-104
lines changed

src/OpenMage/ComposerPlugin/Copy/AbstractCopyPlugin.php

Lines changed: 84 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Composer\Package\BasePackage;
1515
use Composer\Script\Event;
16+
use ErrorException;
1617
use Exception;
1718
use Symfony\Component\Filesystem\Exception\IOException;
1819
use Symfony\Component\Filesystem\Filesystem;
@@ -28,21 +29,24 @@ abstract class AbstractCopyPlugin implements CopyInterface
2829
*
2930
* @var BasePackage[]
3031
*/
31-
public array $installedComposerPackages = [];
32+
public array $composerPackages = [];
3233

3334
/**
3435
* Packages installad via Unpkg downloadd
3536
*
3637
* @var array<string, array<string, string>>
3738
*/
38-
public array $installedUnpkgPackages = [];
39+
public array $unpkgPackages = [];
3940

4041
/**
4142
* Composer event
4243
*/
43-
protected Event $event;
44+
protected ?Event $event;
4445

45-
public function __construct(Event $event)
46+
/**
47+
* @codeCoverageIgnore
48+
*/
49+
public function __construct(?Event $event)
4650
{
4751
$this->event = $event;
4852
}
@@ -54,12 +58,8 @@ public function __construct(Event $event)
5458
*/
5559
public function processComposerInstall(): void
5660
{
57-
if (!$this instanceof CopyFromComposerInterface) {
58-
return;
59-
}
60-
6161
$package = $this->getComposerPackage();
62-
if (!$package) {
62+
if (!$package || !$this instanceof CopyFromComposerInterface) {
6363
return;
6464
}
6565

@@ -70,11 +70,12 @@ public function processComposerInstall(): void
7070
$this->getComposerSource(),
7171
);
7272

73-
$filesystem = new Filesystem();
73+
$event = $this->getEvent();
74+
$filesystem = $this->getFileSystem();
7475

7576
if (!$filesystem->exists($copySourcePath) && $this instanceof CopyFromUnpkgInterface) {
76-
if ($this->event->getIO()->isVerbose()) {
77-
$this->event->getIO()->write(sprintf(
77+
if ($event && $event->getIO()->isVerbose()) {
78+
$event->getIO()->write(sprintf(
7879
'Fallback to Unpkg %s for %s',
7980
$this->getUnpkgName(),
8081
$this->getComposerName(),
@@ -95,11 +96,13 @@ public function processComposerInstall(): void
9596

9697
try {
9798
$filesystem->copy($copySource, $copytarget);
98-
if ($this->event->getIO()->isVeryVerbose()) {
99-
$this->event->getIO()->write(sprintf('Copy %s to %s', $copySource, $copytarget));
99+
if ($event && $event->getIO()->isVeryVerbose()) {
100+
$event->getIO()->write(sprintf('Copy %s to %s', $copySource, $copytarget));
101+
}
102+
} catch (IOException $exception) {
103+
if ($event) {
104+
$event->getIO()->write($exception->getMessage());
100105
}
101-
} catch (IOException $IOException) {
102-
$this->event->getIO()->write($IOException->getMessage());
103106
}
104107
}
105108
}
@@ -109,18 +112,15 @@ public function processComposerInstall(): void
109112
*/
110113
public function processUnpkgInstall(): void
111114
{
112-
if (!$this instanceof CopyFromUnpkgInterface) {
113-
return;
114-
}
115-
116-
if (!$this->getUnpkgVersion()) {
115+
if (!$this instanceof CopyFromUnpkgInterface || !$this->getUnpkgVersion()) {
117116
return;
118117
}
119118

119+
$event = $this->getEvent();
120120
$sourcePath = $this->getUnpkSourcePath();
121121

122-
if ($this->event->getIO()->isVerbose()) {
123-
$this->event->getIO()->write(sprintf(
122+
if ($event && $event->getIO()->isVerbose()) {
123+
$event->getIO()->write(sprintf(
124124
'Trying to download %s %s from %s',
125125
$this->getUnpkgName(),
126126
$this->getUnpkgVersion(),
@@ -132,57 +132,69 @@ public function processUnpkgInstall(): void
132132
$sourceFilePath = $sourcePath . $fileName;
133133
try {
134134
$content = file_get_contents($sourceFilePath);
135-
} catch (\ErrorException $errorException) {
136-
$this->event->getIO()->write($errorException->getMessage());
135+
} catch (ErrorException $errorException) {
136+
if ($event) {
137+
$event->getIO()->write($errorException->getMessage());
138+
}
137139
return;
138140
}
139141

140142
if (!$content) {
141-
$this->event->getIO()->write(sprintf('Could not read from %s', $sourceFilePath));
143+
if ($event) {
144+
$event->getIO()->write(sprintf('Could not read from %s', $sourceFilePath));
145+
}
142146
return;
143147
}
144148

145149
try {
146-
$filesystem = new Filesystem();
147150
$targetFilePath = $this->getCopyTargetPath() . '/' . $fileName;
148-
$filesystem->dumpFile($targetFilePath, $content);
149-
if ($this->event->getIO()->isVerbose()) {
150-
$this->event->getIO()->write(sprintf('Added %s', $targetFilePath));
151+
$this->getFileSystem()->dumpFile($targetFilePath, $content);
152+
if ($event && $event->getIO()->isVerbose()) {
153+
$event->getIO()->write(sprintf('Added %s', $targetFilePath));
154+
}
155+
} catch (IOException $exception) {
156+
if ($event) {
157+
$event->getIO()->write($exception->getMessage());
151158
}
152-
} catch (IOException $IOException) {
153-
$this->event->getIO()->write($IOException->getMessage());
154159
return;
155160
}
156161
}
157162
}
158163

159164
public function getComposerPackage(): ?BasePackage
160165
{
161-
if ($this instanceof CopyFromComposerInterface) {
162-
$vendorName = $this->getComposerName();
163-
$module = $this->getInstalledComposerPackage($vendorName);
164-
if ($module) {
165-
return $module;
166-
}
166+
if (!$this instanceof CopyFromComposerInterface) {
167+
return null;
168+
}
169+
170+
$vendorName = $this->getComposerName();
171+
$module = $this->getInstalledComposerPackage($vendorName);
172+
if ($module) {
173+
return $module;
174+
}
175+
176+
$event = $this->getEvent();
177+
if (!$event) {
178+
return null;
179+
}
167180

168-
$locker = $this->event->getComposer()->getLocker();
169-
$repo = $locker->getLockedRepository();
181+
$locker = $event->getComposer()->getLocker();
182+
$repo = $locker->getLockedRepository();
170183

171-
foreach ($repo->getPackages() as $package) {
172-
if ($package->getName() === $vendorName) {
173-
$this->setInstalledComposerPackage($vendorName, $package);
174-
if ($this->event->getIO()->isVerbose()) {
175-
$this->event->getIO()->write(sprintf('%s found with version %s', $vendorName, $package->getVersion()));
176-
}
177-
return $this->getInstalledComposerPackage($vendorName);
184+
foreach ($repo->getPackages() as $package) {
185+
if ($package->getName() === $vendorName) {
186+
$this->setInstalledComposerPackage($vendorName, $package);
187+
if ($event->getIO()->isVerbose()) {
188+
$event->getIO()->write(sprintf('%s found with version %s', $vendorName, $package->getVersion()));
178189
}
190+
return $this->getInstalledComposerPackage($vendorName);
179191
}
180192
}
181193
return null;
182194
}
183195

184196
/**
185-
* Get path to NPM dist
197+
* Get path to Unpkg dist
186198
*/
187199
protected function getUnpkSourcePath(): string
188200
{
@@ -212,8 +224,13 @@ protected function getCwd(): string
212224
*/
213225
protected function getVendorDirectoryFromComposer(): string
214226
{
227+
$event = $this->getEvent();
228+
if (!$event) {
229+
return '';
230+
}
231+
215232
/** @var string $vendorDir */
216-
$vendorDir = $this->event->getComposer()->getConfig()->get(self::VENDOR_DIR);
233+
$vendorDir = $event->getComposer()->getConfig()->get(self::VENDOR_DIR);
217234
return $vendorDir;
218235
}
219236

@@ -222,7 +239,12 @@ protected function getVendorDirectoryFromComposer(): string
222239
*/
223240
protected function getMageRootDirectoryFromComposer(): string
224241
{
225-
$composerExtra = $this->event->getComposer()->getPackage()->getExtra();
242+
$event = $this->getEvent();
243+
if (!$event) {
244+
return '';
245+
}
246+
247+
$composerExtra = $event->getComposer()->getPackage()->getExtra();
226248
$magentoRootDir = '';
227249

228250
if (array_key_exists(self::EXTRA_MAGENTO_ROOT_DIR, $composerExtra) &&
@@ -245,11 +267,21 @@ private function getCopyTargetPath(): string
245267

246268
protected function getInstalledComposerPackage(string $vendorName): ?BasePackage
247269
{
248-
return $this->installedComposerPackages[$vendorName] ?? null;
270+
return $this->composerPackages[$vendorName] ?? null;
249271
}
250272

251273
private function setInstalledComposerPackage(string $vendorName, BasePackage $package): void
252274
{
253-
$this->installedComposerPackages[$vendorName] = $package;
275+
$this->composerPackages[$vendorName] = $package;
276+
}
277+
278+
public function getFileSystem(): Filesystem
279+
{
280+
return new Filesystem();
281+
}
282+
283+
public function getEvent(): ?Event
284+
{
285+
return $this->event;
254286
}
255287
}

src/OpenMage/ComposerPlugin/Copy/CopyFromComposerInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
namespace OpenMage\ComposerPlugin\Copy;
1313

1414
/**
15-
* PluginInterface
15+
* CopyFromComposerInterface
1616
*/
1717
interface CopyFromComposerInterface
1818
{
1919
/**
20-
* Npm name
20+
* Composer name
2121
*/
2222
public function getComposerName(): string;
2323

src/OpenMage/ComposerPlugin/Copy/CopyFromUnpkgInterface.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,30 @@
1212
namespace OpenMage\ComposerPlugin\Copy;
1313

1414
/**
15-
* PluginInterface
15+
* CopyFromUnpkgInterface
1616
*/
1717
interface CopyFromUnpkgInterface
1818
{
1919
public const UNPKG_URL = 'https://unpkg.com/{{package}}@{{version}}/';
2020

2121
/**
22-
* Npm name
22+
* Unpkg name
2323
*/
2424
public function getUnpkgName(): string;
2525

26+
/**
27+
* Unpkg version
28+
*/
2629
public function getUnpkgVersion(): string;
2730

31+
/**
32+
* Unpkg source
33+
*/
2834
public function getUnpkgSource(): string;
2935

3036
/**
37+
* Unpkg files
38+
*
3139
* @return string[]
3240
*/
3341
public function getUnpkgFiles(): array;

src/OpenMage/ComposerPlugin/Copy/CopyInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace OpenMage\ComposerPlugin\Copy;
1313

1414
/**
15-
* PluginInterface
15+
* CopyInterface
1616
*/
1717
interface CopyInterface
1818
{

src/OpenMage/ComposerPlugin/Copy/Plugins/ChartJs.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ public function getUnpkgName(): string
2424
return 'chart.js';
2525
}
2626

27+
/**
28+
* @SuppressWarnings("PHPMD.StaticAccess")
29+
*/
2730
public function getUnpkgVersion(): string
2831
{
2932
/** @var string $version */

src/OpenMage/ComposerPlugin/Copy/Plugins/JQuery.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function getComposerSource(): string
3030

3131
public function getComposerFiles(): array
3232
{
33-
return ['*.map', '*.js'];
33+
return ['*.js', '*.map'];
3434
}
3535

3636
public function getCopyTarget(): string

0 commit comments

Comments
 (0)