Extension.php 13.6 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 = '';

Thomas Löffler's avatar
Thomas Löffler committed
119 120 121 122 123
    /**
     * @var int
     */
    protected $likes = 0;

124 125 126 127 128
    /**
     * @var int
     */
    protected $notifications = 0;

129
    /**
130
     * Initialize all ObjectStorage instances.
131 132 133
     */
    public function __construct()
    {
134 135
        $this->tags = new ObjectStorage();
        $this->versions = new ObjectStorage();
136 137 138 139 140 141 142
    }

    /**
     * Setter for extKey
     *
     * @param string $extKey extKey
     */
143
    public function setExtKey(string $extKey)
144 145 146 147 148 149 150 151 152
    {
        $this->extKey = $extKey;
    }

    /**
     * Getter for extKey
     *
     * @return string extKey
     */
153
    public function getExtKey(): string
154 155 156 157 158 159 160 161 162
    {
        return $this->extKey;
    }

    /**
     * Setter for forgeLink
     *
     * @param string $forgeLink forgeLink
     */
163
    public function setForgeLink(string $forgeLink)
164 165 166 167 168 169 170 171 172
    {
        $this->forgeLink = $forgeLink;
    }

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

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

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

    /**
     * Getter for tags
     *
201
     * @return \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\T3o\TerFe2\Domain\Model\Tag> Tags
202
     */
203
    public function getTags(): ObjectStorage
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
    public function addTag(Tag $tag)
214 215 216 217 218 219 220
    {
        $this->tags->attach($tag);
    }

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

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

    /**
     * Get versions reverse sorted by version number
     *
     * @return array Versions
     */
243
    public function getReverseVersionsWithPositiveReviewsByVersionNumber(): array
244
    {
245
        $versions = [];
246 247 248 249 250 251 252 253

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

        krsort($versions);
254

255 256 257 258 259 260
        return $versions;
    }

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

        foreach ($this->versions as $version) {
            $versions[$version->getVersionNumber()] = $version;
        }
        krsort($versions);
273 274 275 276 277 278 279
        if ($limit > 0) {
            $versions = array_slice($versions, 0, $limit, true);
            if ($reverseAfterSlice) {
                $versions = array_reverse($versions);
            }
        }

280 281 282 283 284 285
        return $versions;
    }

    /**
     * Returns the count of versions
     *
286
     * @return int Version count
287
     */
288
    public function getVersionCount(): int
289
    {
290
        return !empty($this->versions) ? count($this->versions) : 0;
291 292 293 294 295
    }

    /**
     * Adds a Version
     *
296
     * @param \T3o\TerFe2\Domain\Model\Version $version The version to be added
297
     */
298
    public function addVersion(Version $version)
299 300 301
    {
        $this->versions->attach($version);
        $this->setLastVersion($version);
302
        $this->addDownloads($version->getAllDownloads());
303 304 305 306 307
    }

    /**
     * Removes a Version
     *
308
     * @param \T3o\TerFe2\Domain\Model\Version $version
309
     */
310
    public function removeVersion(Version $version)
311 312
    {
        if (!$this->versions->contains($version)) {
313
            throw new \UnexpectedValueException('Tried to remove a version which is not part of the extension.', 1339359060);
314 315 316 317
        }

        $versionCount = $this->getVersionCount();
        $this->versions->detach($version);
318
        if ($version === $this->lastVersion) {
319
            if ($versionCount === 1) {
Markus Sommer's avatar
Markus Sommer committed
320
                $this->lastVersion = null;
321
            } else {
322 323
                $reverseVersionsByVersionNumber = $this->getReverseVersionsByVersionNumber();
                $this->lastVersion = array_shift($reverseVersionsByVersionNumber);
324 325 326 327 328 329 330
            }
        }
    }

    /**
     * Setter for lastVersion, will only set if given version is newer than existing one
     *
331
     * @param \T3o\TerFe2\Domain\Model\Version $lastVersion lastVersion
332
     */
333
    public function setLastVersion(Version $lastVersion)
334
    {
335
        $this->lastVersion = $lastVersion;
336 337 338 339 340
    }

    /**
     * Getter for lastVersion
     *
341
     * @return \T3o\TerFe2\Domain\Model\Version lastVersion
342
     */
Thomas Löffler's avatar
Thomas Löffler committed
343
    public function getLastVersion()
344 345 346 347 348 349 350 351 352
    {
        return $this->lastVersion;
    }

    /**
     * Setter for frontendUser
     *
     * @param string $frontendUser Frontend user
     */
353
    public function setFrontendUser(string $frontendUser)
354 355 356 357 358 359 360 361 362
    {
        $this->frontendUser = $frontendUser;
    }

    /**
     * Getter for frontendUser
     *
     * @return string Frontend user
     */
363
    public function getFrontendUser(): string
364 365 366 367 368 369 370 371 372
    {
        return $this->frontendUser;
    }

    /**
     * Sets externalManual
     *
     * @param string $externalManual
     */
