Completed
Push — master ( 98cd2e...56265b )
by Thomas
09:01
created

PackageService::getNamespace()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 16
ccs 6
cts 6
cp 1
rs 9.2
cc 4
eloc 11
nc 5
nop 0
crap 4
1
<?php
2
namespace keeko\tools\services;
3
4
use keeko\framework\schema\PackageSchema;
5
use phootwork\file\exception\FileException;
6
use phootwork\file\File;
7
use keeko\tools\utils\NamespaceResolver;
8
9
class PackageService extends AbstractService {
10
11
	/** @var PackageSchema */
12
	private $package = null;
13
	private $keeko = null;
14
	private $module = null;
15
	private $actions = null;
0 ignored issues
show
Unused Code introduced by
The property $actions is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
16
	private $app = null;
17
	private $namespace = null;
18
	
19
	/**
20
	 *
21
	 * @throws FileException
22
	 * @return PackageSchema
23
	 */
24
	public function getPackage() {
25
		if ($this->package === null) {
26 20
			$file = new File($this->service->getProject()->getComposerFileName());
27 20
			if ($file->exists()) {
28 20
				$this->package = PackageSchema::fromFile($file->getPathname());
29 20
			} else {
30 17
				$this->package = new PackageSchema();
31 17
			}
32 3
		}
33
34 20
		return $this->package;
35
	}
36 20
	
37
	/**
38
	 * Returns the root namespace for this package
39
	 *
40
	 * @return string the namespace
41
	 */
42
	public function getNamespace() {
43
		if ($this->namespace === null) {
44 20
			$input = $this->io->getInput();
45 20
			$ns = $input->hasOption('namespace')
46 20
			? $input->getOption('namespace')
47 20
			: null;
48 20
			if ($ns === null) {
49
				$package = $this->service->getPackageService()->getPackage();
50 20
				$ns = NamespaceResolver::getNamespace('src', $package);
51
			}
52
	
53
			$this->namespace = trim($ns, '\\');
54
		}
55
	
56
		return $this->namespace;
57
	}
58 17
	
59 17
	/**
60 17
	 * Returns the keeko node from the composer.json extra
61 17
	 *
62 17
	 * @return KeekoSchema
63
	 */
64 17
	public function getKeeko() {
65
		if ($this->keeko === null) {
66
			$package = $this->getPackage();
67
			$this->keeko = $package->getKeeko();
68
		}
69
	
70
		return $this->keeko;
71
	}
72
	
73
	/**
74
	 * Returns the keeko module schema
75
	 *
76
	 * @return ModuleSchema
77
	 */
78
	public function getModule() {
79
		if ($this->module === null) {
80
			$keeko = $this->getKeeko();
81
			$this->module = $keeko->getModule();
82
		}
83
	
84
		return $this->module;
85
	}
86
	
87 8
	/**
88 8
	 * Returns the keeko app schema
89 8
	 *
90 5
	 * @return AppSchema
91
	 */
92
	public function getApp() {
93 7
		if ($this->app === null) {
94
			$keeko = $this->getKeeko();
95
			$this->app = $keeko->getApp();
96 5
		}
97 5
	
98 5
		return $this->app;
99 5
	}
100 5
	
101 5
	/**
102 5
	 * Returns an action
103
	 *
104
	 * @param string $name
105 5
	 * @return ActionSchema
106 5
	 */
107
	public function getAction($name) {
108
		$module = $this->getModule();
109
		if ($module !== null && $module->hasAction($name)) {
110
			return $module->getAction($name);
111
		}
112
113
		return null;
114
	}
115
116
	public function getActionType($actionName, $modelName) {
117
		$input = $this->io->getInput();
118
		$type = $input->hasOption('type') ? $input->getOption('type') : null;
119
		if ($type === null) {
120
			if (($pos = strrpos($actionName, '-')) !== false) {
121 16
				$type = substr($actionName, $pos + 1);
122 16
			} else if ($modelName == $actionName) {
123 13
				$type = 'read';
124 13
			}
125 16
		}
126 16
		return $type;
127
	}
128
129
// 	public function updateAction($name, $data) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
130 16
// 		$this->actions[$name] = $data;
131 16
// 		$this->package['extra']['keeko']['module']['actions'] = $this->actions;
132
// 	}
133
	
134
// 	private function getSlug($package = null) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
52% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
135
// 		if ($package === null) {
136
// 			$package = $this->getPackage();
137
// 		}
138
// 		return str_replace('/', '.', $package['name']);
139
// 	}
140
	
141
	public function savePackage(PackageSchema $package = null) {
142
		if ($package === null) {
143
			$package = $this->getPackage();
144
		}
145
		
146
		$filename = $this->service->getProject()->getComposerFileName();
147
		$this->service->getJsonService()->write($filename, $package->toArray());
148
		$this->io->writeln(sprintf('Package <info>%s</info> written at <info>%s</info>', $package->getFullName(), $filename));
149
	}
150
}
151