Extension.php 19.2 KB
Newer Older
1
<?php
Tomas Norre Mikkelsen's avatar
Tomas Norre Mikkelsen committed
2

3
4
namespace T3o\TerFe2\Domain\Model;

5
6
/*
 * This file is part of the TYPO3 CMS project.
7
 *
8
9
10
 * 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.
11
 *
12
13
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
16
 * The TYPO3 project - inspiring people to share!
 */
17

18
19
20
use T3o\TerFe2\Utility\VersionUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\VersionNumberUtility;
21
use T3o\TerFe2\Domain\Repository\DownloadRepository;
22
use T3o\TerFe2\Utility\VersionUtility;
23
24
use TYPO3\CMS\Extbase\Persistence\ObjectStorage;

25
26
27
/**
 * Extension container
 */
28
class Extension extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity
29
30
31
{
    /**
     * Extension key
32
     *
33
34
     * @var string
     */
35
    protected $extKey = '';
36
37
38
39
40

    /**
     * Link to forge project
     *
     * @var string
41
     * @TYPO3\CMS\Extbase\Annotation\Validate("\T3o\TerFe2\Validation\Validator\UrlValidator")
42
     */
43
    protected $forgeLink = '';
44
45
46

    /**
     * Tags
47
     *
48
     * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\T3o\TerFe2\Domain\Model\Tag>
49
     */
50
    protected $tags;
51
52
53

    /**
     * Versions
54
     *
55
     * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\T3o\TerFe2\Domain\Model\Version>
56
     * @TYPO3\CMS\Extbase\Annotation\ORM\Lazy
57
     */
58
    protected $versions;
59
60
61

    /**
     * Last version
62
     *
63
     * @var \T3o\TerFe2\Domain\Model\Version
64
     */
65
    protected $lastVersion;
66
67
68

    /**
     * Frontend user
69
     *
70
71
     * @var string
     */
72
    protected $frontendUser = '';
73
74
75

    /**
     * Sum of all version downloads
76
     *
77
     * @var int
78
     */
79
    protected $downloads = 0;
80
81
82

    /**
     * Creation date
83
84
     *
     * @var \DateTime
85
     */
86
    protected $crdate;
87
88
89
90
91

    /**
     * Link to an external repository
     *
     * @var string
92
     * @TYPO3\CMS\Extbase\Annotation\Validate("\T3o\TerFe2\Validation\Validator\UrlValidator")
93
     */
94
    protected $repositoryUrl = '';
95
96
97
98
99

    /**
     * Link to an external manual
     *
     * @var string
100
     * @TYPO3\CMS\Extbase\Annotation\Validate("\T3o\TerFe2\Validation\Validator\UrlValidator")
101
     */
102
    protected $externalManual = '';
103
104
105
106
107

    /**
     * Link to paypal donation
     *
     * @var string
108
     * @TYPO3\CMS\Extbase\Annotation\Validate("\T3o\TerFe2\Validation\Validator\UrlValidator")
109
     */
110
    protected $paypalUrl = '';
111

112
113
114
115
116
    /**
     * @var string
     */
    protected $securityTeamNotice = '';

117
118
119
    /**
     * @var \DateTime
     */
120
    protected $expire;
121

122
123
124
125
    /**
     * Composer name of extension on packagist.org
     *
     * @var string
126
     * @TYPO3\CMS\Extbase\Annotation\Validate("\T3o\TerFe2\Validation\Validator\ComposerNameValidator")
127
128
129
     */
    protected $composerName = '';

Thomas Löffler's avatar
Thomas Löffler committed
130
131
132
133
134
    /**
     * @var int
     */
    protected $likes = 0;

135
136
137
138
139
    /**
     * @var int
     */
    protected $notifications = 0;

140
141
142
143
144
145
146
147
148
149
    /**
     * @var string
     */
    protected $localizationStatus = '';

    /**
     * @var string
     */
    protected $crowdinKey = '';

150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
    /**
     * @var int
     */
    protected $lastDownloadSync;

    /**
     * @var DownloadRepository
     */
    protected $downloadRepository;

    public function injectDownloadRepository(DownloadRepository $downloadRepository)
    {
        $this->downloadRepository = $downloadRepository;
    }

165
    /**
166
     * Initialize all ObjectStorage instances.
167
168
169
     */
    public function __construct()
    {
170
171
        $this->tags = new ObjectStorage();
        $this->versions = new ObjectStorage();
172
173
174
175
176
177
178
    }

    /**
     * Setter for extKey
     *
     * @param string $extKey extKey
     */
179
    public function setExtKey(string $extKey)
180
181
182
183
184
185
186
187
188
    {
        $this->extKey = $extKey;
    }

    /**
     * Getter for extKey
     *
     * @return string extKey
     */
189
    public function getExtKey(): string
190
191
192
193
194
195
196
197
198
    {
        return $this->extKey;
    }

    /**
     * Setter for forgeLink
     *
     * @param string $forgeLink forgeLink
     */
199
    public function setForgeLink(string $forgeLink)
200
201
202
203
204
205
206
207
208
    {
        $this->forgeLink = $forgeLink;
    }

    /**
     * Getter for forgeLink
     *
     * @return string forgeLink
     */
209
    public function getForgeLink(): string
210
211
212
213
214
215
216
    {
        return $this->forgeLink;
    }

    /**
     * Setter for crdate
     *
217
     * @param \DateTime $crdate creation date
218
     */
219
    public function setCrdate(\DateTime $crdate)
220
221
222
223
224
225
226
    {
        $this->crdate = $crdate;
    }

    /**
     * Getter for crdate
     *
227
     * @return \DateTime crdate
228
     */
229
    public function getCrdate(): \DateTime
230
231
232
233
234
235
236
    {
        return $this->crdate;
    }

    /**
     * Getter for tags
     *
237
     * @return \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\T3o\TerFe2\Domain\Model\Tag> Tags
238
     */
239
    public function getTags(): ObjectStorage
240
241
242
243
244
245
246
    {
        return $this->tags;
    }

    /**
     * Adds a Tag
     *
247
     * @param \T3o\TerFe2\Domain\Model\Tag $tag The tag to be added
248
     */
249
    public function addTag(Tag $tag)
250
251
252
253
254
255
256
    {
        $this->tags->attach($tag);
    }

    /**
     * Removes a Tag
     *
257
     * @param \T3o\TerFe2\Domain\Model\Tag $tag The tag to be removed
258
     */
259
    public function removeTag(Tag $tag)
260
261
262
263
264
265
266
    {
        $this->tags->detach($tag);
    }

    /**
     * Getter for versions
     *
267
     * @return Version[]|\TYPO3\CMS\Extbase\Persistence\ObjectStorage<\T3o\TerFe2\Domain\Model\Version>
268
     */
269
    public function getVersions(): ObjectStorage
270
271
272
273
274
275
276
277
278
    {
        return $this->versions;
    }

    /**
     * Get versions reverse sorted by version number
     *
     * @return array Versions
     */
279
    public function getReverseVersionsWithPositiveReviewsByVersionNumber(): array
280
    {
281
        $versions = [];
282
283
284
285
286
287
288
289

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

        krsort($versions);
290

291
292
293
294
295
296
        return $versions;
    }

    /**
     * Get versions reverse sorted by version number
     *
297
298
     * @param int $limit
     * @param bool $reverseAfterSlice
299
     * @return array|\T3o\TerFe2\Domain\Model\Version[] Versions
300
     */
301
    public function getReverseVersionsByVersionNumber(int $limit = 0, bool $reverseAfterSlice = true): array
302
    {
303
        $versions = [];
304
305
306
307
308

        foreach ($this->versions as $version) {
            $versions[$version->getVersionNumber()] = $version;
        }
        krsort($versions);
309
310
311
312
313
314
315
        if ($limit > 0) {
            $versions = array_slice($versions, 0, $limit, true);
            if ($reverseAfterSlice) {
                $versions = array_reverse($versions);
            }
        }

316
317
318
319
320
321
        return $versions;
    }

    /**
     * Returns the count of versions
     *
322
     * @return int Version count
323
     */
324
    public function getVersionCount(): int
325
    {
326
        return count($this->versions);
327
328
329
330
331
    }

    /**
     * Adds a Version
     *
332
     * @param \T3o\TerFe2\Domain\Model\Version $version The version to be added
333
     */
334
    public function addVersion(Version $version)
335
336
337
    {
        $this->versions->attach($version);
        $this->setLastVersion($version);
338
        $this->addDownloads($version->getAllDownloads());
339
340
341
342
343
    }

    /**
     * Removes a Version
     *
344
     * @param \T3o\TerFe2\Domain\Model\Version $version
345
     */
346
    public function removeVersion(Version $version)
347
348
    {
        if (!$this->versions->contains($version)) {
349
            throw new \UnexpectedValueException('Tried to remove a version which is not part of the extension.', 1339359060);
350
351
352
353
        }

        $versionCount = $this->getVersionCount();
        $this->versions->detach($version);
354
        if ($version === $this->lastVersion) {
355
            if ($versionCount === 1) {
Markus Sommer's avatar
Markus Sommer committed
356
                $this->lastVersion = null;
357
            } else {
358
359
                $reverseVersionsByVersionNumber = $this->getReverseVersionsByVersionNumber();
                $this->lastVersion = array_shift($reverseVersionsByVersionNumber);
360
361
362
363
364
365
366
            }
        }
    }

    /**
     * Setter for lastVersion, will only set if given version is newer than existing one
     *
367
     * @param \T3o\TerFe2\Domain\Model\Version $lastVersion lastVersion
368
     */
369
    public function setLastVersion(Version $lastVersion)
370
    {
371
        $this->lastVersion = $lastVersion;
372
373
374
375
376
    }

    /**
     * Getter for lastVersion
     *
377
     * @return \T3o\TerFe2\Domain\Model\Version lastVersion
378
     */
Thomas Löffler's avatar
Thomas Löffler committed
379
    public function getLastVersion()
380
381
382
383
384
385
386
387
388
    {
        return $this->lastVersion;
    }

    /**
     * Setter for frontendUser
     *
     * @param string $frontendUser Frontend user
     */
389
    public function setFrontendUser(string $frontendUser)
390
391
392
393
394
395
396
397
398
    {
        $this->frontendUser = $frontendUser;
    }

    /**
     * Getter for frontendUser
     *
     * @return string Frontend user
     */
399
    public function getFrontendUser(): string
400
401
402
403
404
405
406
407
408
    {
        return $this->frontendUser;
    }

    /**
     * Sets externalManual
     *
     * @param string $externalManual
     */
409
    public function setExternalManual(string $externalManual)
410
411
412
413
414
415
416
417
418
    {
        $this->externalManual = $externalManual;
    }

    /**
     * Gets externalManual
     *
     * @return string
     */
419
    public function getExternalManual(): string
420
421
422
423
424
425
426
427
428
    {
        return $this->externalManual;
    }

    /**
     * Sets paypalUrl
     *
     * @param string $paypalUrl
     */
429
    public function setPaypalUrl(string $paypalUrl)
430
431
432
433
434
435
436
437
438
    {
        $this->paypalUrl = $paypalUrl;
    }

    /**
     * Gets paypalUrl
     *
     * @return string
     */
439
    public function getPaypalUrl(): string
440
441
442
443
444
445
446
447
448
    {
        return $this->paypalUrl;
    }

    /**
     * Sets repositoryUrl
     *
     * @param string $repositoryUrl
     */
449
    public function setRepositoryUrl(string $repositoryUrl)
450
451
452
453
454
455
456
457
458
    {
        $this->repositoryUrl = $repositoryUrl;
    }

    /**
     * Gets repositoryUrl
     *
     * @return string
     */
459
    public function getRepositoryUrl(): string
460
461
462
463
464
465
466
467
468
    {
        return $this->repositoryUrl;
    }

    /**
     * Returns all votes for the extension
     *
     * @return array Vote counts
     */
469
    public function getVotes(): array
470
    {
471
        $votes = [
472
473
            'positive' => 0,
            'negative' => 0,
474
        ];
475
476
477
478
479
480
481

        foreach ($this->versions as $version) {
            $experiences = $version->getExperiences();
            if (!is_array($experiences)) {
                continue;
            }
            foreach ($experiences as $experience) {
482
                $rating = $experience->getRating();
483
484
485
486
487
488
489
490
491
492
493
494
495
496
                if ($rating > 0) {
                    $votes['positive'] += $rating;
                } else {
                    $votes['negative'] += $rating;
                }
            }
        }

        return $votes;
    }

    /**
     * Set all downloads sum
     *
497
     * @param int $downloads Count of downloads
498
     */
499
    public function setDownloads(int $downloads)
500
    {
501
        $this->downloads = $downloads;
502
503
504
505
506
    }

    /**
     * Get sum of all version downloads
     *
507
     * @return int All downloads
508
     */
509
    public function getDownloads(): int
510
    {
511
        return $this->downloads;
512
513
514
515
516
    }

    /**
     * Add downloads to all downloads sum
     *
517
     * @param int $downloads Count of downloads to add
518
     */
519
    public function addDownloads(int $downloads)
520
    {
521
        $this->downloads += $downloads;
522
523
524
525
526
    }

    /**
     * @return \DateTime
     */
527
    public function getExpire(): \DateTime
528
    {
529
530
531
532
        if ($this->expire === null) {
            $this->expire = new \DateTime();
            $this->expire->setTimestamp(0);
        }
533
534
535
536
        return $this->expire;
    }

    /**
537
     * @param \DateTime|null $expire
538
     */
539
    public function setExpire(\DateTime $expire = null)
540
    {
541
542
543
544
        if ($expire === null) {
            $expire = new \DateTime();
            $expire->setTimestamp(0);
        }
545
546
547
548
549
550
551
552
553
554
555
556
        $this->expire = $expire;
    }

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

        foreach ($versions as $version) {
557
            $downloads += $version->getAllDownloads();
558
559
560
561
562
563
        }

        if (!empty($downloads)) {
            $this->setDownloads($downloads);
        }
    }
