Extension.php 13 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
     * Initialize all ObjectStorage instances.
126 127 128
     */
    public function __construct()
    {
129 130
        $this->tags = new ObjectStorage();
        $this->versions = new ObjectStorage();
131 132 133 134 135 136 137
    }

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

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

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

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

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

    /**
     * Getter for crdate
     *
186
     * @return \DateTime crdate
187
     */
188
    public function getCrdate(): \DateTime
189 190 191 192 193 194 195
    {
        return $this->crdate;
    }

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

    /**
     * Adds a Tag
     *
206
     * @param \T3o\TerFe2\Domain\Model\Tag $tag The tag to be added
207
     */
208
    public function addTag(Tag $tag)
209 210 211 212 213 214 215
    {
        $this->tags->attach($tag);
    }

    /**
     * Removes a Tag
     *
216
     * @param \T3o\TerFe2\Domain\Model\Tag $tag The tag to be removed
217
     */
218
    public function removeTag(Tag $tag)
219 220 221 222 223 224 225
    {
        $this->tags->detach($tag);
    }

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

    /**
     * Get versions reverse sorted by version number
     *
     * @return array Versions
     */
238
    public function getReverseVersionsWithPositiveReviewsByVersionNumber(): array
239
    {
240
        $versions = [];
241 242 243 244 245 246 247 248

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

        krsort($versions);
249

250 251 252 253 254 255
        return $versions;
    }

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

        foreach ($this->versions as $version) {
            $versions[$version->getVersionNumber()] = $version;
        }
        krsort($versions);
268 269 270 271 272 273 274
        if ($limit > 0) {
            $versions = array_slice($versions, 0, $limit, true);
            if ($reverseAfterSlice) {
                $versions = array_reverse($versions);
            }
        }

275 276 277 278 279 280
        return $versions;
    }

    /**
     * Returns the count of versions
     *
281
     * @return int Version count
282
     */
283
    public function getVersionCount(): int
284
    {
285
        return !empty($this->versions) ? count($this->versions) : 0;
286 287 288 289 290
    }

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

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

        $versionCount = $this->getVersionCount();
        $this->versions->detach($version);
313
        if ($version === $this->lastVersion) {
314
            if ($versionCount === 1) {
Markus Sommer's avatar
Markus Sommer committed
315
                $this->lastVersion = null;
316 317 318 319 320 321 322 323 324
            } else {
                $this->lastVersion = array_shift($this->getReverseVersionsByVersionNumber());
            }
        }
    }

    /**
     * Setter for lastVersion, will only set if given version is newer than existing one
     *
325
     * @param \T3o\TerFe2\Domain\Model\Version $lastVersion lastVersion
326
     */
327
    public function setLastVersion(Version $lastVersion)
328
    {
329
        $this->lastVersion = $lastVersion;
330 331 332 333 334
    }

    /**
     * Getter for lastVersion
     *
335
     * @return \T3o\TerFe2\Domain\Model\Version lastVersion
336
     */
Thomas Löffler's avatar
Thomas Löffler committed
337
    public function getLastVersion()
338 339 340 341 342 343 344 345 346
    {
        return $this->lastVersion;
    }

    /**
     * Setter for frontendUser
     *
     * @param string $frontendUser Frontend user
     */
347
    public function setFrontendUser(string $frontendUser)
348 349 350 351 352 353 354 355 356
    {
        $this->frontendUser = $frontendUser;
    }

    /**
     * Getter for frontendUser
     *
     * @return string Frontend user
     */
357
    public function getFrontendUser(): string
358 359 360 361 362 363 364 365 366
    {
        return $this->frontendUser;
    }

    /**
     * Sets externalManual
     *
     * @param string $externalManual
     */
367
    public function setExternalManual(string $externalManual)
368 369 370 371 372 373 374 375 376
    {
        $this->externalManual = $externalManual;
    }

    /**
     * Gets externalManual
     *
     * @return string
     */