373
    public function setExternalManual(string $externalManual)
374 375 376 377 378 379 380 381 382
    {
        $this->externalManual = $externalManual;
    }

    /**
     * Gets externalManual
     *
     * @return string
     */
383
    public function getExternalManual(): string
384 385 386 387 388 389 390 391 392
    {
        return $this->externalManual;
    }

    /**
     * Sets paypalUrl
     *
     * @param string $paypalUrl
     */
393
    public function setPaypalUrl(string $paypalUrl)
394 395 396 397 398 399 400 401 402
    {
        $this->paypalUrl = $paypalUrl;
    }

    /**
     * Gets paypalUrl
     *
     * @return string
     */
403
    public function getPaypalUrl(): string
404 405 406 407 408 409 410 411 412
    {
        return $this->paypalUrl;
    }

    /**
     * Sets repositoryUrl
     *
     * @param string $repositoryUrl
     */
413
    public function setRepositoryUrl(string $repositoryUrl)
414 415 416 417 418 419 420 421 422
    {
        $this->repositoryUrl = $repositoryUrl;
    }

    /**
     * Gets repositoryUrl
     *
     * @return string
     */
423
    public function getRepositoryUrl(): string
424 425 426 427 428 429 430 431 432
    {
        return $this->repositoryUrl;
    }

    /**
     * Returns all votes for the extension
     *
     * @return array Vote counts
     */
433
    public function getVotes(): array
434
    {
435
        $votes = [
436 437
            'positive' => 0,
            'negative' => 0,
438
        ];
439 440 441 442 443 444 445

        foreach ($this->versions as $version) {
            $experiences = $version->getExperiences();
            if (!is_array($experiences)) {
                continue;
            }
            foreach ($experiences as $experience) {
446
                $rating = $experience->getRating();
447 448 449 450 451 452 453 454 455 456 457 458 459 460
                if ($rating > 0) {
                    $votes['positive'] += $rating;
                } else {
                    $votes['negative'] += $rating;
                }
            }
        }

        return $votes;
    }

    /**
     * Set all downloads sum
     *
461
     * @param int $downloads Count of downloads
462
     */
463
    public function setDownloads(int $downloads)
464
    {
465
        $this->downloads = $downloads;
466 467 468 469 470
    }

    /**
     * Get sum of all version downloads
     *
471
     * @return int All downloads
472
     */
473
    public function getDownloads(): int
474
    {
475
        return $this->downloads;
476 477 478 479 480
    }

    /**
     * Add downloads to all downloads sum
     *
481
     * @param int $downloads Count of downloads to add
482
     */
483
    public function addDownloads(int $downloads)
484
    {
485
        $this->downloads += $downloads;
486 487 488 489 490
    }

    /**
     * @return \DateTime
     */
491
    public function getExpire(): \DateTime
492
    {
493 494 495 496
        if ($this->expire === null) {
            $this->expire = new \DateTime();
            $this->expire->setTimestamp(0);
        }
497 498 499 500
        return $this->expire;
    }

    /**
501
     * @param \DateTime|null $expire
502
     */
503
    public function setExpire(\DateTime $expire = null)
504 505 506 507 508 509 510 511 512 513 514 515 516
    {
        $this->expire = $expire;
    }

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

        foreach ($versions as $version) {
517
            $downloads += $version->getAllDownloads();
518 519 520 521 522 523
        }

        if (!empty($downloads)) {
            $this->setDownloads($downloads);
        }
    }
524 525 526 527 528

    /**
     * @param int $versionCount
     * @return string
     */
529
    public function getDownloadsByVersionsAsJson($versionCount = 6): string
530 531 532 533 534
    {
        if (empty($this->versions)) {
            return '';
        }
        $versions = [];
535
        foreach ($this->getReverseVersionsByVersionNumber($versionCount) as $version) {
536 537 538 539 540 541 542 543 544 545 546
            /** @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);
    }
547 548 549 550 551 552

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

555
        $versions = array_reverse($this->getReverseVersionsByVersionNumber());
556
        list($firstVersion) = $versions;
557 558 559

        if (!empty($firstVersion)) {
            $dateOfFirstUpload = date('d-m-Y', $firstVersion->getUploadDate());
560 561 562 563
        }

        return $dateOfFirstUpload;
    }
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579

    /**
     * @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
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607

    /**
     * @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--;
        }
    }
608 609 610 611 612 613 614 615 616 617

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

    public function setNotifications(int $notifications): void
    {
        $this->notifications = $notifications;
    }
618 619 620 621 622 623 624 625 626 627

    public function getMatrixOfSupportedTypo3Versions(): array
    {
        $supportedTypo3Versions = [];
        foreach ($this->versions as $version) {
            $version->getMatrixOfSupportedTypo3Versions($supportedTypo3Versions);
        }

        return $supportedTypo3Versions;
    }
628
}