Completed
Push — master ( 713b15...55b21a )
by Thomas
03:46
created

CodeGenerator   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 75%

Importance

Changes 8
Bugs 1 Features 1
Metric Value
wmc 8
c 8
b 1
f 1
lcom 1
cbo 4
dl 0
loc 63
ccs 15
cts 20
cp 0.75
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 4 1
A getConfig() 0 3 1
A getGeneratorStrategy() 0 3 1
A __construct() 0 11 3
A generate() 0 7 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 1
		} 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
		}
73
74 16
		return $this->strategy->generate($model);
75
	}
76
77
}
78