Completed
Pull Request — master (#268)
by
unknown
03:19
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
     * @var Xhgui_Profiles
9
     */
10
    private $profiles;
11
12
    /**
13
     * Xhgui_Controller_Import constructor.
14
     * @param Slim $app
15
     * @param Xhgui_Profiles $profiles
16
     */
17
    public function __construct(Slim $app, Xhgui_Profiles $profiles)
18
    {
19
        parent::__construct($app);
20
        $this->setProfiles($profiles);
21
    }
22
23
    /**
24
     * Import main page. Use to select source and target.
25
     */
26
    public function index()
27
    {
28
29
        $settings = $this->app->container->get('settings');
30
31
        $handlers = [];
32
        if (!empty($settings['save.handler.filename'])) {
33
            $handlers[] = 'file';
34
        }
35
36
        if (!empty($settings['save.handler.upload.uri'])) {
37
            $handlers[] = 'upload';
38
        }
39
40
        if (!empty($settings['db.host']) && strpos($settings['db.host'], 'mongodb')) {
41
            $handlers[] = 'mongodb';
42
        }
43
        if (!empty($settings['db.dsn'])) {
44
            $handlers[] = 'pdo';
45
        }
46
        $this->_template = 'import/index.twig';
47
        $this->set(array(
48
            'base_url'              => 'home',
49
            'configured_handlers'   => $handlers,
50
            'status'                => $this->app->flashData()
51
        ));
52
    }
53
54
    /**
55
     * Main import function. It does all the work.
56
     */
57
    public function import()
58
    {
59
        $request = $this->app->request();
60
        $this->_template = '';
61
        
62
        $readConfig         = $this->app->container->get('settings');
63
        $saveHandlerConfig  = $this->app->container->get('settings');
64
        $source             = $request->post('source');
65
        $target             = $request->post('target');
66
67
        // get data to import
68
        $readConfig['save.handler'] = $source;
69
        $reader = Xhgui_Storage_Factory::factory($readConfig);
70
71
        // get save handler:
72
        $saveHandlerConfig['save.handler'] = $target;
73
        $saver = Xhgui_Saver::factory($saveHandlerConfig);
74
75
76
        try {
77
            $filter = new Xhgui_Storage_Filter();
78
            $page = 0;
79
            $filter->setPage($page);
80
            do {
81
                $allRows = $reader->find($filter);
82
                foreach ($allRows as $row) {
83
                    $row['meta']['application'] = 'test';
84
                    $row['meta']['version'] = 'test';
85
                    $row['meta']['branch'] = 'test';
86
                    $row['meta']['controller'] = 'test';
87
                    $row['meta']['action'] = 'test';
88
                    $row['meta']['session_id'] = 'test';
89
                    $saver->save($row);
90
                }
91
92
                $filter->setPage($page++);
93
            } while ($allRows->count() > 0);
94
            
95
            $this->app->flash('success', 'Import successful');
96
        } catch (Exception $e) {
97
            $this->app->flash('failure', 'Import failed');
98
        }
99
100
        $this->app->redirect($this->app->urlFor('import'));
101
    }
102
103
    /**
104
     * @return Xhgui_Profiles
105
     */
106
    public function getProfiles()
107
    {
108
        return $this->profiles;
109
    }
110
111
    /**
112
     * @param Xhgui_Profiles $profiles
113
     */
114
    public function setProfiles($profiles)
115
    {
116
        $this->profiles = $profiles;
117
    }
118
}
119