LogoSettings::body()   A
last analyzed

Complexity

Conditions 5
Paths 1

Size

Total Lines 65
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
cc 5
eloc 36
c 4
b 1
f 0
nc 1
nop 0
dl 0
loc 65
rs 9.0328

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Epesi\Core\System\Logo;
4
5
use Epesi\Core\System\Modules\ModuleView;
6
use Illuminate\Support\Facades\Auth;
7
use Epesi\Core\System\View\Form;
8
use Epesi\Core\System\Model\Variable;
9
use Illuminate\Support\Facades\Storage;
10
use Epesi\Core\Layout\View\ActionBar;
11
use atk4\ui\View;
12
13
class LogoSettings extends ModuleView
14
{
15
	protected $label = 'Title & Logo';
16
	
17
	protected static $defaultLogo = 'epesi-logo.png';
18
	
19
	public static function access()
20
	{
21
		return Auth::user()->can('modify system settings');
22
	}
23
	
24
	public function body()
25
	{
26
		$layout = View::addTo($this)->addStyle('max-width:1200px;margin:auto;');
27
		\atk4\ui\Header::addTo($layout, [ __($this->label)]);
28
		$segment = View::addTo($layout, ['ui' => 'segment']);
29
30
		$form = Form::addTo($segment);
31
32
		$form->addControl('title', __('Base page title'));
33
		
34
		$form->addControl('custom_logo', [\atk4\ui\Form\Control\Checkbox::class, 'caption' => __('Use custom logo')]);
35
		
36
		$form->model->setMulti([
37
		        'title' => Variable::recall('system.title'),
38
		        'custom_logo' => (bool) Variable::recall('system.logo')
39
		]);
40
		
41
		$logo = $form->addControl('logo', [
42
				\atk4\ui\Form\Control\UploadImage::class, 
43
				'defaultSrc' => url('logo'), 
44
				'thumbnail' => (new View(['element'=>'img', 'class' => ['right', 'floated', 'image'], 'ui' => true]))->setStyle('max-width', '150px'),
45
				'placeholder' => __('Upload file to replace system logo')
46
		]);
47
		
48
		$form->addControlDisplayRules(['logo' => ['custom_logo' => 'checked']]);
49
		
50
		$logo->onDelete(function($fileName) use ($logo) {
0 ignored issues
show
Bug introduced by
The method onDelete() does not exist on atk4\ui\Form\Control. It seems like you code against a sub-type of atk4\ui\Form\Control such as atk4\ui\Form\Control\Upload. ( Ignorable by Annotation )

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

50
		$logo->/** @scrutinizer ignore-call */ 
51
         onDelete(function($fileName) use ($logo) {
Loading history...
51
			$this->storage()->delete(self::alias() . '/tmp/' . $fileName);
52
			
53
			$logo->setThumbnailSrc(asset('storage/' . self::alias() . '/' . self::$defaultLogo));
0 ignored issues
show
Bug introduced by
The method setThumbnailSrc() does not exist on atk4\ui\Form\Control. It seems like you code against a sub-type of atk4\ui\Form\Control such as atk4\ui\Form\Control\UploadImage. ( Ignorable by Annotation )

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

53
			$logo->/** @scrutinizer ignore-call */ 
54
          setThumbnailSrc(asset('storage/' . self::alias() . '/' . self::$defaultLogo));
Loading history...
54
		});
55
56
		$logo->onUpload(function ($files) use ($form, $logo) {
0 ignored issues
show
Bug introduced by
The method onUpload() does not exist on atk4\ui\Form\Control. It seems like you code against a sub-type of atk4\ui\Form\Control such as atk4\ui\Form\Control\Upload. ( Ignorable by Annotation )

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

56
		$logo->/** @scrutinizer ignore-call */ 
57
         onUpload(function ($files) use ($form, $logo) {
Loading history...
57
			if ($files === 'error')	return $form->error('logo', __('Error uploading image'));
0 ignored issues
show
Bug introduced by
It seems like __('Error uploading image') can also be of type array; however, parameter $str of atk4\ui\Form::error() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

57
			if ($files === 'error')	return $form->error('logo', /** @scrutinizer ignore-type */ __('Error uploading image'));
Loading history...
58
			
59
			$tmpPath = self::alias() . '/tmp/' . $files['name'];
60
			
61
			$logo->setThumbnailSrc(asset('storage/' . $tmpPath));
62
	
63
			$this->storage()->put($tmpPath, file_get_contents($files['tmp_name']));
64
		});
65
					
66
		$form->onSubmit(function($form) {
67
			if ($logo = $form->model->get('custom_logo') ? $form->model->get('logo') : null) {
68
				$storage = $this->storage();
69
				$from = self::alias() . '/tmp/' . $logo;
70
				$to = self::alias() . '/' . $logo;				
71
				
72
				if ($storage->exists($to)) {
73
					$storage->delete($to);
74
				}
75
				
76
				$storage->move($from, $to);
77
			}
78
79
	        Variable::memorize('system.logo', $logo);
80
			
81
			Variable::memorize('system.title', $form->model->get('title'));
82
			
83
			return $this->notifySuccess(__('Title and logo updated! Refresh page to see changes ...'));
84
		});
85
			
86
		ActionBar::addItemButton('back')->link(url('view/system'));
87
			
88
		ActionBar::addItemButton('save')->on('click', $form->submit());
89
	}
90
	
91
	public static function getLogoFile()
92
	{
93
		return self::storage()->path(self::alias() . '/' . Variable::recall('system.logo', self::$defaultLogo));
94
	}
95
	
96
	public static function getTitle()
97
	{
98
		return Variable::recall('system.title', config('epesi.ui.title'));
99
	}
100
101
	public static function storage()
102
	{
103
		return Storage::disk('public');
104
	}
105
	
106
}
107