Completed
Pull Request — master (#30)
by
unknown
05:11
created

CodeGenerator::generate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
namespace gossi\codegen\generator;
3
4
use gossi\codegen\config\CodeGeneratorConfig;
5
use gossi\codegen\model\GenerateableInterface;
6
use gossi\codegen\visitor\GeneratorVisitor;
7
8
/**
9
 * Code generator
10
 *
11
 * Generates code for any generateable model
12
 *
13
 * @author Thomas Gossmann
14
 */
15
class CodeGenerator {
16
17
	protected $config;
18
19
	/**
20
	 * @var GeneratorStrategy
21
	 */
22
	protected $strategy;
23
24
	/**
25
	 *
26
	 * @param CodeGeneratorConfig|array $config
27
	 */
28 10
	public function __construct($config = null) {
29 10
		if (is_array($config)) {
30 9
			$this->config = new CodeGeneratorConfig($config);
31 10
		} else if ($config instanceof CodeGeneratorConfig) {
32
			$this->config = $config;
33
		} else {
34 1
			$this->config = new CodeGeneratorConfig();
35
		}
36
37 10
		$this->init();
38 10
	}
39
40 16
	protected function init() {
41 16
		$visitor = new GeneratorVisitor($this->config);
42 16
		$this->strategy = new GeneratorStrategy($visitor);
43 16
	}
44
45
	/**
46
	 * Returns the used configuration
47
	 *
48
	 * @return CodeGeneratorConfig
49
	 */
50
	public function getConfig() {
51
		return $this->config;
52
	}
53
54
	/**
55
	 * Returns the used generator strategy
56
	 *
57
	 * @return DefaultGeneratorStrategy
58
	 */
59
	public function getGeneratorStrategy() {
60
		return $this->strategy;
61
	}
62
63
	/**
64
	 * Generates code from a given model
65
	 *
66
	 * @param GenerateableInterface $model
67
	 * @return string the generated code
68
	 */
69 16
	public function generate(GenerateableInterface $model) {
70 16
		if ($this->config->getGenerateDocblock()) {
71 3
			$model->generateDocblock();
72 3
		}
73
74 16
		return $this->strategy->generate($model);
75
	}
76
77
}
78