Failed Conditions
Push — master ( 029e81...ce5fbf )
by Luca
06:29
created

Export   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 262
Duplicated Lines 0 %

Test Coverage

Coverage 92.31%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 23
eloc 51
dl 0
loc 262
ccs 60
cts 65
cp 0.9231
rs 10
c 1
b 0
f 0

22 Methods

Rating   Name   Duplication   Size   Complexity  
A getCardCount() 0 3 1
A markAsInProgress() 0 5 1
A getBackgroundColor() 0 3 1
A stats() 0 9 2
A isIncludeLegend() 0 3 1
A getMemory() 0 3 1
A getMaxHeight() 0 3 1
A setIncludeLegend() 0 3 1
A getPath() 0 3 1
A getStatus() 0 3 1
A setMaxHeight() 0 3 1
A getTextColor() 0 3 1
A markAsErrored() 0 5 1
A getFormat() 0 3 1
A __construct() 0 4 1
A setTextColor() 0 3 1
A getStart() 0 3 1
A setFormat() 0 3 1
A markAsDone() 0 4 1
A getDuration() 0 3 1
A getFilename() 0 3 1
A setBackgroundColor() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application\Model;
6
7
use Application\DBAL\Types\ExportFormatType;
8
use Application\DBAL\Types\ExportStatusType;
9
use Application\Traits\HasFileSize;
10
use Application\Traits\HasSite;
11
use Application\Traits\HasSiteInterface;
12
use Cake\Chronos\Chronos;
13
use Doctrine\Common\Collections\ArrayCollection;
14
use Doctrine\Common\Collections\Collection as DoctrineCollection;
15
use Doctrine\ORM\Mapping as ORM;
16
use GraphQL\Doctrine\Annotation as API;
17
use Throwable;
18
19
/**
20
 * An export of cards in various format
21
 *
22
 * @ORM\Entity(repositoryClass="Application\Repository\ExportRepository")
23
 */
