Skip to content

Commit 610536a

Browse files
committed
Merge branch 'refactor'
2 parents 716b179 + d0bc6a9 commit 610536a

File tree

6 files changed

+269
-84
lines changed

6 files changed

+269
-84
lines changed

src/CLI/Commands/AttachmentCommand.php

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use TypistTech\ImageOptimizeCommand\Operations\AttachmentImages\Backup;
1111
use TypistTech\ImageOptimizeCommand\Operations\AttachmentImages\Optimize;
1212
use TypistTech\ImageOptimizeCommand\Operations\Backup as BaseBackup;
13+
use TypistTech\ImageOptimizeCommand\Operations\Optimize as BaseOptimize;
1314
use TypistTech\ImageOptimizeCommand\OptimizerChainFactory;
1415
use TypistTech\ImageOptimizeCommand\Repositories\AttachmentRepository;
1516

@@ -41,12 +42,13 @@ public function __invoke($args, $_assocArgs): void
4142
$ids = array_filter($ids);
4243

4344
$repo = new AttachmentRepository();
45+
$fileSystem = new Filesystem();
4446
$logger = LoggerFactory::create();
4547

46-
$backupOperation = $this->createBackupOperation($repo, $logger);
47-
$backupOperation->execute(...$ids);
48+
$backupOperation = $this->createBackupOperation($repo, $fileSystem, $logger);
49+
$optimizeOperation = $this->createOptimizeOperation($repo, $fileSystem, $logger);
4850

49-
$optimizeOperation = $this->createOptimizeOperation($repo, $logger);
51+
$backupOperation->execute(...$ids);
5052
$optimizeOperation->execute(...$ids);
5153
}
5254

