Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
89.13% covered (warning)
89.13%
41 / 46
94.12% covered (success)
94.12%
16 / 17
CRAP
0.00% covered (danger)
0.00%
0 / 1
ItemImportResult
89.13% covered (warning)
89.13%
41 / 46
94.12% covered (success)
94.12%
16 / 17
26.87
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 addItem
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getFirstItem
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setFirstItem
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setTotalSuccessfulImport
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setTotalScannedItems
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getTotalSuccessfulImport
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getTotalScannedItems
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getItems
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getTotalWarnings
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getTotalErrors
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getWarnings
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getErrors
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getErrorsAndWarnings
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 addException
37.50% covered (danger)
37.50%
3 / 8
0.00% covered (danger)
0.00%
0 / 1
5.20
 addAggregatedException
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 addInternalException
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 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 (under the project TAO-PRODUCT);
19 */
20
21declare(strict_types=1);
22
23namespace oat\taoQtiItem\model\import;
24
25use core_kernel_classes_Resource;
26use oat\taoQtiItem\model\import\Validator\AbstractValidationException;
27use oat\taoQtiItem\model\import\Validator\AggregatedValidationException;
28use oat\taoQtiItem\model\import\Validator\ErrorValidationException;
29use oat\taoQtiItem\model\import\Validator\WarningValidationException;
30use Throwable;
31
32class ItemImportResult
33{
34    /** @var ItemInterface[] */
35    private $items;
36
37    /** @var ErrorValidationException[] */
38    private $errors;
39
40    /** @var WarningValidationException[] */
41    private $warnings;
42
43    /** @var int*/
44    private $totalWarnings;
45
46    /** @var int */
47    private $totalErrors;
48
49    /** @var ErrorValidationException[]|WarningValidationException[] */
50    private $errorsAndWarnings;
51
52    /** @var int */
53    private $totalSuccessfulImport;
54
55    /** @var int */
56    private $totalScannedItems;
57
58    /** @var core_kernel_classes_Resource|null */
59    private $firstItem;
60
61    public function __construct()
62    {
63        $this->items = [];
64        $this->errors = [];
65        $this->warnings = [];
66        $this->totalErrors = 0;
67        $this->totalWarnings = 0;
68        $this->errorsAndWarnings = [];
69        $this->totalSuccessfulImport = 0;
70        $this->totalScannedItems = 0;
71    }
72
73    public function addItem(int $line, ItemInterface $item): self
74    {
75        $this->items[$line] = $item;
76
77        return $this;
78    }
79
80    public function getFirstItem(): ?core_kernel_classes_Resource
81    {
82        return $this->firstItem;
83    }
84
85    public function setFirstItem(core_kernel_classes_Resource $firstItem): void
86    {
87        $this->firstItem = $firstItem;
88    }
89
90    public function setTotalSuccessfulImport(int $totalSuccessfulImport): void
91    {
92        $this->totalSuccessfulImport = $totalSuccessfulImport;
93    }
94
95    public function setTotalScannedItems(int $totalScannedItems): void
96    {
97        $this->totalScannedItems = $totalScannedItems;
98    }
99
100    public function getTotalSuccessfulImport(): int
101    {
102        return $this->totalSuccessfulImport;
103    }
104
105    public function getTotalScannedItems(): int
106    {
107        return $this->totalScannedItems;
108    }
109
110    /**
111     * @return ItemInterface[]
112     */
113    public function getItems(): array
114    {
115        return $this->items;
116    }
117
118    public function getTotalWarnings(): int
119    {
120        return $this->totalWarnings;
121    }
122
123    public function getTotalErrors(): int
124    {
125        return $this->totalErrors;
126    }
127
128    /**
129     * @return WarningValidationException[][]
130     */
131    public function getWarnings(): array
132    {
133        return $this->warnings;
134    }
135
136    /**
137     * @return ErrorValidationException[][]
138     */
139    public function getErrors(): array
140    {
141        return $this->errors;
142    }
143
144    /**
145     * @return ErrorValidationException[][]|WarningValidationException[][]
146     */
147    public function getErrorsAndWarnings(): array
148    {
149        return $this->errorsAndWarnings;
150    }
151
152    public function addException(int $line, Throwable $exception): self
153    {
154        if ($exception instanceof AggregatedValidationException) {
155            $this->addAggregatedException($line, $exception);
156
157            return $this;
158        }
159
160        if ($exception instanceof AbstractValidationException) {
161            $this->addInternalException($line, $exception);
162
163            return $this;
164        }
165
166        $this->addInternalException($line, new ErrorValidationException($exception->getMessage()));
167
168        return $this;
169    }
170
171    private function addAggregatedException(int $line, AggregatedValidationException $exception): self
172    {
173        foreach ($exception->getErrors() as $errorException) {
174            $this->addInternalException($line, $errorException);
175        }
176
177        foreach ($exception->getWarnings() as $warningException) {
178            $this->addInternalException($line, $warningException);
179        }
180
181        return $this;
182    }
183
184    private function addInternalException(int $line, AbstractValidationException $exception): self
185    {
186        $this->errorsAndWarnings[$line] = isset($this->errorsAndWarnings[$line]) ? $this->errorsAndWarnings[$line] : [];
187        $this->errorsAndWarnings[$line][] = $exception;
188
189        if ($exception instanceof ErrorValidationException) {
190            $this->totalErrors++;
191
192            $this->errors[$line] = isset($this->errors[$line]) ? $this->errors[$line] : [];
193            $this->errors[$line][] = $exception;
194        }
195
196        if ($exception instanceof WarningValidationException) {
197            $this->totalWarnings++;
198
199            $this->warnings[$line] = isset($this->warnings[$line]) ? $this->warnings[$line] : [];
200            $this->warnings[$line][] = $exception;
201        }
202
203        return $this;
204    }
205}