BaseGenerator::write()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 11
rs 9.4285
cc 3
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace Factory\Generators;
4
5
use League\Flysystem\Adapter\Local;
6
use League\Flysystem\Filesystem;
7
use Factory\Composer;
8
use Factory\Utils;
9
use Mustache_Engine;
10
11
abstract class BaseGenerator
12
{
13
    /**
14
     * @var \Factory\Composer
15
     */
16
    protected $composer;
17
    /**
18
     * @var Filesystem
19
     */
20
    private $internal;
21
    /**
22
     * @var Mustache_Engine
23
     */
24
    private $engine;
25
    /**
26
     * @var \League\Flysystem\Filesystem
27
     */
28
    protected $external;
29
30
    /**
31
     * @var array
32
     */
33
    protected $data;
34
35
    /**
36
     * @param mixed $data
37
     *
38
     * @return static
39
     */
40
    public function setData($data)
41
    {
42
        $fullClassName = $this->composer->getClassNamespace($data['name']);
43
44
        $data = array_merge([
45
            'namespace' => Utils::getJustNamespace($fullClassName),
46
            'className' => Utils::getJustClassName($fullClassName),
47
        ], $data);
48
49
        $this->data = $data;
50
        return $this;
51
    }
52
53
    /**
54
     * @var boolean
55
     */
56
    protected $force = false;
57
58
    /**
59
     * @param boolean $force
60
     *
61
     * @return static
62
     */
63
    public function setForce($force = false)
64
    {
65
        $this->force = $force;
66
        return $this;
67
    }
68
69
    public function __construct(Composer $composer, Filesystem $external = null, Mustache_Engine $engine = null)
70
    {
71
        $this->internal = new Filesystem(new Local(__DIR__ . '/../../stubs'));
72
        $this->external = $external ? $external : new Filesystem(new Local(getcwd()));
73
        $this->engine = $engine ? $engine : new Mustache_Engine;
74
        $this->composer = $composer;
75
    }
76
77
    public function prepare($template, $data = [])
78
    {
79
        $template = $this->internal->get($template)->read();
80
81
        return $this->engine->render($template, $data);
82
    }
83
84
    protected function write($data)
85
    {
86
        $destination = $this->composer->getClassPath($this->data['name']);
87
88
        if($this->force && $this->external->has($destination))
89
        {
90
            $this->external->delete($destination);
91
        }
92
93
        return $this->external->write($destination, $data);
94
    }
95
96
    public function generate($stub, $data)
97
    {
98
        $template = $this->prepare($stub, array_merge($this->data, $data));
99
100
        return $this->write($template);
101
    }
102
103
    abstract function make();
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
104
}
105