Skip to content

Commit fa87212

Browse files
committed
Add ProcessFactory::createRemoveFileProcess()
1 parent dfd713c commit fa87212

File tree

3 files changed

+94
-7
lines changed

3 files changed

+94
-7
lines changed

changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
### master
22

3+
- Add `ProcessFactory::createRemoveFileProcess()`
4+
35
### [0.4.0](../../compare/0.3.0...0.4.0) - 2022-02-14
46

57
- Add `ParallelProcessesApplication::hasProcess()`

src/Process/ProcessFactory.php

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,53 @@
55
namespace Steevanb\ParallelProcess\Process;
66

77
use Steevanb\ParallelProcess\Console\Application\ParallelProcessesApplication;
8+
use steevanb\PhpTypedArray\ScalarArray\StringArray;
89

910
class ProcessFactory
1011
{
1112
public static function createRemoveDirectoryProcess(
12-
string $directory,
13+
string $path,
1314
ParallelProcessesApplication $application,
1415
string $name = null,
1516
string $cwd = null
1617
): ?Process {
17-
if (is_dir($directory)) {
18-
$return = new Process(['rm', '-rf', $directory], $cwd);
19-
if (is_string($name)) {
20-
$return->setName($name);
21-
}
18+
if (is_dir($path)) {
19+
$return = static::createProcess(new StringArray(['rm', '-rf', $path]), $application, $name, $cwd);
20+
} else {
21+
$return = null;
22+
}
23+
24+
return $return;
25+
}
2226

23-
$application->addProcess($return);
27+
public static function createRemoveFileProcess(
28+
string $filePathname,
29+
ParallelProcessesApplication $application,
30+
string $name = null,
31+
string $cwd = null
32+
): ?Process {
33+
if (is_file($filePathname)) {
34+
$return = static::createProcess(new StringArray(['rm', $filePathname]), $application, $name, $cwd);
2435
} else {
2536
$return = null;
2637
}
2738

2839
return $return;
2940
}
41+
42+
protected static function createProcess(
43+
StringArray $command,
44+
ParallelProcessesApplication $application,
45+
string $name = null,
46+
string $cwd = null
47+
): Process {
48+
$return = new Process($command->toArray(), $cwd);
49+
if (is_string($name)) {
50+
$return->setName($name);
51+
}
52+
53+
$application->addProcess($return);
54+
55+
return $return;
56+
}
3057
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Steevanb\ParallelProcess\Tests\Process\ProcessFactory;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Steevanb\ParallelProcess\{
9+
Console\Application\ParallelProcessesApplication,
10+
Process\Process,
11+
Process\ProcessFactory
12+
};
13+
14+
/** @covers \Steevanb\ParallelProcess\Process\ProcessFactory::createRemoveFileProcess */
15+
class CreateRemoveFileProcessTest extends TestCase
16+
{
17+
public function testFileExists(): void
18+
{
19+
$application = new ParallelProcessesApplication();
20+
21+
/** @var Process $process */
22+
$process = ProcessFactory::createRemoveFileProcess(__FILE__, $application);
23+
24+
static::assertInstanceOf(Process::class, $process);
25+
static::assertTrue($application->hasProcess($process));
26+
}
27+
28+
public function testFileNotFound(): void
29+
{
30+
$application = new ParallelProcessesApplication();
31+
32+
$process = ProcessFactory::createRemoveFileProcess(__FILE__ . '.foo.bar', $application);
33+
34+
static::assertNull($process);
35+
}
36+
37+
public function testName(): void
38+
{
39+
$application = new ParallelProcessesApplication();
40+
41+
/** @var Process $process */
42+
$process = ProcessFactory::createRemoveFileProcess(__FILE__, $application, 'foo');
43+
44+
static::assertInstanceOf(Process::class, $process);
45+
static::assertSame('foo', $process->getName());
46+
}
47+
48+
public function testCwd(): void
49+
{
50+
$application = new ParallelProcessesApplication();
51+
52+
/** @var Process $process */
53+
$process = ProcessFactory::createRemoveFileProcess(__FILE__, $application, 'foo', '/foo/bar');
54+
55+
static::assertInstanceOf(Process::class, $process);
56+
static::assertSame('/foo/bar', $process->getWorkingDirectory());
57+
}
58+
}

0 commit comments

Comments
 (0)