Completed
Push — master ( 1c2262...88c0f4 )
by Thomas
05:39
created

ActionCommandHelperTrait   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 11
c 2
b 0
f 0
lcom 1
cbo 8
dl 0
loc 59
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
getService() 0 1 ?
A getAction() 0 10 2
C getAcl() 0 29 8
A guessClassname() 0 5 1
1
<?php
2
namespace keeko\tools\helpers;
3
4
use keeko\framework\schema\ActionSchema;
5
use keeko\tools\services\CommandService;
6
use keeko\tools\utils\NamespaceResolver;
7
use keeko\framework\utils\NameUtils;
8
9
trait ActionCommandHelperTrait {
10
	
11
	/**
12
	 * @return CommandService
13
	 */
14
	abstract protected function getService();
15
	
16
	/**
17
	 *
18
	 * @param string $actionName
19
	 * @return ActionSchema
20
	 */
21
	private function getAction($actionName) {
22
		$packageService = $this->getService()->getPackageService();
23
		$action = $packageService->getAction($actionName);
24
		if ($action === null) {
25
			$action = new ActionSchema($actionName);
26
			$module = $packageService->getModule();
27
			$module->addAction($action);
28
		}
29
		return $action;
30
	}
31
	
32
	private function getAcl(ActionSchema $action) {
33
		$input = $this->getService()->getIOService()->getInput();
34
		$acls = [];
35
		$acl = $input->getOption('acl');
36
		if ($acl !== null && count($acl) > 0) {
37
			if (!is_array($acl)) {
38
				$acl = [$acl];
39
			}
40
			foreach ($acl as $group) {
41
				if (strpos($group, ',') !== false) {
42
					$groups = explode(',', $group);
43
					foreach ($groups as $g) {
44
						$acls[] = trim($g);
45
					}
46
				} else {
47
					$acls[] = $group;
48
				}
49
			}
50
				
51
			return $acls;
52
		}
53
	
54
		// read default from package
55
		if (!$action->getAcl()->isEmpty()) {
56
			return $action->getAcl()->toArray();
57
		}
58
	
59
		return $acls;
60
	}
61
	
62
	private function guessClassname($name) {
63
		$package = $this->getService()->getPackageService()->getPackage();
64
		$namespace = NamespaceResolver::getNamespace('src/action', $package);
65
		return $namespace . '\\' . NameUtils::toStudlyCase($name) . 'Action';
66
	}
67
}