24
class Export extends AbstractModel implements HasSiteInterface
25
{
26
    private const EXPORT_PATH = 'htdocs/export/';
27
28
    use HasSite;
29
    use HasFileSize;
30
31
    /**
32
     * Number of card exported.
33
     *
34
     * This is kept separated from cards and collection, because those could
35
     * be deleted and we want to keep the count of card forever.
36
     *
37
     * @var int
38
     *
39
     * @ORM\Column(type="integer", options={"default" = 0, "unsigned" = true})
40
     */
41
    private $cardCount = 0;
42
43
    /**
44
     * @var string
45
     *
46
     * @ORM\Column(type="string", length=2000, options={"default" = ""})
47
     */
48
    private $filename = '';
49
50
    /**
51
     * @var string
52
     *
53
     * @ORM\Column(type="ExportStatus", options={"default" = ExportStatusType::TODO})
54
     */
55
    private $status = ExportStatusType::TODO;
56
57
    /**
58
     * @var string
59
     *
60
     * @ORM\Column(type="ExportFormat", options={"default" = ExportFormatType::ZIP})
61
     */
62
    private $format = ExportFormatType::ZIP;
63
64
    /**
65
     * Max height of image. Zero means no max.
66
     *
67
     * @var int
68
     *
69
     * @ORM\Column(type="integer", options={"default" = 0, "unsigned" = true})
70
     */
71
    private $maxHeight = 0;
72
73
    /**
74
     * @var bool
75
     * @ORM\Column(type="boolean", options={"default" = true})
76
     */
77
    private $includeLegend = true;
78
79
    /**
80
     * @var string
81
     * @ORM\Column(type="string", options={"default" = "#FFFFFF"})
82
     */
83
    private $textColor = '#FFFFFF';
84
85
    /**
86
     * @var string
87
     * @ORM\Column(type="string", options={"default" = "#000000"})
88
     */
89
    private $backgroundColor = '#000000';
90
91
    /**
92
     * Start time of export process
93
     *
94
     * @var null|Chronos
95
     * @ORM\Column(type="datetime", nullable=true)
96
     */
97
    private $start;
98
99
    /**
100
     * Duration of export process in seconds
101
     *
102
     * @var null|int
103
     * @ORM\Column(type="integer", nullable=true, options={"unsigned" = true})
104
     */
105
    private $duration;
106
107
    /**
108
     * Peak memory usage in MiB
109
     *
110
     * @var null|int
111
     * @ORM\Column(type="integer", nullable=true, options={"unsigned" = true})
112
     */
113
    private $memory;
114
115
    /**
116
     * @ORM\Column(type="string", length=2000, options={"default" = ""})
117
     */
118
    private string $errorMessage = '';
119
120
    /**
121
     * The collections exported. This is only for informative purpose and only `cards`
122
     * contains the real cards that will be exported.
123
     *
124
     * @var DoctrineCollection
125
     *
126
     * @ORM\ManyToMany(targetEntity="Collection")
127
     */
128
    private $collections;
129
130
    /**
131
     * All cards to export, either picked one-by-one, or selected via a collection.
132
     *
133
     * @var DoctrineCollection
134
     *
135
     * @ORM\ManyToMany(targetEntity="Card")
136
     */
137
    private $cards;
138
139 5
    public function __construct()
140
    {
141 5
        $this->collections = new ArrayCollection();
142 5
        $this->cards = new ArrayCollection();
143 5
    }
144
145 1
    public function getCardCount(): int
146
    {
147 1
        return $this->cardCount;
148
    }
149
150
    /**
151
     * Get absolute path to export on disk
152
     *
153
     * @API\Exclude
154
     */
155 2
    public function getPath(): string
156
    {
157 2
        return realpath('.') . '/' . self::EXPORT_PATH . $this->getFilename();
158
    }
159
160 2
    public function getFilename(): string
161
    {
162 2
        return $this->filename;
163
    }
164
165
    /**
166
     * @API\Field(type="ExportStatus")
167
     */
168 2
    public function getStatus(): string
169
    {
170 2
        return $this->status;
171
    }
172
173 2
    public function markAsInProgress(string $filename): void
174
    {
175 2
        $this->status = ExportStatusType::IN_PROGRESS;
176 2
        $this->start = new Chronos();
177 2
        $this->filename = $filename;
178 2
    }
179
180 2
    public function markAsDone(): void
181
    {
182 2
        $this->status = ExportStatusType::DONE;
183 2
        $this->stats();
184 2
    }
185
186
    public function markAsErrored(Throwable $throwable): void
187
    {
188
        $this->status = ExportStatusType::ERRORED;
189
        $this->errorMessage = $throwable->getMessage();
190
        $this->stats();
191
    }
192
193 2
    private function stats(): void
194
    {
195 2
        $now = new Chronos();
196 2
        $this->duration = $now->getTimestamp() - $this->start->getTimestamp();
0 ignored issues
show
Bug introduced by
The method getTimestamp() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

196
        $this->duration = $now->getTimestamp() - $this->start->/** @scrutinizer ignore-call */ getTimestamp();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
197 2
        $this->memory = (int) round(memory_get_peak_usage() / 1024 / 1024);
198
199 2
        $path = $this->getPath();
200 2
        if (is_readable($path)) {
201 2
            $this->fileSize = filesize($path);
202
        }
203 2
    }
204
205
    /**
206
     * @API\Field(type="ExportFormat")
207
     */
208 3
    public function getFormat(): string
209
    {
210 3
        return $this->format;
211
    }
212
213
    /**
214
     * @API\Input(type="ExportFormat")
215
     */
216 2
    public function setFormat(string $format): void
217
    {
218 2
        $this->format = $format;
219 2
    }
220
221 3
    public function getMaxHeight(): int
222
    {
223 3
        return $this->maxHeight;
224
    }
225
226 2
    public function setMaxHeight(int $maxHeight): void
227
    {
228 2
        $this->maxHeight = $maxHeight;
229 2
    }
230
231 3
    public function isIncludeLegend(): bool
232
    {
233 3
        return $this->includeLegend;
234
    }
235
236 2
    public function setIncludeLegend(bool $includeLegend): void
237
    {
238 2
        $this->includeLegend = $includeLegend;
239 2
    }
240
241
    /**
242
     * @API\Field(type="Color")
243
     */
244 2
    public function getTextColor(): string
245
    {
246 2
        return $this->textColor;
247
    }
248
249
    /**
250
     * @API\Input(type="Color")
251
     */
252 2
    public function setTextColor(string $textColor): void
253
    {
254 2
        $this->textColor = $textColor;
255 2
    }
256
257
    /**
258
     * @API\Field(type="Color")
259
     */
260 2
    public function getBackgroundColor(): string
261
    {
262 2
        return $this->backgroundColor;
263
    }
264
265
    /**
266
     * @API\Input(type="Color")
267
     */
268 2
    public function setBackgroundColor(string $backgroundColor): void
269
    {
270 2
        $this->backgroundColor = $backgroundColor;
271 2
    }
272
273 1
    public function getStart(): ?Chronos
274
    {
275 1
        return $this->start;
276
    }
277
278 1
    public function getDuration(): ?int
279
    {
280 1
        return $this->duration;
281
    }
282
283 1
    public function getMemory(): ?int
284
    {
285 1
        return $this->memory;
286
    }
287
}
288