Passed
Push — dougall-pheobe-database-map-fi... ( ...2d7731 )
by
unknown
22:10
created

FormController::actionSave()   A

Complexity

Conditions 5
Paths 10

Size

Total Lines 37
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 37
rs 9.2888
c 0
b 0
f 0
cc 5
nc 10
nop 1
ccs 0
cts 25
cp 0
crap 30
1
<?php
2
/**
3
 * @link http://www.newicon.net/neon
4
 * @copyright Copyright (c) 2017-2018 Newicon Ltd
5
 * @license http://www.newicon.net/neon/license/
6
 * @author Steve O'Brien <[email protected]> 10/10/2017 16:20
7
 * @package neon/phoebe
8
 */
9
10
namespace neon\phoebe\controllers\appforms\api;
11
12
use neon\core\web\ApiController;
13
use yii\web\HttpException;
14
use neon\core\helpers\Hash;
15
16
/**
17
 * This handles form definition changes via the Phoebe Builder
18
 */
19
class FormController extends ApiController
20
{
21
22
	/**
23
	 * @inheritdoc
24
	 */
25
	public function verbs()
26
	{
27
		return [
28
			'save' => ['post'],
29
		];
30
	}
31
32
	/**
33
	 * Save an applicationForm definition
34
	 * @param string $class the application form class_type
35
	 * @return array|null json
36
	 */
37
	public function actionSave($class)
38
	{
39
		$phoebe = $this->phoebe();
40
41
		// temporary hack to sort out uuids being passed in rather than
42
		// a human readable definition.
43
		if ($this->is_uuid($class))
44
			$class=str_replace(['-','_'], ['a','Z'],Hash::uuid2uuid64($class));
45
		else
46
			$class = $phoebe->canonicaliseRef($class);
47
48
		$definition = neon()->request->getBodyParams();
49
		if (!isset($definition['name'])) {
50
			throw new HttpException(400, 'You must supply a form name');
51
		}
52
53
		// start a migration for the changes
54
		$this->startMigration('appform_'.$class.'_edit');
0 ignored issues
show
Bug introduced by
'appform_' . $class . '_edit' of type string is incompatible with the type neon\phoebe\controllers\appforms\api\type expected by parameter $label of neon\phoebe\controllers\...oller::startMigration(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

54
		$this->startMigration(/** @scrutinizer ignore-type */ 'appform_'.$class.'_edit');
Loading history...
55
56
		// save the class information
57
		$phoebeClass = $phoebe->getClass($class);
58
		if (!$phoebeClass)
59
			$phoebeClass = $phoebe->addClass($class, 'phoebe');
60
		$phoebeClass->editClass([
61
			'definition' => $definition,
62
			'label' => $definition['name']
63
		]);
64
65
		// close the migration
66
		$this->endMigration();
67
68
		$phoebeClass =  $phoebe->getClass($class);
69
		if ($phoebeClass == null) {
70
			$phoebeClass = ['error' => "No class exists with a type of '$class'"];
71
		}
72
73
		return $phoebeClass->toArray();
74
	}
75
76
	// ------------------------------------- //
77
	// ----------- Private Methods ----------//
78
79
	/**
80
	 * Temporary hack to see if the string is a uuid - not an exhaustive test
81
	 */
82
	private function is_uuid($str)
83
	{
84
		return (strlen($str)==36 && substr_count($str, '-')==4);
85
	}
86
87
	/**
88
	 * Create a migration with the provided label
89
	 * @param type $label
0 ignored issues
show
Bug introduced by
The type neon\phoebe\controllers\appforms\api\type was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
90
	 */
91
	private function startMigration($label)
92
	{
93
		neon('dds')->getIDdsAppMigrator('phoebe')->openMigrationFile($label);
94
	}
95
96
	/**
97
	 * close the currently open migration
98
	 */
99
	private function endMigration()
100
	{
101
		neon('dds')->getIDdsAppMigrator('phoebe')->closeMigrationFile();
102
	}
103
104
	private function phoebe()
105
	{
106
		return neon('phoebe')->getService()->getPhoebeType('applicationForm');
107
	}
108
109
}
110