564
565
566
567
568

    /**
     * @param int $versionCount
     * @return string
     */
569
    public function getDownloadsByVersionsAsJson($versionCount = 6): string
570
571
572
573
574
    {
        if (empty($this->versions)) {
            return '';
        }
        $versions = [];
575
        foreach ($this->getReverseVersionsByVersionNumber($versionCount) as $version) {
576
577
578
579
580
581
582
583
584
585
586
            /** @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);
    }
587

588
589
590
591
592
    /**
     * @return string
     */
    public function getDownloadsByTimeIntervalAsJson(): string
    {
Tomas Norre Mikkelsen's avatar
Tomas Norre Mikkelsen committed
593
        if (empty($this->versions)) {
594
595
596
597
598
599
            return '';
        }

        $intervals = [];

        // The for loop is turned around to ensure the sorting order in the multi dimensional array.
Tomas Norre Mikkelsen's avatar
Tomas Norre Mikkelsen committed
600
601
        for ($i = 5; $i >= 0; $i--) {
            $time = strtotime('-' . $i . 'month');
602
            $month = date('Ym', $time);
603
604
605
606
607
608
609
610
611
612
613
614
615
            $downloadsPackagist = $this->downloadRepository->findDownloadCounterByMonthAndExtensionKeyAndSource($month, $this->getExtKey(), VersionUtility::DOWNLOAD_SOURCE_PACKAGIST);
            $downloadsTer = $this->downloadRepository->findDownloadCounterByMonthAndExtensionKeyAndSource($month, $this->getExtKey(), VersionUtility::DOWNLOAD_SOURCE_TER);
            $downloadsEm = $this->downloadRepository->findDownloadCounterByMonthAndExtensionKeyAndSource($month, $this->getExtKey(), VersionUtility::DOWNLOAD_SOURCE_EM);

            // How to define the start date for the feature?
            if ($downloadsPackagist === 0 && $downloadsTer === 0 && $downloadsEm === 0){
                continue;
            }

            $intervals['release'][] = date('M. Y', $time);
            $intervals['downloads_packagist'][] = $downloadsPackagist;
            $intervals['downloads_ter'][] = $downloadsTer;
            $intervals['downloads_em'][] = $downloadsEm;
616
617
618
619
620
621
            $intervals['versions'][] = '';
        }

        return json_encode($intervals);
    }

622
623
624
625
626
    /**
     * @return \DateTime|null
     */
    public function getDateOfFirstUpload()
    {
627
628
        $dateOfFirstUpload = null;

629
        $versions = array_reverse($this->getReverseVersionsByVersionNumber());
630
        [$firstVersion] = $versions;
631
632
633

        if (!empty($firstVersion)) {
            $dateOfFirstUpload = date('d-m-Y', $firstVersion->getUploadDate());
634
635
636
637
        }

        return $dateOfFirstUpload;
    }
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653

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

    /**
     * @param string $composerName
     */
    public function setComposerName($composerName)
    {
        $this->composerName = $composerName;
    }
Thomas Löffler's avatar
Thomas Löffler committed
654

655
656
657
    /**
     * @return string
     */
658
    public function getPackagistPage(): string
659
    {
660
661
662
        if (empty($this->composerName)) {
            return '';
        }
663
664
665
        return 'https://packagist.org/packages/' . $this->composerName;
    }

Thomas Löffler's avatar
Thomas Löffler committed
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
    /**
     * @return int
     */
    public function getLikes()
    {
        return $this->likes;
    }

    /**
     * @param int $likes
     */
    public function setLikes(int $likes)
    {
        $this->likes = $likes;
    }

    public function increaseLikes()
    {
        $this->likes++;
    }

    public function decreaseLikes()
    {
        if ($this->likes > 0) {
            $this->likes--;
        }
    }
693
694
695
696
697
698
699
700
701
702

    public function getNotifications(): int
    {
        return $this->notifications;
    }

    public function setNotifications(int $notifications): void
    {
        $this->notifications = $notifications;
    }
703

704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
    /**
     * @return string
     */
    public function getSecurityTeamNotice(): string
    {
        return $this->securityTeamNotice;
    }

    /**
     * @param string $securityTeamNotice
     */
    public function setSecurityTeamNotice(string $securityTeamNotice)
    {
        $this->securityTeamNotice = $securityTeamNotice;
    }

720
721
722
723
724
725
726
727
728
    public function getMatrixOfSupportedTypo3Versions(): array
    {
        $supportedTypo3Versions = [];
        foreach ($this->versions as $version) {
            $version->getMatrixOfSupportedTypo3Versions($supportedTypo3Versions);
        }

        return $supportedTypo3Versions;
    }
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763

    /**
     * @return array
     */
    public function getLocalizationStatus(): array
    {
        if (!empty($this->localizationStatus)) {
            return json_decode($this->localizationStatus, true);
        }
        return [];
    }

    /**
     * @param string $localizationStatus
     */
    public function setLocalizationStatus(string $localizationStatus)
    {
        $this->localizationStatus = $localizationStatus;
    }

    /**
     * @return string
     */
    public function getCrowdinKey(): string
    {
        return $this->crowdinKey;
    }

    /**
     * @param string $crowdinKey
     */
    public function setCrowdinKey(string $crowdinKey)
    {
        $this->crowdinKey = $crowdinKey;
    }
764

765
    public function getAdditionalSupportVersions(): array
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
    {
        $supportDevVersion = [];
        $supportELTSVersions = [];
        $supportOlderVersions = [];

        $ltsVersionService = GeneralUtility::makeInstance(\T3o\TerFe2\Service\LTSVersionService::class);
        if ($ltsVersionService->getLatestDevelopmentVersion(true) > VersionNumberUtility::convertVersionNumberToInteger($ltsVersionService->coreData['latest_lts'])
            && VersionUtility::doesExtensionSupportTypo3Version($this, $ltsVersionService->getLatestDevelopmentVersion(true))) {
            $supportDevVersion[] = ['label' => $ltsVersionService->getLatestDevelopmentVersion() . '-dev', 'badgeClass' => 'primary'];
        }
        foreach ($ltsVersionService->getELTSVersions() as $versionNumber) {
            if (VersionUtility::doesExtensionSupportTypo3Version($this, $versionNumber)) {
                $supportELTSVersions[] = ['label' => $ltsVersionService->getVersionLabelOfRelease($versionNumber) . ' LTS', 'badgeClass' => 'warning'];
            }
        }

        foreach ($ltsVersionService->getOldLTSVersions() as $versionNumber) {
            if (VersionUtility::doesExtensionSupportTypo3Version($this, $versionNumber)) {
                $supportOlderVersions[] = ['label' => $ltsVersionService->getVersionLabelOfRelease($versionNumber) . ' LTS', 'badgeClass' => 'danger'];
            }
        }

788
        return array_merge($supportOlderVersions, $supportELTSVersions, $supportDevVersion);
789
    }
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805

    /**
     * @return int
     */
    public function getLastDownloadSync(): int
    {
        return $this->lastDownloadSync;
    }

    /**
     * @param int $lastDownloadSync
     */
    public function setLastDownloadSync(int $lastDownloadSync)
    {
        $this->lastDownloadSync = $lastDownloadSync;
    }
806
}