Passed
Push — master ( 4954b5...87483c )
by Georgi
03:11
created

LogoSettings   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 35
c 1
b 0
f 0
dl 0
loc 76
rs 10
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A body() 0 47 2
A getLogoMeta() 0 7 1
A getTitle() 0 3 1
A access() 0 3 1
A getLogoPath() 0 3 1
1
<?php
2
3
namespace Epesi\Core\System\Logo;
4
5
use Epesi\Core\System\Integration\Modules\ModuleView;
6
use Illuminate\Support\Facades\Auth;
7
use Epesi\Core\System\Seeds\Form;
8
use Epesi\Core\System\Database\Models\Variable;
9
use Illuminate\Support\Facades\Storage;
10
use Epesi\Core\Layout\Seeds\ActionBar;
11
use atk4\ui\View;
12
13
class LogoSettings extends ModuleView
14
{
15
	protected $label = 'Title & Logo';
16
	
17
	public static function access()
18
	{
19
		return Auth::user()->can('modify system settings');
20
	}
21
	
22
	public function body()
23
	{
24
		$layout = $this->add(['View'])->addStyle('max-width:1200px;margin:auto;');
25
		$layout->add(['Header', __($this->label)]);
26
		$segment = $layout->add(['View', ['ui' => 'segment']]);
27
28
		$form = $segment->add(new Form());
29
30
		$form->addField('title', __('Base page title'))->set(Variable::get('system.title'));		
31
		$logo = $form->addField('logo', [
32
				'UploadImg', 
33
				'defaultSrc' => url('logo'), 
34
				'thumbnail' => (new View(['element'=>'img', 'class' => ['right', 'floated', 'image'], 'ui' => true]))->setStyle('max-width', '150px'),
35
				'placeholder' => __('Upload new logo')
36
		]);
37
		
38
		$logo->onDelete(function($fileName) {
0 ignored issues
show
Bug introduced by
The method onDelete() does not exist on atk4\ui\FormField\Generic. It seems like you code against a sub-type of atk4\ui\FormField\Generic such as atk4\ui\FormField\Upload. ( Ignorable by Annotation )

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

38
		$logo->/** @scrutinizer ignore-call */ 
39
         onDelete(function($fileName) {
Loading history...
39
			$token = md5($fileName);
40
			
41
			Storage::delete(self::alias() . '/tmp/' . $token);
42
		});
43
44
		$logo->onUpload(function ($files) use ($form, $logo) {
0 ignored issues
show
Bug introduced by
The method onUpload() does not exist on atk4\ui\FormField\Generic. It seems like you code against a sub-type of atk4\ui\FormField\Generic such as atk4\ui\FormField\Upload. ( Ignorable by Annotation )

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

44
		$logo->/** @scrutinizer ignore-call */ 
45
         onUpload(function ($files) use ($form, $logo) {
Loading history...
45
			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

45
			if ($files === 'error')	return $form->error('logo', /** @scrutinizer ignore-type */ __('Error uploading image'));
Loading history...
46
			
47
			$tmpPath = self::alias() . '/tmp/' . $files['name'];
48
			
49
			$logo->setThumbnailSrc(asset('storage/' . $tmpPath));
0 ignored issues
show
Bug introduced by
The method setThumbnailSrc() does not exist on atk4\ui\FormField\Generic. It seems like you code against a sub-type of atk4\ui\FormField\Generic such as atk4\ui\FormField\UploadImg. ( Ignorable by Annotation )

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

49
			$logo->/** @scrutinizer ignore-call */ 
50
          setThumbnailSrc(asset('storage/' . $tmpPath));
Loading history...
50
	
51
			Storage::disk('public')->put($tmpPath, file_get_contents($files['tmp_name']));
52
		});
53
					
54
		$form->onSubmit(function($form) {
55
			$name = $form->model['logo'];
56
						
57
			Storage::disk('public')->move(self::alias() . '/tmp/' . $name, self::alias() . '/' . $name);
58
						
59
			Variable::put('system.title', $form->model['title']);
60
			
61
			Variable::put('system.logo', $name);
62
						
63
			return $form->notify(__('Title and logo updated!'));
64
		});
65
			
66
		ActionBar::addButton('back')->link(url('view/system'));
67
			
68
		ActionBar::addButton('save')->on('click', $form->submit());
69
	}
70
	
71
	public static function getLogoPath()
72
	{
73
		return self::alias() . '/' . Variable::get('system.logo', 'epesi-logo.png');
74
	}
75
	
76
	public static function getLogoMeta()
77
	{
78
		$logoPath = self::getLogoPath();
79
		
80
		return array_merge(Storage::getMetadata($logoPath), [
81
				'mime' => Storage::mimeType($logoPath),
82
				'contents' => Storage::get($logoPath)
83
		]);
84
	}
85
	
86
	public static function getTitle()
87
	{
88
		return Variable::get('system.title', config('epesi.app.title'));
89
	}
90
}
91