Conditions | 5 |
Paths | 1 |
Total Lines | 60 |
Code Lines | 33 |
Lines | 0 |
Ratio | 0 % |
Changes | 4 | ||
Bugs | 1 | Features | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
24 | public function body() |
||
25 | { |
||
26 | $layout = $this->add(['View'])->addStyle('max-width:1200px;margin:auto;'); |
||
27 | $layout->add(['Header', __($this->label)]); |
||
28 | $segment = $layout->add(['View', ['ui' => 'segment']]); |
||
29 | |||
30 | $form = $segment->add(new Form()); |
||
31 | |||
32 | $form->addField('title', __('Base page title'))->set(Variable::get('system.title')); |
||
33 | |||
34 | $form->addField('custom_logo', ['CheckBox', 'caption' => __('Use custom logo')])->set((bool) Variable::get('system.logo')); |
||
35 | |||
36 | $logo = $form->addField('logo', [ |
||
37 | 'UploadImg', |
||
38 | 'defaultSrc' => url('logo'), |
||
39 | 'thumbnail' => (new View(['element'=>'img', 'class' => ['right', 'floated', 'image'], 'ui' => true]))->setStyle('max-width', '150px'), |
||
40 | 'placeholder' => __('Upload file to replace system logo') |
||
41 | ]); |
||
42 | |||
43 | $form->addFieldsDisplayRules(['logo' => ['custom_logo' => 'checked']]); |
||
44 | |||
45 | $logo->onDelete(function($fileName) use ($logo) { |
||
|
|||
46 | $this->storage()->delete(self::alias() . '/tmp/' . $fileName); |
||
47 | |||
48 | $logo->setThumbnailSrc(asset('storage/' . self::alias() . '/' . self::$defaultLogo)); |
||
49 | }); |
||
50 | |||
51 | $logo->onUpload(function ($files) use ($form, $logo) { |
||
52 | if ($files === 'error') return $form->error('logo', __('Error uploading image')); |
||
53 | |||
54 | $tmpPath = self::alias() . '/tmp/' . $files['name']; |
||
55 | |||
56 | $logo->setThumbnailSrc(asset('storage/' . $tmpPath)); |
||
57 | |||
58 | $this->storage()->put($tmpPath, file_get_contents($files['tmp_name'])); |
||
59 | }); |
||
60 | |||
61 | $form->onSubmit(function($form) { |
||
62 | if ($name = $form->model['custom_logo']? $form->model['logo']: null) { |
||
63 | $storage = $this->storage(); |
||
64 | $from = self::alias() . '/tmp/' . $name; |
||
65 | $to = self::alias() . '/' . $name; |
||
66 | |||
67 | if ($storage->exists($to)) { |
||
68 | $storage->delete($to); |
||
69 | } |
||
70 | |||
71 | $storage->move($from, $to); |
||
72 | } |
||
73 | |||
74 | Variable::put('system.logo', $name); |
||
75 | |||
76 | Variable::put('system.title', $form->model['title']); |
||
77 | |||
78 | return $form->notify(__('Title and logo updated! Refresh page to see changes ...')); |
||
79 | }); |
||
80 | |||
81 | ActionBar::addButton('back')->link(url('view/system')); |
||
82 | |||
83 | ActionBar::addButton('save')->on('click', $form->submit()); |
||
84 | } |
||
102 |