Completed
Pull Request — master (#16)
by Tim
09:52
created

ImportrController::createAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 0
cts 15
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 14
nc 1
nop 2
crap 2
1
<?php
2
3
namespace HDNET\Importr\Controller;
4
5
use HDNET\Importr\Domain\Model\Import;
6
use HDNET\Importr\Domain\Model\Strategy;
7
use TYPO3\CMS\Core\Messaging\FlashMessage;
8
use TYPO3\CMS\Core\Messaging\FlashMessageService;
9
use TYPO3\CMS\Core\Utility\GeneralUtility;
10
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
11
12
/**
13
 * Description of ImportrController
14
 *
15
 * @author timlochmueller
16
 */
17
class ImportrController extends ActionController
18
{
19
20
    /**
21
     * @var \TYPO3\CMS\Core\Resource\ResourceFactory
22
     * @inject
23
     */
24
    protected $resourceFactory;
25
26
    /**
27
     * @var \HDNET\Importr\Domain\Repository\StrategyRepository
28
     * @inject
29
     */
30
    protected $strategyRepository;
31
32
    /**
33
     * @var \HDNET\Importr\Domain\Repository\ImportRepository
34
     * @inject
35
     */
36
    protected $importRepository;
37
38
    /**
39
     * @var \HDNET\Importr\Service\Manager
40
     * @inject
41
     */
42
    protected $importManager;
43
44
    /**
45
     * @var \HDNET\Importr\Service\ImportServiceInterface
46
     * @inject
47
     */
48
    protected $importService;
49
50
    /**
51
     * @return void
52
     */
53
    public function indexAction()
54
    {
55
        $combinedIdentifier = GeneralUtility::_GP('id');
56
        if (isset($combinedIdentifier) && is_string($combinedIdentifier)) {
57
            $folder = $this->resourceFactory->getFolderObjectFromCombinedIdentifier($combinedIdentifier);
58
            $files = [];
59
            foreach ($folder->getFiles() as $file) {
60
                $files[$file->getStorage()
61
                    ->getUid() . ':' . $file->getIdentifier()] = $file->getName();
62
            }
63
            $this->view->assign('folder', $files);
64
        }
65
        $this->view->assign('imports', $this->importRepository->findUserQueue());
66
    }
67
68
    /**
69
     * @param string $identifier
70
     */
71
    public function importAction($identifier)
72
    {
73
        $file = $this->resourceFactory->getObjectFromCombinedIdentifier($identifier);
74
        $this->view->assign('file', $file);
75
        $this->view->assign('strategies', $this->strategyRepository->findAllUser());
76
    }
77
78
    /**
79
     *
80
     * @param string $identifier
81
     * @param \HDNET\Importr\Domain\Model\Strategy $strategy
82
     *
83
     * @return void
84
     */
85
    public function previewAction($identifier, Strategy $strategy)
86
    {
87
        $file = $this->resourceFactory->getObjectFromCombinedIdentifier($identifier);
88
        $this->view->assign('filepath', $file->getPublicUrl());
89
        $this->view->assign('strategy', $strategy);
90
91
        $previewData = $this->importManager->getPreview($strategy, $file->getPublicUrl());
92
        $this->view->assign('preview', $previewData);
93
    }
94
95
    /**
96
     *
97
     * @param string $filepath
98
     * @param \HDNET\Importr\Domain\Model\Strategy $strategy
99
     *
100
     * @return void
101
     */
102
    public function createAction($filepath, Strategy $strategy)
103
    {
104
        $this->importService->addToQueue($filepath, $strategy);
105
        $text = 'The Import file %s width the strategy %s was successfully added to the queue';
106
        $message = GeneralUtility::makeInstance(
107
            FlashMessage::class,
108
            sprintf($text, $filepath, $strategy->getTitle()),
109
            'Import is in Queue',
110
            FlashMessage::INFO,
111
            true
112
        );
113
114
        $flashMessageService = $this->objectManager->get(
115
            FlashMessageService::class
116
        );
117
        $messageQueue = $flashMessageService->getMessageQueueByIdentifier();
118
        $messageQueue->addMessage($message);
119
120
        $this->redirect('index');
121
    }
122
123
    /**
124
     * @param Import $import
125
     */
126
    public function resetAction(Import $import)
127
    {
128
        $import->reset();
129
        $this->importRepository->update($import);
130
        $this->redirect('index');
131
    }
132
}
133