Completed
Push — master ( 2c03f9...eba89f )
by Ryan
02:13
created

WidgetsController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 4
Bugs 2 Features 0
Metric Value
wmc 8
c 4
b 2
f 0
lcom 0
cbo 2
dl 0
loc 97
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 4 1
A choose() 0 7 1
A create() 0 14 1
A edit() 0 20 1
A save() 0 14 4
1
<?php namespace Anomaly\DashboardModule\Http\Controller\Admin;
2
3
use Anomaly\ConfigurationModule\Configuration\Form\ConfigurationFormBuilder;
4
use Anomaly\DashboardModule\Widget\Contract\WidgetInterface;
5
use Anomaly\DashboardModule\Widget\Contract\WidgetRepositoryInterface;
6
use Anomaly\DashboardModule\Widget\Extension\Form\WidgetExtensionFormBuilder;
7
use Anomaly\DashboardModule\Widget\Extension\WidgetExtension;
8
use Anomaly\DashboardModule\Widget\Form\WidgetFormBuilder;
9
use Anomaly\DashboardModule\Widget\Table\WidgetTableBuilder;
10
use Anomaly\Streams\Platform\Addon\Extension\ExtensionCollection;
11
use Anomaly\Streams\Platform\Http\Controller\AdminController;
12
13
/**
14
 * Class WidgetsController
15
 *
16
 * @link          http://pyrocms.com/
17
 * @author        PyroCMS, Inc. <[email protected]>
18
 * @author        Ryan Thompson <[email protected]>
19
 * @package       Anomaly\DashboardModule\Http\Controller\Admin
20
 */
21
class WidgetsController extends AdminController
22
{
23
24
    /**
25
     * Display an index of existing entries.
26
     *
27
     * @param WidgetTableBuilder $table
28
     * @return \Symfony\Component\HttpFoundation\Response
29
     */
30
    public function index(WidgetTableBuilder $table)
31
    {
32
        return $table->render();
33
    }
34
35
    /**
36
     * Return the modal for choosing a widget.
37
     *
38
     * @param ExtensionCollection $extensions
39
     * @return \Illuminate\Contracts\View\View|mixed
40
     */
41
    public function choose(ExtensionCollection $extensions)
42
    {
43
        return $this->view->make(
44
            'module::ajax/choose_widget',
45
            ['extensions' => $extensions->search('anomaly.module.dashboard::widget.*')]
46
        );
47
    }
48
49
    /**
50
     * Create a new entry.
51
     *
52
     * @param ExtensionCollection                          $extensions
53
     * @param WidgetExtensionFormBuilder|WidgetFormBuilder $form
54
     * @param WidgetFormBuilder                            $widget
55
     * @param ConfigurationFormBuilder                     $configuration
56
     * @return \Symfony\Component\HttpFoundation\Response
57
     */
58
    public function create(
59
        ExtensionCollection $extensions,
60
        WidgetExtensionFormBuilder $form,
61
        WidgetFormBuilder $widget,
62
        ConfigurationFormBuilder $configuration
63
    ) {
64
        /* @var WidgetExtension $extension */
65
        $extension = $extensions->get($this->request->get('extension'));
66
67
        $form->addForm('widget', $widget->setExtension($extension));
68
        $form->addForm('configuration', $configuration->setEntry($extension->getNamespace()));
69
70
        return $form->render();
71
    }
72
73
    /**
74
     * Edit an existing entry.
75
     *
76
     * @param ExtensionCollection                          $extensions
77
     * @param WidgetExtensionFormBuilder|WidgetFormBuilder $form
78
     * @param WidgetFormBuilder                            $widget
79
     * @param ConfigurationFormBuilder                     $configuration
80
     * @return \Symfony\Component\HttpFoundation\Response
81
     */
82
    public function edit(
83
        ExtensionCollection $extensions,
84
        WidgetExtensionFormBuilder $form,
85
        WidgetFormBuilder $widget,
86
        ConfigurationFormBuilder $configuration,
87
        WidgetRepositoryInterface $widgets,
88
        $id
89
    ) {
90
        /* @var WidgetInterface $entry */
91
        $entry = $widgets->find($id);
92
93
        /* @var WidgetExtension $extension */
94
        $extension = $entry->getExtension();
95
96
        $form->setEntry($id);
97
        $form->addForm('widget', $widget->setEntry($id));
98
        $form->addForm('configuration', $configuration->setScope($id)->setEntry($extension->getNamespace()));
99
100
        return $form->render();
101
    }
102
103
    public function save(WidgetRepositoryInterface $widgets)
104
    {
105
        foreach (json_decode($this->request->get('columns')) as $column => $columns) {
106
            foreach ($columns as $position => $widget) {
107
                if ($widget = $widgets->find($widget->id)) {
108
109
                    $widget->setAttribute('column', $column + 1);
110
                    $widget->setAttribute('sort_order', $position + 1);
111
112
                    $widgets->save($widget);
113
                }
114
            }
115
        }
116
    }
117
}
118