InPlaceSubstitution::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 4
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Cocotte\Shell\EnvironmentSubstitution;
4
5
use Cocotte\Console\Style;
6
use Cocotte\Filesystem\Filesystem;
7
use Cocotte\Finder\Finder;
8
use Cocotte\Shell\ProcessRunner;
9
use Symfony\Component\Finder\SplFileInfo;
10
use Symfony\Component\Process\Process;
11
12
final class InPlaceSubstitution implements SubstitutionStrategy
13
{
14
    /**
15
     * @var Finder
16
     */
17
    private $finder;
18
19
    /**
20
     * @var Style
21
     */
22
    private $style;
23
24
    /**
25
     * @var ProcessRunner
26
     */
27
    private $processRunner;
28
29
    /**
30
     * @var Filesystem
31
     */
32
    private $filesystem;
33
34
    public function __construct(
35
        Finder $finder,
36
        Style $style,
37
        ProcessRunner $processRunner,
38
        Filesystem $filesystem
39
    ) {
40
        $this->finder = $finder;
41
        $this->style = $style;
42
        $this->processRunner = $processRunner;
43
        $this->filesystem = $filesystem;
44
    }
45
46
    public function substitute(Process $envSubstProcess)
47
    {
48
        /** @var SplFileInfo $file */
49
        foreach ($this->finder->files() as $file) {
50
            $this->style->verbose('In-place substitution of environment in '.$file->getRealPath());
51
52
            $envSubstProcess->setInput($file->getContents());
53
            $this->processRunner->mustRun($envSubstProcess);
54
55
            $this->filesystem->dumpFile($file->getRealPath(), $envSubstProcess->getOutput());
56
        }
57
    }
58
}