ProjectFilesystem::updateAllFilesOfType()   A
last analyzed

Complexity

Conditions 5
Paths 2

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 13
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 25
rs 9.5222
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Scriptura\QuickStart;
6
7
use League\Flysystem\Filesystem;
8
use League\Flysystem\Adapter\Local;
9
10
class ProjectFilesystem
11
{
12
    /**
13
     * @var \League\Flysystem\Filesystem
14
     */
15
    private $filesystem;
16
17
    public function __construct(string $directory)
18
    {
19
        $adapter = new Local($directory, LOCK_EX, Local::SKIP_LINKS);
20
        $this->filesystem = new Filesystem($adapter);
21
    }
22
23
    public function createFile(string $file, string $content)
24
    {
25
        $this->filesystem->put($file, $content);
26
    }
27
28
    public function updateFile(string $file, callable $callback)
29
    {
30
        if (!$this->filesystem->has($file)) {
31
            return;
32
        }
33
        $contents = $this->filesystem->read($file);
34
35
        $contents = $callback($contents);
36
37
        $this->filesystem->put($file, $contents);
38
    }
39
40
    public function updateAllFilesOfType(string $type, callable $callback)
41
    {
42
        $files = array_filter($this->filesystem->listContents('', true), function (array $file) use ($type) {
43
            $query = 'vendor/';
44
            if (strpos($file['path'], $query) === 0) {
45
                return false;
46
            }
47
48
            if (!isset($file['extension'])) {
49
                return false;
50
            }
51
52
            if ($file['extension'] !== $type) {
53
                return false;
54
            }
55
56
            return true;
57
        });
58
59
        foreach ($files as $file) {
60
            $contents = $this->filesystem->read($file['path']);
61
62
            $contents = $callback($contents);
63
64
            $this->filesystem->put($file['path'], $contents);
65
        }
66
    }
67
}
68