Project::getPackage()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 0
cp 0
cc 3
eloc 6
nc 3
nop 0
crap 12
1
<?php
2
namespace keeko\tools\model;
3
4
use keeko\framework\schema\GeneratorDefinitionSchema;
5
use keeko\framework\schema\PackageSchema;
6
use phootwork\file\File;
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 GeneratorDefinitionSchema */
17 20
	private $generatorDefinition;
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 getGeneratorDefinitionFileName() {
80
		return $this->root . '/generator.json';
81
	}
82
83
	/**
84
	 * @return boolean
85
	 */
86
	public function hasGeneratorDefinitionFile() {
87
		$file = new File($this->getGeneratorDefinitionFileName());
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 generator definition from 'generator.json' or creates a blank one, if
108
	 * the file doesn't exist
109
	 *
110
	 * @throws JsonEmptyException
111
	 * @throws \RuntimeException
112
	 * @return GeneratorDefinitionSchema
113
	 */
114
	public function getGeneratorDefinition() {
115
		if ($this->generatorDefinition === null) {
116
			$this->generatorDefinition = $this->hasGeneratorDefinitionFile()
117
				? GeneratorDefinitionSchema::fromFile($this->getGeneratorDefinitionFileName())
118
				: new GeneratorDefinitionSchema();
119
		}
120
121
		return $this->generatorDefinition;
122
	}
123
}
124