Extension.php 12.7 KB
Newer Older
1
<?php
2
3
namespace T3o\TerFe2\Domain\Model;

4
5
/*
 * This file is part of the TYPO3 CMS project.
6
 *
7
8
9
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
10
 *
11
12
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
15
 * The TYPO3 project - inspiring people to share!
 */
16

17
18
use TYPO3\CMS\Extbase\Persistence\ObjectStorage;

19
20
21
/**
 * Extension container
 */
22
class Extension extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity
23
24
25
{
    /**
     * Extension key
26
     *
27
28
     * @var string
     */
29
    protected $extKey = '';
30
31
32
33
34

    /**
     * Link to forge project
     *
     * @var string
35
     * @validate \T3o\TerFe2\Validation\Validator\UrlValidator
36
     */
37
    protected $forgeLink = '';
38
39
40

    /**
     * Tags
41
     *
42
     * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\T3o\TerFe2\Domain\Model\Tag>
43
     */
44
    protected $tags;
45
46
47

    /**
     * Versions
48
     *
49
     * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\T3o\TerFe2\Domain\Model\Version>
50
51
     * @lazy
     */
52
    protected $versions;
53
54
55

    /**
     * Last version
56
     *
57
     * @var \T3o\TerFe2\Domain\Model\Version
58
     */
59
    protected $lastVersion;
60
61
62

    /**
     * Frontend user
63
     *
64
65
     * @var string
     */
66
    protected $frontendUser = '';
67
68
69

    /**
     * Sum of all version downloads
70
     *
71
     * @var int
72
     */
73
    protected $downloads = 0;
74
75
76

    /**
     * Creation date
77
78
     *
     * @var \DateTime
79
     */
80
    protected $crdate;
81
82
83
84
85

    /**
     * Link to an external repository
     *
     * @var string
86
     * @validate \T3o\TerFe2\Validation\Validator\UrlValidator
87
     */
88
    protected $repositoryUrl = '';
89
90
91
92
93

    /**
     * Link to an external manual
     *
     * @var string
94
     * @validate \T3o\TerFe2\Validation\Validator\UrlValidator
95
     */
96
    protected $externalManual = '';
97
98
99
100
101

    /**
     * Link to paypal donation
     *
     * @var string
102
     * @validate \T3o\TerFe2\Validation\Validator\UrlValidator
103
     */
104
    protected $paypalUrl = '';
105
106
107
108

    /**
     * @var \DateTime
     */
109
    protected $expire;
110

111
112
113
114
115
116
117
118
    /**
     * Composer name of extension on packagist.org
     *
     * @var string
     * @validate \T3o\TerFe2\Validation\Validator\ComposerNameValidator
     */
    protected $composerName = '';

119
120

    /**
121
     * Initialize all ObjectStorage instances.
122
123
124
     */
    public function __construct()
    {
125
126
        $this->tags = new ObjectStorage();
        $this->versions = new ObjectStorage();
127
128
129
130
131
132
133
134
135
    }


    /**
     * Setter for extKey
     *
     * @param string $extKey extKey
     * @return void
     */
136
    public function setExtKey(string $extKey)
137
138
139
140
141
142
143
144
145
146
    {
        $this->extKey = $extKey;
    }


    /**
     * Getter for extKey
     *
     * @return string extKey
     */
147
    public function getExtKey(): string
148
149
150
151
152
153
154
155
156
157
158
    {
        return $this->extKey;
    }


    /**
     * Setter for forgeLink
     *
     * @param string $forgeLink forgeLink
     * @return void
     */
159
    public function setForgeLink(string $forgeLink)
160
161
162
163
164
165
166
167
168
169
    {
        $this->forgeLink = $forgeLink;
    }


    /**
     * Getter for forgeLink
     *
     * @return string forgeLink
     */
170
    public function getForgeLink(): string
171
172
173
174
175
176
177
    {
        return $this->forgeLink;
    }

    /**
     * Setter for crdate
     *
178
     * @param \DateTime $crdate creation date
179
180
     * @return void
     */
181
    public function setCrdate(\DateTime $crdate)
182
183
184
185
186
187
188
189
    {
        $this->crdate = $crdate;
    }


    /**
     * Getter for crdate
     *
190
     * @return \DateTime crdate
191
     */
192
    public function getCrdate(): \DateTime
193
194
195
196
197
198
199
    {
        return $this->crdate;
    }

    /**
     * Getter for tags
     *
200
     * @return \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\T3o\TerFe2\Domain\Model\Tag> Tags
201
     */
202
    public function getTags(): ObjectStorage
203
204
205
206
207
208
209
210
    {
        return $this->tags;
    }


    /**
     * Adds a Tag
     *
211
     * @param \T3o\TerFe2\Domain\Model\Tag $tag The tag to be added
212
213
     * @return void
     */
214
    public function addTag(Tag $tag)
215
216
217
218
219
220
221
222
    {
        $this->tags->attach($tag);
    }


    /**
     * Removes a Tag
     *
223
     * @param \T3o\TerFe2\Domain\Model\Tag $tag The tag to be removed
224
225
     * @return void
     */
226
    public function removeTag(Tag $tag)
227
228
229
230
231
232
233
234
    {
        $this->tags->detach($tag);
    }


    /**
     * Getter for versions
     *
235
     * @return Version[]|\TYPO3\CMS\Extbase\Persistence\ObjectStorage<\T3o\TerFe2\Domain\Model\Version>
236
     */
237
    public function getVersions(): ObjectStorage
238
239
240
241
242
243
244
245
246
247
    {
        return $this->versions;
    }


    /**
     * Get versions reverse sorted by version number
     *
     * @return array Versions
     */
248
    public function getReverseVersionsWithPositiveReviewsByVersionNumber(): array
249
    {
250
        $versions = [];
251
252
253
254
255
256
257
258

        foreach ($this->versions as $version) {
            if ($version->getReviewState() > -1) {
                $versions[$version->getVersionNumber()] = $version;
            }
        }

        krsort($versions);
259

260
261
262
263
264
265
        return $versions;
    }

    /**
     * Get versions reverse sorted by version number
     *
266
267
     * @param int $limit
     * @param bool $reverseAfterSlice
268
     * @return array|\T3o\TerFe2\Domain\Model\Version[] Versions
269
     */
270
    public function getReverseVersionsByVersionNumber(int $limit = 0, bool $reverseAfterSlice = true): array
271
    {
272
        $versions = [];
273
274
275
276
277

        foreach ($this->versions as $version) {
            $versions[$version->getVersionNumber()] = $version;
        }
        krsort($versions);
278
279
280
281
282
283
284
        if ($limit > 0) {
            $versions = array_slice($versions, 0, $limit, true);
            if ($reverseAfterSlice) {
                $versions = array_reverse($versions);
            }
        }

285
286
287
288
289
290
        return $versions;
    }

    /**
     * Returns the count of versions
     *
291
     * @return int Version count
292
     */
293
    public function getVersionCount(): int
294
295
296
297
298
299
300
301
    {
        return (!empty($this->versions) ? count($this->versions) : 0);
    }


    /**
     * Adds a Version
     *
302
     * @param \T3o\TerFe2\Domain\Model\Version $version The version to be added
303
304
     * @return void
     */
305
    public function addVersion(Version $version)
306
307
308
    {
        $this->versions->attach($version);
        $this->setLastVersion($version);
309
        $this->addDownloads($version->getAllDownloads());
310
311
312
313
314
315
    }


    /**
     * Removes a Version
     *
316
     * @param \T3o\TerFe2\Domain\Model\Version $version
317
     */
318
    public function removeVersion(Version $version)
319
320
    {
        if (!$this->versions->contains($version)) {
321
            throw new \UnexpectedValueException('Tried to remove a version which is not part of the extension.', 1339359060);
322
323
324
325
        }

        $versionCount = $this->getVersionCount();
        $this->versions->detach($version);
326
        if ($version === $this->lastVersion) {
327
            if ($versionCount === 1) {
Markus Sommer's avatar
Markus Sommer committed
328
                $this->lastVersion = null;
329
330
331
332
333
334
335
336
337
            } else {
                $this->lastVersion = array_shift($this->getReverseVersionsByVersionNumber());
            }
        }
    }

    /**
     * Setter for lastVersion, will only set if given version is newer than existing one
     *
338
     * @param \T3o\TerFe2\Domain\Model\Version $lastVersion lastVersion
339
340
     * @return void
     */
341
    public function setLastVersion(Version $lastVersion)
342
    {
343
        $this->lastVersion = $lastVersion;
344
345
346
347
348
349
    }


    /**
     * Getter for lastVersion
     *
350
     * @return \T3o\TerFe2\Domain\Model\Version lastVersion
351
     */
Thomas Löffler's avatar
Thomas Löffler committed
352
    public function getLastVersion()
353
354
355
356
357
358
359
360
361
362
    {
        return $this->lastVersion;
    }

    /**
     * Setter for frontendUser
     *
     * @param string $frontendUser Frontend user
     * @return void
     */
363
    public function setFrontendUser(string $frontendUser)
364
365
366
367
368
369
370
371
372
373
    {
        $this->frontendUser = $frontendUser;
    }


    /**
     * Getter for frontendUser
     *
     * @return string Frontend user
     */
374
    public function getFrontendUser(): string
375
376
377
378
379
380
381
382
383
    {
        return $this->frontendUser;
    }

    /**
     * Sets externalManual
     *
     * @param string $externalManual
     */
384
    public function setExternalManual(string $externalManual)
385
386
387
388
389
390
391
392
393
    {
        $this->externalManual = $externalManual;
    }

    /**
     * Gets externalManual
     *
     * @return string
     */
394
    public function getExternalManual(): string
395
396
397
398
399
400
401
402
403
    {
        return $this->externalManual;
    }

    /**
     * Sets paypalUrl
     *
     * @param string $paypalUrl
     */
404
    public function setPaypalUrl(string $paypalUrl)
405
406
407
408
409
410
411
412
413
    {
        $this->paypalUrl = $paypalUrl;
    }

    /**
     * Gets paypalUrl
     *
     * @return string
     */
414
    public function getPaypalUrl(): string
415
416
417
418
419
420
421
422
423
    {
        return $this->paypalUrl;
    }

    /**
     * Sets repositoryUrl
     *
     * @param string $repositoryUrl
     */
424
    public function setRepositoryUrl(string $repositoryUrl)
425
426
427
428
429
430
431
432
433
    {
        $this->repositoryUrl = $repositoryUrl;
    }

    /**
     * Gets repositoryUrl
     *
     * @return string
     */
434
    public function getRepositoryUrl(): string
435
436
437
438
439
440
441
442
443
    {
        return $this->repositoryUrl;
    }

    /**
     * Returns all votes for the extension
     *
     * @return array Vote counts
     */
444
    public function getVotes(): array
445
    {
446
        $votes = [
447
448
            'positive' => 0,
            'negative' => 0,
449
        ];
450
451
452
453
454
455
456

        foreach ($this->versions as $version) {
            $experiences = $version->getExperiences();
            if (!is_array($experiences)) {
                continue;
            }
            foreach ($experiences as $experience) {
457
                $rating = $experience->getRating();
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
                if ($rating > 0) {
                    $votes['positive'] += $rating;
                } else {
                    $votes['negative'] += $rating;
                }
            }
        }

        return $votes;
    }


    /**
     * Set all downloads sum
     *
473
     * @param int $downloads Count of downloads
474
475
     * @return void
     */
476
    public function setDownloads(int $downloads)
477
    {
478
        $this->downloads = $downloads;
479
480
481
482
483
    }

    /**
     * Get sum of all version downloads
     *
484
     * @return int All downloads
485
     */
486
    public function getDownloads(): int
487
    {
488
        return $this->downloads;
489
490
491
492
493
    }

    /**
     * Add downloads to all downloads sum
     *
494
     * @param int $downloads Count of downloads to add
495
496
     * @return void
     */
497
    public function addDownloads(int $downloads)
498
    {
499
        $this->downloads += $downloads;
500
501
502
503
504
    }

    /**
     * @return \DateTime
     */
505
    public function getExpire(): \DateTime
506
    {
507
508
509
510
        if ($this->expire === null) {
            $this->expire = new \DateTime();
            $this->expire->setTimestamp(0);
        }
511
512
513
514
        return $this->expire;
    }

    /**
515
     * @param \DateTime|null $expire
516
     */
517
    public function setExpire(\DateTime $expire = null)
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
    {
        $this->expire = $expire;
    }

    /**
     * Recalculate sum of all downloads
     *
     * @return void
     */
    public function recalculateDownloads()
    {
        $downloads = 0;
        $versions = $this->getVersions();

        foreach ($versions as $version) {
533
            $downloads += $version->getAllDownloads();
534
535
536
537
538
539
        }

        if (!empty($downloads)) {
            $this->setDownloads($downloads);
        }
    }
540
541
542
543
544

    /**
     * @param int $versionCount
     * @return string
     */
545
    public function getDownloadsByVersionsAsJson($versionCount = 6): string
546
547
548
549
550
    {
        if (empty($this->versions)) {
            return '';
        }
        $versions = [];
551
        foreach ($this->getReverseVersionsByVersionNumber($versionCount) as $version) {
552
553
554
555
556
557
558
559
560
561
562
            /** @var \T3o\TerFe2\Domain\Model\Version $version */
            $versions['release'][$version->getUploadDate()] = date('d-m-Y', $version->getUploadDate());
            $versions['versions'][] = $version->getVersionString();
            $versions['downloads'][] = $version->getAllDownloads();
        }
        $versions['release'] = array_splice($versions['release'], $versionCount * -1);
        $versions['versions'] = array_splice($versions['versions'], $versionCount * -1);
        $versions['downloads'] = array_splice($versions['downloads'], $versionCount * -1);

        return json_encode($versions);
    }
563
564
565
566
567
568

    /**
     * @return \DateTime|null
     */
    public function getDateOfFirstUpload()
    {
569
570
        $dateOfFirstUpload = null;

571
        $versions = array_reverse($this->getReverseVersionsByVersionNumber());
572
573
574
575
        list ($firstVersion) = $versions;

        if (!empty($firstVersion)) {
            $dateOfFirstUpload = date('d-m-Y', $firstVersion->getUploadDate());
576
577
578
579
        }

        return $dateOfFirstUpload;
    }
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595

    /**
     * @return string
     */
    public function getComposerName()
    {
        return $this->composerName;
    }

    /**
     * @param string $composerName
     */
    public function setComposerName($composerName)
    {
        $this->composerName = $composerName;
    }
596
}