Completed
Push — master ( 4d90dd...0c32ab )
by Sebastian
03:54
created

Filesystem::createBuilderClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
namespace Spatie\SchemaOrg\Generator\Writer;
4
5
use League\Flysystem\Adapter\Local;
6
use Spatie\SchemaOrg\Generator\Type;
7
use League\Flysystem\Filesystem as Flysystem;
8
use Spatie\SchemaOrg\Generator\TypeCollection;
9
10
class Filesystem
11
{
12
    /** @var \League\Flysystem\Filesystem */
13
    protected $flysystem;
14
15
    /** @var \Spatie\SchemaOrg\Generator\Writer\Template */
16
    protected $typeTemplate;
17
18
    public function __construct(string $root)
19
    {
20
        $adapter = new Local($root);
21
        $this->flysystem = new Flysystem($adapter);
22
23
        $this->typeTemplate = new Template('Type.php.twig');
24
        $this->builderClassTemplate = new Template('Schema.php.twig');
0 ignored issues
show
Bug introduced by
The property builderClassTemplate does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
25
    }
26
27
    public function clear()
28
    {
29
        $this->flysystem->deleteDir('src');
30
        $this->flysystem->createDir('src');
31
    }
32
33
    public function cloneStaticFiles()
34
    {
35
        $files = $this->flysystem->listContents('generator/templates/static', true);
36
37
        foreach ($files as $file) {
38
            if ($file['type'] !== 'file') {
39
                continue;
40
            }
41
42
            $this->flysystem->put(
43
                str_replace('generator/templates/static', 'src', $file['path']),
44
                $this->flysystem->read($file['path'])
0 ignored issues
show
Security Bug introduced by
It seems like $this->flysystem->read($file['path']) targeting League\Flysystem\Filesystem::read() can also be of type false; however, League\Flysystem\Filesystem::put() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
45
            );
46
        }
47
    }
48
49
    public function createType(Type $type)
50
    {
51
        $this->flysystem->put(
52
            "src/{$type->name}.php",
53
            $this->typeTemplate->render(['type' => $type])
54
        );
55
    }
56
57
    public function createBuilderClass(TypeCollection $types)
58
    {
59
        $this->flysystem->put(
60
            'src/Schema.php',
61
            $this->builderClassTemplate->render(['types' => $types->toArray()])
62
        );
63
    }
64
}
65