Completed
Pull Request — master (#273)
by
unknown
01:12
created

Xhgui_Controller_Import   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 3
dl 0
loc 125
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B index() 0 24 6
A import() 0 38 4
A getStorageFactory() 0 4 1
A setStorageFactory() 0 4 1
A getSaver() 0 4 1
A setSaver() 0 4 1
1
<?php
2
3
use Slim\Slim;
4
5
class Xhgui_Controller_Import extends Xhgui_Controller
6
{
7
    /**
8
     * @var Xhgui_Storage_Factory
9
     */
10
    protected $storageFactory;
11
12
    /**
13
     * @var Xhgui_Saver
14
     */
15
    protected $saver;
16
17
    /**
18
     * Xhgui_Controller_Import constructor.
19
     * @param Slim $app
20
     */
21
    public function __construct(Slim $app, Xhgui_Storage_Factory $storageFactory, Xhgui_Saver $saver)
22
    {
23
        parent::__construct($app);
24
        $this->setStorageFactory($storageFactory);
25
        $this->setSaver($saver);
26
    }
27
28
    /**
29
     * Import main page. Use to select source and target.
30
     */
31
    public function index()
32
    {
33
        $settings = $this->app->container->get('settings');
34
35
        $handlers = array();
36
        if (!empty($settings['save.handler.path'])) {
37
            $handlers[] = 'file';
38
        }
39
        if (!empty($settings['save.handler.upload.uri'])) {
40
            $handlers[] = 'upload';
41
        }
42
        if (!empty($settings['db.host']) && strpos($settings['db.host'], 'mongodb') !== false) {
43
            $handlers[] = 'mongodb';
44
        }
45
        if (!empty($settings['db.dsn'])) {
46
            $handlers[] = 'pdo';
47
        }
48
        $this->_template = 'import/index.twig';
49
        $this->set(array(
50
            'base_url'              => 'home',
51
            'configured_handlers'   => $handlers,
52
            'status'                => $this->app->flashData()
53
        ));
54
    }
55
56
    /**
57
     * Main import function. It does all the work.
58
     */
59
    public function import()
60
    {
61
        $request = $this->app->request();
62
        $this->_template = '';
63
        
64
        $readConfig = $this->app->container->get('settings');
65
        $saveHandlerConfig = $this->app->container->get('settings');
66
        $source = $request->post('source');
67
        $target = $request->post('target');
68
69
        // get data to import
70
        $readConfig['save.handler'] = $source;
71
        $reader = $this->getStorageFactory()->create($readConfig);
72
73
        // get save handler:
74
        $saveHandlerConfig['save.handler'] = $target;
75
        $saver = $this->getSaver()->create($saveHandlerConfig);
0 ignored issues
show
Bug introduced by
The method create() does not seem to exist on object<Xhgui_Saver>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
76
77
        try {
78
            $filter = new Xhgui_Storage_Filter();
79
            $page = 0;
80
            $filter->setPage($page);
81
            do {
82
                $allRows = $reader->find($filter);
83
                foreach ($allRows as $row) {
84
                    $saver->save($row);
85
                }
86
87
                $filter->setPage($page++);
88
            } while (count($allRows) > 0);
89
            
90
            $this->app->flash('success', 'Import successful');
91
        } catch (Exception $e) {
92
            $this->app->flash('failure', 'Import failed');
93
        }
94
95
        $this->app->redirect($this->app->urlFor('import'));
96
    }
97
98
    /**
99
     * @return Xhgui_Storage_Factory
100
     */
101
    public function getStorageFactory()
102
    {
103
        return $this->storageFactory;
104
    }
105
106
    /**
107
     * @param Xhgui_Storage_Factory $storageFactory
108
     */
109
    public function setStorageFactory($storageFactory)
110
    {
111
        $this->storageFactory = $storageFactory;
112
    }
113
114
    /**
115
     * @return Xhgui_Saver
116
     */
117
    public function getSaver()
118
    {
119
        return $this->saver;
120
    }
121
122
    /**
123
     * @param Xhgui_Saver $saver
124
     */
125
    public function setSaver($saver)
126
    {
127
        $this->saver = $saver;
128
    }
129
}
130