377
    public function getExternalManual(): string
378 379 380 381 382 383 384 385 386
    {
        return $this->externalManual;
    }

    /**
     * Sets paypalUrl
     *
     * @param string $paypalUrl
     */
387
    public function setPaypalUrl(string $paypalUrl)
388 389 390 391 392 393 394 395 396
    {
        $this->paypalUrl = $paypalUrl;
    }

    /**
     * Gets paypalUrl
     *
     * @return string
     */
397
    public function getPaypalUrl(): string
398 399 400 401 402 403 404 405 406
    {
        return $this->paypalUrl;
    }

    /**
     * Sets repositoryUrl
     *
     * @param string $repositoryUrl
     */
407
    public function setRepositoryUrl(string $repositoryUrl)
408 409 410 411 412 413 414 415 416
    {
        $this->repositoryUrl = $repositoryUrl;
    }

    /**
     * Gets repositoryUrl
     *
     * @return string
     */
417
    public function getRepositoryUrl(): string
418 419 420 421 422 423 424 425 426
    {
        return $this->repositoryUrl;
    }

    /**
     * Returns all votes for the extension
     *
     * @return array Vote counts
     */
427
    public function getVotes(): array
428
    {
429
        $votes = [
430 431
            'positive' => 0,
            'negative' => 0,
432
        ];
433 434 435 436 437 438 439

        foreach ($this->versions as $version) {
            $experiences = $version->getExperiences();
            if (!is_array($experiences)) {
                continue;
            }
            foreach ($experiences as $experience) {
440
                $rating = $experience->getRating();
441 442 443 444 445 446 447 448 449 450 451 452 453 454
                if ($rating > 0) {
                    $votes['positive'] += $rating;
                } else {
                    $votes['negative'] += $rating;
                }
            }
        }

        return $votes;
    }

    /**
     * Set all downloads sum
     *
455
     * @param int $downloads Count of downloads
456
     */
457
    public function setDownloads(int $downloads)
458
    {
459
        $this->downloads = $downloads;
460 461 462 463 464
    }

    /**
     * Get sum of all version downloads
     *
465
     * @return int All downloads
466
     */
467
    public function getDownloads(): int
468
    {
469
        return $this->downloads;
470 471 472 473 474
    }

    /**
     * Add downloads to all downloads sum
     *
475
     * @param int $downloads Count of downloads to add
476
     */
477
    public function addDownloads(int $downloads)
478
    {
479
        $this->downloads += $downloads;
480 481 482 483 484
    }

    /**
     * @return \DateTime
     */
485
    public function getExpire(): \DateTime
486
    {
487 488 489 490
        if ($this->expire === null) {
            $this->expire = new \DateTime();
            $this->expire->setTimestamp(0);
        }
491 492 493 494
        return $this->expire;
    }

    /**
495
     * @param \DateTime|null $expire
496
     */
497
    public function setExpire(\DateTime $expire = null)
498 499 500 501 502 503 504 505 506 507 508 509 510
    {
        $this->expire = $expire;
    }

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

        foreach ($versions as $version) {
511
            $downloads += $version->getAllDownloads();
512 513 514 515 516 517
        }

        if (!empty($downloads)) {
            $this->setDownloads($downloads);
        }
    }
518 519 520 521 522

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

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

549
        $versions = array_reverse($this->getReverseVersionsByVersionNumber());
550
        list($firstVersion) = $versions;
551 552 553

        if (!empty($firstVersion)) {
            $dateOfFirstUpload = date('d-m-Y', $firstVersion->getUploadDate());
554 555 556 557
        }

        return $dateOfFirstUpload;
    }
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573

    /**
     * @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
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602

    /**
     * @return int
     */
    public function getLikes()
    {
        return $this->likes;
    }

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

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

    public function decreaseLikes()
    {
        if ($this->likes > 0) {
            $this->likes--;
        }
    }
603
}