@@ -55,10 +57,12 @@ public function __invoke($args, $_assocArgs): void
5557
*
5658
* @return Backup
5759
*/
58-
protected function createBackupOperation(AttachmentRepository $repo, LoggerInterface $logger): Backup
59-
{
60-
$fileSystem = new Filesystem();
61-
$baseBackup = new BaseBackup($fileSystem, $logger);
60+
protected function createBackupOperation(
61+
AttachmentRepository $repo,
62+
Filesystem $filesystem,
63+
LoggerInterface $logger
64+
): Backup {
65+
$baseBackup = new BaseBackup($filesystem, $logger);
6266

6367
return new Backup($repo, $baseBackup, $logger);
6468
}
@@ -68,10 +72,14 @@ protected function createBackupOperation(AttachmentRepository $repo, LoggerInter
6872
*
6973
* @return Optimize
7074
*/
71-
protected function createOptimizeOperation(AttachmentRepository $repo, LoggerInterface $logger): Optimize
72-
{
75+
protected function createOptimizeOperation(
76+
AttachmentRepository $repo,
77+
Filesystem $filesystem,
78+
LoggerInterface $logger
79+
): Optimize {
7380
$optimizerChain = OptimizerChainFactory::create();
81+
$baseOptimize = new BaseOptimize($optimizerChain, $filesystem, $logger);
7482

75-
return new Optimize($repo, $optimizerChain, $logger);
83+
return new Optimize($repo, $baseOptimize, $logger);
7684
}
7785
}

src/Operations/AttachmentImages/Optimize.php

Lines changed: 31 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44

55
namespace TypistTech\ImageOptimizeCommand\Operations\AttachmentImages;
66

7-
use Spatie\ImageOptimizer\OptimizerChain;
8-
use Symfony\Component\Filesystem\Exception\IOException;
97
use TypistTech\ImageOptimizeCommand\LoggerInterface;
8+
use TypistTech\ImageOptimizeCommand\Operations\Optimize as BaseOptimize;
109
use TypistTech\ImageOptimizeCommand\Repositories\AttachmentRepository;
11-
use function WP_CLI\Utils\normalize_path;
1210

1311
class Optimize
1412
{
@@ -27,120 +25,85 @@ class Optimize
2725
protected $repo;
2826

2927
/**
30-
* The optimizer chain.
28+
* The optimizer operation.
3129
*
32-
* @var OptimizerChain
30+
* @var BaseOptimize
3331
*/
34-
protected $optimizerChain;
32+
protected $optimize;
3533

3634
/**
3735
* Optimize constructor.
3836
*
39-
* @param AttachmentRepository $repo The repo.
40-
* @param OptimizerChain $optimizerChain The optimizer chain.
41-
* @param LoggerInterface $logger The logger.
37+
* @param AttachmentRepository $repo The repo.
38+
* @param BaseOptimize $optimize The optimizer operation.
39+
* @param LoggerInterface $logger The logger.
4240
*/
43-
public function __construct(AttachmentRepository $repo, OptimizerChain $optimizerChain, LoggerInterface $logger)
41+
public function __construct(AttachmentRepository $repo, BaseOptimize $optimize, LoggerInterface $logger)
4442
{
4543
$this->repo = $repo;
46-
$this->optimizerChain = $optimizerChain;
44+
$this->optimize = $optimize;
4745
$this->logger = $logger;
4846
}
4947

5048
/**
5149
* Optimize images of attachments.
5250
*
53-
* @param int ...$ids The attachment IDs.
51+
* @param int|int[] ...$ids The attachment IDs.
5452
*
5553
* @return void
5654
*/
5755
public function execute(int ...$ids): void
5856
{
5957
$this->logger->section('Optimizing images for ' . count($ids) . ' attachment(s)');
6058

61-
$ids = array_filter($ids, function (int $id): bool {
59+
$nonOptimizedIds = array_filter($ids, function (int $id): bool {
6260
// phpcs:ignore
63-
$isOptimized = $this->repo->isOptimized($id);
64-
65-
if ($isOptimized) {
66-
// phpcs:ignore
67-
$this->logger->warning('Skip: Attachment already optimized - ID: ' . $id);
68-
}
69-
70-
return ! $isOptimized;
61+
return ! $this->isOptimized($id);
7162
});
72-
$ids = array_filter($ids);
7363

7464
array_map(function (int $id): void {
7565
// phpcs:ignore
7666
$this->optimizeAttachment($id);
77-
}, $ids);
67+
}, $nonOptimizedIds);
7868

7969
$this->logger->info('Finished');
8070
}
8171

8272
/**
83-
* Optimize all images of an attachment.
73+
* Whether the attachment is optimized.
74+
* Log warning if already optimized.
8475
*
8576
* @param int $id The attachment ID.
8677
*
87-
* @return void
78+
* @return bool
8879
*/
89-
protected function optimizeAttachment(int $id): void
80+
protected function isOptimized(int $id): bool
9081
{
91-
$this->logger->debug('Optimizing images of attachment ID: ' . $id);
92-
93-
$paths = $this->repo->getPaths($id);
94-
95-
$normalizedPaths = array_map(function (string $path): string {
96-
return normalize_path($path);
97-
}, $paths);
98-
99-
$results = array_map(function (string $imagePath): bool {
82+
if ($this->repo->isOptimized($id)) {
10083
// phpcs:ignore
101-
return $this->optimizeImage($imagePath);
102-
}, $normalizedPaths);
84+
$this->logger->warning('Skip: Attachment already optimized - ID: ' . $id);
10385

104-
if (in_array(true, $results, true)) {
105-
$this->logger->debug('Marking attachment ID: ' . $id . ' as optimized.');
106-
$this->repo->markAsOptimized($id);
107-
$this->logger->notice('Optimized images of attachment ID: ' . $id);
86+
return true;
10887
}
88+
89+
return false;
10990
}
11091

11192
/**
112-
* Optimize an image.
93+
* Optimize all images of an attachment.
11394
*
114-
* @param string $path Path to the image.
95+
* @param int $id The attachment ID.
11596
*
116-
* @return bool
97+
* @return void
11798
*/
118-
protected function optimizeImage(string $path): bool
99+
protected function optimizeAttachment(int $id): void
119100
{
120-
try {
121-
$this->logger->debug('Optimizing image - ' . $path);
122-
123-
if (! is_readable($path)) {
124-
$this->logger->error('Image not readable - ' . $path);
125-
126-
return false;
127-
}
128-
129-
// phpcs:ignore WordPress.VIP.FileSystemWritesDisallow.file_ops_is_writable
130-
if (! is_writable($path)) {
131-
$this->logger->error('Image not writable - ' . $path);
101+
$this->logger->section('Optimizing images of attachment ID: ' . $id);
132102

133-
return false;
134-
}
135-
136-
$this->optimizerChain->optimize($path);
137-
138-
return true;
139-
// phpcs:ignore
140-
} catch (IOException $exception) {
141-
$this->logger->error('Failed to optimize ' . $path);
103+
$paths = $this->repo->getPaths($id);
104+
$this->optimize->execute(...$paths);
142105

143-
return false;
144-
}
106+
$this->repo->markAsOptimized($id);
107+
$this->logger->info('Marked attachment ID: ' . $id . ' as optimized.');
145108
}
146109
}

src/Operations/AttachmentImages/Restore.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@
1313

1414
class Restore
1515
{
16-
protected const SUCCESS = 0;
17-
protected const ERROR = 1;
18-
1916
/**
2017
* The logger.
2118
*

src/Operations/Backup.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@
1212

1313
class Backup
1414
{
15-
protected const SUCCESS = 0;
16-
protected const ERROR = 1;
17-
protected const SKIP = 2;
1815
public const ORIGINAL_EXTENSION = '.original';
1916

2017
/**

src/Operations/Optimize.php

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TypistTech\ImageOptimizeCommand\Operations;
6+
7+
use Spatie\ImageOptimizer\OptimizerChain;
8+
use Symfony\Component\Filesystem\Exception\IOException;
9+
use Symfony\Component\Filesystem\Filesystem;
10+
use TypistTech\ImageOptimizeCommand\LoggerInterface;
11+
use function WP_CLI\Utils\normalize_path;
12+
13+
class Optimize
14+
{
15+
/**
16+
* The optimizer chain.
17+
*
18+
* @var OptimizerChain
19+
*/
20+
protected $optimizerChain;
21+
22+
/**
23+
* The filesystem.
24+
*
25+
* @var Filesystem
26+
*/
27+
protected $filesystem;
28+
29+
/**
30+
* The logger.
31+
*
32+
* @var LoggerInterface
33+
*/
34+
protected $logger;
35+
36+
/**
37+
* Optimize constructor.
38+
*
39+
* @param OptimizerChain $optimizerChain The optimizer chain.
40+
* @param Filesystem $filesystem The file system.
41+
* @param LoggerInterface $logger The logger.
42+
*/
43+
public function __construct(OptimizerChain $optimizerChain, Filesystem $filesystem, LoggerInterface $logger)
44+
{
45+
$this->optimizerChain = $optimizerChain;
46+
$this->filesystem = $filesystem;
47+
$this->logger = $logger;
48+
}
49+
50+
/**
51+
* Optimize the files.
52+
*
53+
* @param string ...$paths Paths to the files.
54+
*/
55+
public function execute(string ...$paths): void
56+
{
57+
$total = count($paths);
58+
$this->logger->section('Optimizing ' . $total . ' file(s)');
59+
60+
$normalizedPaths = array_map(function (string $path): string {
61+
return normalize_path($path);
62+
}, $paths);
63+
64+
$validPaths = array_filter($normalizedPaths, function (string $path): bool {
65+
// phpcs:ignore
66+
return $this->isValid($path);
67+
});
68+
69+
array_map(function (string $path): void {
70+
// phpcs:ignore
71+
$this->logger->debug('Optimizing image - ' . $path);
72+
// phpcs:ignore
73+
$this->optimizerChain->optimize($path);
74+
}, $validPaths);
75+
76+
$this->logger->info('Finished');
77+
}
78+
79+
/**
80+
* Whether the file exists and readable and writable.
81+
*
82+
* @param string $path Path to the file.
83+
*
84+
* @return bool
85+
*/
86+
protected function isValid(string $path): bool
87+
{
88+
try {
89+
$this->logger->debug('Checking image - ' . $path);
90+
91+
$isExists = $this->filesystem->exists($path);
92+
if (! $isExists) {
93+
$this->logger->error('Image not exist - ' . $path);
94+
95+
return false;
96+
}
97+
98+
if (! is_readable($path)) {
99+
$this->logger->error('Image not readable - ' . $path);
100+
101+
return false;
102+
}
103+
104+
// phpcs:ignore WordPress.VIP.FileSystemWritesDisallow.file_ops_is_writable
105+
if (! is_writable($path)) {
106+
$this->logger->error('Image not writable - ' . $path);
107+
108+
return false;
109+
}
110+
111+
return true;
112+
// phpcs:ignore
113+
} catch (IOException $exception) {
114+
$this->logger->error('Unable to optimize ' . $path);
115+
116+
return false;
117+
}
118+
}
119+
}

0 commit comments

Comments
 (0)