Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ExportByHandler
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 3
182
0.00% covered (danger)
0.00%
0 / 1
 __invoke
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
42
 validateParams
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
42
 getFileSystemService
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3/**
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; under version 2
7 * of the License (non-upgradable).
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 *
18 * Copyright (c) 2017 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
19 *
20 */
21
22namespace oat\tao\model\task;
23
24use common_report_Report as Report;
25use oat\oatbox\extension\AbstractAction;
26use oat\oatbox\filesystem\FileSystemService;
27use oat\tao\model\taskQueue\Task\FilesystemAwareTrait;
28
29class ExportByHandler extends AbstractAction
30{
31    use FilesystemAwareTrait;
32
33    public const PARAM_EXPORT_HANDLER = 'export_handler';
34    public const PARAM_EXPORT_DATA = 'export_data';
35
36    public function __invoke($params)
37    {
38        $this->validateParams($params);
39
40        /** @var \tao_models_classes_export_ExportHandler $exporter */
41        $exporter = new $params[self::PARAM_EXPORT_HANDLER]();
42
43        try {
44            // export data under a temp directory stored locally
45            $report = $exporter->export($params[self::PARAM_EXPORT_DATA], \tao_helpers_Export::getExportPath());
46
47            if ($report instanceof Report) {
48                $filePath = $report->getData();
49            } else {
50                $filePath = (string) $report;
51                $report = $filePath
52                    ? Report::createSuccess(__('Export succeeded.'))
53                    : Report::createFailure(__('Export failed.'));
54            }
55
56            if ($filePath && ($newFileName = $this->saveFileToStorage($filePath))) {
57                // set the new file name
58                $report->setData($newFileName);
59            }
60        } catch (\common_exception_UserReadableException $e) {
61            $report = Report::createFailure($e->getUserMessage());
62        }
63
64        return $report;
65    }
66
67    /**
68     * @param array $params
69     * @throws \InvalidArgumentException
70     */
71    private function validateParams($params)
72    {
73        if (
74            !isset($params[self::PARAM_EXPORT_HANDLER])
75            || !class_exists($params[self::PARAM_EXPORT_HANDLER])
76            || !is_a($params[self::PARAM_EXPORT_HANDLER], \tao_models_classes_export_ExportHandler::class, true)
77        ) {
78            throw new \InvalidArgumentException('Please provide a valid export handler');
79        }
80
81        if (!isset($params[self::PARAM_EXPORT_DATA]) || !is_array($params[self::PARAM_EXPORT_DATA])) {
82            throw new \InvalidArgumentException('Please provide the export data as array');
83        }
84    }
85
86    /**
87     * @see FilesystemAwareTrait::getFileSystemService()
88     */
89    protected function getFileSystemService()
90    {
91        return $this->getServiceLocator()
92            ->get(FileSystemService::SERVICE_ID);
93    }
94}