ReplaceText   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 17
dl 0
loc 30
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handle() 0 18 4
1
<?php
2
3
namespace HotRodCli\Jobs\Module;
4
5
use Symfony\Component\Finder\Finder;
6
use Symfony\Component\Filesystem\Filesystem;
7
use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
8
9
class ReplaceText
10
{
11
    protected $finder;
12
13
    protected $fileSystem;
14
15
    public function __construct(Finder $finder, Filesystem $filesystem)
16
    {
17
        $this->finder = $finder;
18
        $this->fileSystem = $filesystem;
19
    }
20
21
    public function handle(string $needle, string $value, string $dir)
22
    {
23
        if (!$this->fileSystem->exists($dir)) {
24
            throw new \Exception('There is no such directory');
25
        }
26
27
        $files = $this->finder
28
            ->files()
29
            ->in($dir)
30
            ->contains($needle);
31
32
        foreach ($files as $file) {
33
            $updated = str_replace($needle, $value, $file->getContents());
34
35
            try {
36
                $this->fileSystem->dumpFile($file->getRealPath(), $updated);
37
            } catch (IOExceptionInterface $exception) {
38
                throw new \Exception($exception->getMessage());
39
            }
40
        }
41
    }
42
}
43