Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
AsynchronousResultTransmissionEventHandler
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 4
30
0.00% covered (danger)
0.00%
0 / 1
 transmitResultItemVariable
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 transmitResultTestVariable
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 getQueueDispatcher
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 packVariables
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
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) 2021 (original work) Open Assessment Technologies SA;
19 */
20
21declare(strict_types=1);
22
23namespace oat\taoQtiTest\models\classes\eventHandler\ResultTransmissionEventHandler;
24
25use oat\oatbox\service\ServiceManager;
26use oat\tao\model\service\InjectionAwareService;
27use oat\tao\model\taskQueue\QueueDispatcher;
28use oat\taoQtiTest\models\classes\event\ResultTestVariablesTransmissionEvent;
29use oat\taoQtiTest\models\classes\tasks\ResultTransmission\AbstractResultTransmissionTask;
30use oat\taoQtiTest\models\classes\tasks\ResultTransmission\ResultItemVariableTransmissionTask;
31use oat\taoQtiTest\models\classes\tasks\ResultTransmission\ResultTestVariableTransmissionTask;
32use oat\taoQtiTest\models\event\ResultItemVariablesTransmissionEvent;
33
34class AsynchronousResultTransmissionEventHandler extends InjectionAwareService implements
35    Api\ResultTransmissionEventHandlerInterface
36{
37    public function transmitResultItemVariable(ResultItemVariablesTransmissionEvent $event): void
38    {
39        $queueDispatcher = $this->getQueueDispatcher();
40
41        $queueDispatcher->createTask(new ResultItemVariableTransmissionTask(), [
42            AbstractResultTransmissionTask::EXECUTION_ID_PARAMETER_KEY => $event->getDeliveryExecutionId(),
43            AbstractResultTransmissionTask::VARIABLES_PARAMETER_KEY => $this->packVariables($event->getVariables()),
44            AbstractResultTransmissionTask::TRANSMISSION_ID_PARAMETER_KEY => $event->getTransmissionId(),
45            AbstractResultTransmissionTask::ITEM_URI_PARAMETER_KEY => $event->getItemUri(),
46            AbstractResultTransmissionTask::TEST_URI_PARAMETER_KEY => $event->getTestUri(),
47        ]);
48    }
49
50    public function transmitResultTestVariable(ResultTestVariablesTransmissionEvent $event): void
51    {
52        $queueDispatcher = $this->getQueueDispatcher();
53        $queueDispatcher->createTask(new ResultTestVariableTransmissionTask(), [
54            AbstractResultTransmissionTask::EXECUTION_ID_PARAMETER_KEY => $event->getDeliveryExecutionId(),
55            AbstractResultTransmissionTask::VARIABLES_PARAMETER_KEY => $this->packVariables($event->getVariables()),
56            AbstractResultTransmissionTask::TRANSMISSION_ID_PARAMETER_KEY => $event->getTransmissionId(),
57            AbstractResultTransmissionTask::TEST_URI_PARAMETER_KEY => $event->getTestUri(),
58        ]);
59    }
60
61    private function getQueueDispatcher(): QueueDispatcher
62    {
63        return ServiceManager::getServiceManager()->get(QueueDispatcher::SERVICE_ID);
64    }
65
66    private function packVariables(array $variables): array
67    {
68        return array_map(static function ($record) {
69            if (is_object($record)) {
70                return serialize($record);
71            }
72            return $record;
73        }, $variables);
74    }
75}