Completed
Pull Request — master (#268)
by
unknown
01:13
created

Xhgui_Controller_Import::index()   B

Complexity

Conditions 6
Paths 16

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 8.8657
c 0
b 0
f 0
cc 6
nc 16
nop 0
1
<?php
2
3
use Slim\Slim;
4
5
class Xhgui_Controller_Import extends Xhgui_Controller
6
{
7
8
    /**
9
     * @var Xhgui_Storage_Factory
10
     */
11
    protected $storageFactory;
12
13
    /**
14
     * @var Xhgui_Saver
15
     */
16
    protected $saver;
17
18
    /**
19
     * Xhgui_Controller_Import constructor.
20
     * @param Slim $app
21
     */
22
    public function __construct(Slim $app, Xhgui_Storage_Factory $storageFactory, Xhgui_Saver $saver)
23
    {
24
        parent::__construct($app);
25
        $this->setStorageFactory($storageFactory);
26
        $this->setSaver($saver);
27
    }
28
29
    /**
30
     * Import main page. Use to select source and target.
31
     */
32
    public function index()
33
    {
34
35
        $settings = $this->app->container->get('settings');
36
37
        $handlers = [];
38
        if (!empty($settings['save.handler.path'])) {
39
            $handlers[] = 'file';
40
        }
41
42
        if (!empty($settings['save.handler.upload.uri'])) {
43
            $handlers[] = 'upload';
44
        }
45
46
        if (!empty($settings['db.host']) && strpos($settings['db.host'], 'mongodb') !== false) {
47
            $handlers[] = 'mongodb';
48
        }
49
        if (!empty($settings['db.dsn'])) {
50
            $handlers[] = 'pdo';
51
        }
52
        $this->_template = 'import/index.twig';
53
        $this->set(array(
54
            'base_url'              => 'home',
55
            'configured_handlers'   => $handlers,
56
            'status'                => $this->app->flashData()
57
        ));
58
    }
59
60
    /**
61
     * Main import function. It does all the work.
62
     */
63
    public function import()
64
    {
65
        $request = $this->app->request();
66
        $this->_template = '';
67
        
68
        $readConfig         = $this->app->container->get('settings');
69
        $saveHandlerConfig  = $this->app->container->get('settings');
70
        $source             = $request->post('source');
71
        $target             = $request->post('target');
72
73
        // get data to import
74
        $readConfig['save.handler'] = $source;
75
        $reader = $this->getStorageFactory()->create($readConfig);
76
77
        // get save handler:
78
        $saveHandlerConfig['save.handler'] = $target;
79
        $saver = $this->getSaver()->create($saveHandlerConfig);
80
81
        try {
82
            $filter = new Xhgui_Storage_Filter();
83
            $page = 0;
84
            $filter->setPage($page);
85
            do {
86
                $allRows = $reader->find($filter);
87
                foreach ($allRows as $row) {
88
                    $saver->save($row);
89
                }
90
91
                $filter->setPage($page++);
92
            } while (count($allRows) > 0);
93
            
94
            $this->app->flash('success', 'Import successful');
95
        } catch (Exception $e) {
96
            $this->app->flash('failure', 'Import failed');
97
        }
98
99
        $this->app->redirect($this->app->urlFor('import'));
100
    }
101
102
    /**
103
     * @return Xhgui_Storage_Factory
104
     */
105
    public function getStorageFactory()
106
    {
107
        return $this->storageFactory;
108
    }
109
110
    /**
111
     * @param Xhgui_Storage_Factory $storageFactory
112
     */
113
    public function setStorageFactory($storageFactory)
114
    {
115
        $this->storageFactory = $storageFactory;
116
    }
117
118
    /**
119
     * @return Xhgui_Saver
120
     */
121
    public function getSaver()
122
    {
123
        return $this->saver;
124
    }
125
126
    /**
127
     * @param Xhgui_Saver $saver
128
     */
129
    public function setSaver($saver)
130
    {
131
        $this->saver = $saver;
132
    }
133
}
134