Passed
Pull Request — develop ( #58 )
by
unknown
32:22 queued 14:42
created

StyleManager::getStyleValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
ccs 0
cts 4
cp 0
crap 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: newicon
5
 * Date: 25/09/2018
6
 * Time: 15:11
7
 */
8
9
namespace neon\core\web;
10
11
12
use neon\core\helpers\Arr;
13
use neon\core\helpers\Str;
14
use themedefault\ThemeAssets;
0 ignored issues
show
Bug introduced by
The type themedefault\ThemeAssets 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...
15
use yii\base\Component;
16
use \neon\core\ApplicationWeb;
17
18
class StyleManager extends Component
19
{
20
	protected $_styles = [];
21
	protected $_fields = [];
22
	protected $_definition = [];
23
24
	public function init()
25
	{
26
		parent::init();
27
		neon()->on(ApplicationWeb::EVENT_AFTER_NEON_BOOTSTRAP, [$this, 'loadStyles']);
28
	}
29
30
	public function loadStylesFromConfig()
31
	{
32
33
	}
34
35
	public function DiscoverStylesFromBundle($bundle)
0 ignored issues
show
Unused Code introduced by
The parameter $bundle is not used and could be removed. ( Ignorable by Annotation )

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

35
	public function DiscoverStylesFromBundle(/** @scrutinizer ignore-unused */ $bundle)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
36
	{
37
		$bundle = new ThemeAssets();
38
		$view = new View();
39
		$view->registerAssetBundle($bundle);
40
		$view->resolveBundles();
41
		$view->cssConcatenateFiles($view->cssFiles, $key);
42
		$view->cssProcess = true;
43
		// parse cssFile for smarty tags
44
		$view->getCssByKey($key);
45
46
	}
47
48
	public function getForm()
49
	{
50
		$f = new \neon\core\form\Form('style');
51
		foreach($this->getDefinition() as $field) {
52
			$f->add($field);
53
		}
54
		$f->load($this->getStyles());
55
		return $f;
56
	}
57
58
	public function loadStyles($key='default')
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed. ( Ignorable by Annotation )

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

58
	public function loadStyles(/** @scrutinizer ignore-unused */ $key='default')

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
59
	{
60
		return setting('cms', 'styles');
61
	}
62
63
	/**
64
	 * @param string $name - the name of the css variable
65
	 * @param string $default - the default value for the variable
66
	 * @param array|string $type - a form field config to enable editing the property
67
	 * @param array $options
68
	 * @return string - the current value for the style if the style variable with he name $name already exists
69
	 */
70
	public function add($name, $default, $type, $options=[])
71
	{
72
		// check if a style property already exists
73
		$styleValue = $this->getStyleValue($name, $default);
74
		$options['name'] = $name;
75
		if (!isset($options['class']))
76
			$options['class'] = $type;
77
		$options['label'] = Arr::get($options, 'label', Str::humanize($name));
78
		$this->set($name, $styleValue, $options);
79
		return $styleValue;
80
	}
81
82
	public function getStyleValue($name, $default='')
83
	{
84
		$styles = $this->getStyles();
85
		return Arr::get($styles, $name, $default);
86
	}
87
88
	public function set($name, $value, $field=null, $key='default')
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed. ( Ignorable by Annotation )

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

88
	public function set($name, $value, $field=null, /** @scrutinizer ignore-unused */ $key='default')

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
89
	{
90
		$styles = $this->getStyles();
91
		Arr::setValue($styles, $name, $value);
92
		set_setting('cms', 'styles', $styles);
93
		if ($field !== null) {
94
			$definition = $this->getDefinition();
95
			Arr::setValue($definition, $name, $field);
96
			set_setting('cms', 'styles_definition', $definition);
97
		}
98
	}
99
100
	public function save()
101
	{
102
		// only save if it has changed
103
		//if (md5(setting('cms', 'styles_definition')) !== md5($this->_definition)) {
104
			set_setting('cms', 'styles_definition', $this->_definition);
105
		//}
106
		///if (md5(setting('cms', 'styles')) !== md5($this->_styles)) {
107
			set_setting('cms', 'styles', $this->_styles);
108
		//}
109
	}
110
111
	/**
112
	 * @return array
113
	 */
114
	public function getStyles()
115
	{
116
		return ['color' => '#000'];
117
//		return setting('cms', 'styles', []);
118
	}
119
120
	/**
121
	 * @return array
122
	 */
123
	public function getDefinition()
124
	{
125
		return setting('cms', 'styles_definition', []);
126
	}
127
}