Completed
Push — master ( 966759...8da8ca )
by Thomas
07:52
created

Project::getCodegen()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 9
ccs 0
cts 0
cp 0
rs 9.6666
cc 3
eloc 6
nc 3
nop 0
crap 12
1
<?php
2
namespace keeko\tools\model;
3
4
use phootwork\file\File;
5
use keeko\framework\schema\PackageSchema;
6
use keeko\framework\schema\CodegenSchema;
7
8
class Project {
9 20
	
10 20
	/** @var string */
11 20
	private $root;
12
	
13 15
	/** @var PackageSchema */
14 15
	private $package;
15
	
16
	/** @var CodegenSchema */
17 20
	private $codegen;
18 20
	
19
	public function __construct($workdir) {
20
		$this->root = $workdir;
21
	}
22
	
23
	/**
24
	 * @return string
25
	 */
26
	public function getRootPath() {
27
		return $this->root;
28
	}
29
	
30
	/**
31
	 * @return string
32
	 */
33
	public function getComposerFileName() {
34
		return $this->root . '/composer.json'; 
35
	}
36
	
37
	/**
38
	 * 
39
	 * @return boolean
40
	 */
41
	public function hasComposerFile() {
42
		$file = new File($this->getComposerFileName());
43
		return $file->exists();
44
	}
45
46
	/**
47
	 * @return string
48
	 */
49
	public function getApiFileName() {
50
		return $this->root . '/api.json';
51
	}
52
	
53
	/**
54
	 * @return boolean
55
	 */
56
	public function hasApiFile() {
57
		$file = new File($this->getApiFileName());
58
		return $file->exists();
59
	}
60
	
61
	/**
62
	 * @return string
63
	 */
64
	public function getSchemaFileName() {
65
		return $this->root . '/res/database/schema.xml';
66
	}
67
	
68
	/**
69
	 * @return boolean
70
	 */
71
	public function hasSchemaFile() {
72
		$file = new File($this->getSchemaFileName());
73
		return $file->exists();
74
	}
75
	
76
	/**
77
	 * @return string
78
	 */
79
	public function getCodegenFileName() {
80
		return $this->root . '/codegen.json';
81
	}
82
	
83
	/**
84
	 * @return boolean
85
	 */
86
	public function hasCodegenFile() {
87
		$file = new File($this->getCodegenFileName());
88
		return $file->exists();
89
	}
90
91
	/**
92
	 * Returns the package from 'composer.json' or creates a blank one, if composer.json
93
	 * doesn't exist.
94
	 * 
95
	 * @return PackageSchema
96
	 */
97
	public function getPackage() {
98
		if ($this->package === null) {
99
			$this->package = $this->hasComposerFile()
100
				? PackageSchema::fromFile($this->getComposerFileName())
101
				: new PackageSchema();
102
		}
103
		return $this->package;
104
	}
105
	
106
	/**
107
	 * Returns a codegen schema from 'codegen.json' or creates a blank one, if 'codegen.json'
108
	 * doesn't exist.
109
	 *
110
	 * @throws JsonEmptyException
111
	 * @throws \RuntimeException
112
	 * @return CodegenSchema
113
	 */
114
	public function getCodegen() {
115
		if ($this->codegen === null) {
116
			$this->codegen = $this->hasCodegenFile()
117
				? CodegenSchema::fromFile($this->getCodegenFileName())
118
				: new CodegenSchema();
119
		}
120
	
121
		return $this->codegen;
122
	}
123
}
124