*/ protected $tags; /** * Versions * * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\T3o\TerFe2\Domain\Model\Version> * @lazy */ protected $versions; /** * Last version * * @var \T3o\TerFe2\Domain\Model\Version */ protected $lastVersion; /** * Frontend user * * @var string */ protected $frontendUser = ''; /** * Sum of all version downloads * * @var int */ protected $downloads = 0; /** * Creation date * * @var \DateTime */ protected $crdate; /** * Link to an external repository * * @var string * @validate \T3o\TerFe2\Validation\Validator\UrlValidator */ protected $repositoryUrl = ''; /** * Link to an external manual * * @var string * @validate \T3o\TerFe2\Validation\Validator\UrlValidator */ protected $externalManual = ''; /** * Link to paypal donation * * @var string * @validate \T3o\TerFe2\Validation\Validator\UrlValidator */ protected $paypalUrl = ''; /** * @var \DateTime */ protected $expire; /** * Composer name of extension on packagist.org * * @var string * @validate \T3o\TerFe2\Validation\Validator\ComposerNameValidator */ protected $composerName = ''; /** * @var int */ protected $likes = 0; /** * Initialize all ObjectStorage instances. */ public function __construct() { $this->tags = new ObjectStorage(); $this->versions = new ObjectStorage(); } /** * Setter for extKey * * @param string $extKey extKey */ public function setExtKey(string $extKey) { $this->extKey = $extKey; } /** * Getter for extKey * * @return string extKey */ public function getExtKey(): string { return $this->extKey; } /** * Setter for forgeLink * * @param string $forgeLink forgeLink */ public function setForgeLink(string $forgeLink) { $this->forgeLink = $forgeLink; } /** * Getter for forgeLink * * @return string forgeLink */ public function getForgeLink(): string { return $this->forgeLink; } /** * Setter for crdate * * @param \DateTime $crdate creation date */ public function setCrdate(\DateTime $crdate) { $this->crdate = $crdate; } /** * Getter for crdate * * @return \DateTime crdate */ public function getCrdate(): \DateTime { return $this->crdate; } /** * Getter for tags * * @return \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\T3o\TerFe2\Domain\Model\Tag> Tags */ public function getTags(): ObjectStorage { return $this->tags; } /** * Adds a Tag * * @param \T3o\TerFe2\Domain\Model\Tag $tag The tag to be added */ public function addTag(Tag $tag) { $this->tags->attach($tag); } /** * Removes a Tag * * @param \T3o\TerFe2\Domain\Model\Tag $tag The tag to be removed */ public function removeTag(Tag $tag) { $this->tags->detach($tag); } /** * Getter for versions * * @return Version[]|\TYPO3\CMS\Extbase\Persistence\ObjectStorage<\T3o\TerFe2\Domain\Model\Version> */ public function getVersions(): ObjectStorage { return $this->versions; } /** * Get versions reverse sorted by version number * * @return array Versions */ public function getReverseVersionsWithPositiveReviewsByVersionNumber(): array { $versions = []; foreach ($this->versions as $version) { if ($version->getReviewState() > -1) { $versions[$version->getVersionNumber()] = $version; } } krsort($versions); return $versions; } /** * Get versions reverse sorted by version number * * @param int $limit * @param bool $reverseAfterSlice * @return array|\T3o\TerFe2\Domain\Model\Version[] Versions */ public function getReverseVersionsByVersionNumber(int $limit = 0, bool $reverseAfterSlice = true): array { $versions = []; foreach ($this->versions as $version) { $versions[$version->getVersionNumber()] = $version; } krsort($versions); if ($limit > 0) { $versions = array_slice($versions, 0, $limit, true); if ($reverseAfterSlice) { $versions = array_reverse($versions); } } return $versions; } /** * Returns the count of versions * * @return int Version count */ public function getVersionCount(): int { return !empty($this->versions) ? count($this->versions) : 0; } /** * Adds a Version * * @param \T3o\TerFe2\Domain\Model\Version $version The version to be added */ public function addVersion(Version $version) { $this->versions->attach($version); $this->setLastVersion($version); $this->addDownloads($version->getAllDownloads()); } /** * Removes a Version * * @param \T3o\TerFe2\Domain\Model\Version $version */ public function removeVersion(Version $version) { if (!$this->versions->contains($version)) { throw new \UnexpectedValueException('Tried to remove a version which is not part of the extension.', 1339359060); } $versionCount = $this->getVersionCount(); $this->versions->detach($version); if ($version === $this->lastVersion) { if ($versionCount === 1) { $this->lastVersion = null; } else { $this->lastVersion = array_shift($this->getReverseVersionsByVersionNumber()); } } } /** * Setter for lastVersion, will only set if given version is newer than existing one * * @param \T3o\TerFe2\Domain\Model\Version $lastVersion lastVersion */ public function setLastVersion(Version $lastVersion) { $this->lastVersion = $lastVersion; } /** * Getter for lastVersion * * @return \T3o\TerFe2\Domain\Model\Version lastVersion */ public function getLastVersion() { return $this->lastVersion; } /** * Setter for frontendUser * * @param string $frontendUser Frontend user */ public function setFrontendUser(string $frontendUser) { $this->frontendUser = $frontendUser; } /** * Getter for frontendUser * * @return string Frontend user */ public function getFrontendUser(): string { return $this->frontendUser; } /** * Sets externalManual * * @param string $externalManual */ public function setExternalManual(string $externalManual) { $this->externalManual = $externalManual; } /** * Gets externalManual * * @return string */ public function getExternalManual(): string { return $this->externalManual; } /** * Sets paypalUrl * * @param string $paypalUrl */ public function setPaypalUrl(string $paypalUrl) { $this->paypalUrl = $paypalUrl; } /** * Gets paypalUrl * * @return string */ public function getPaypalUrl(): string { return $this->paypalUrl; } /** * Sets repositoryUrl * * @param string $repositoryUrl */ public function setRepositoryUrl(string $repositoryUrl) { $this->repositoryUrl = $repositoryUrl; } /** * Gets repositoryUrl * * @return string */ public function getRepositoryUrl(): string { return $this->repositoryUrl; } /** * Returns all votes for the extension * * @return array Vote counts */ public function getVotes(): array { $votes = [ 'positive' => 0, 'negative' => 0, ]; foreach ($this->versions as $version) { $experiences = $version->getExperiences(); if (!is_array($experiences)) { continue; } foreach ($experiences as $experience) { $rating = $experience->getRating(); if ($rating > 0) { $votes['positive'] += $rating; } else { $votes['negative'] += $rating; } } } return $votes; } /** * Set all downloads sum * * @param int $downloads Count of downloads */ public function setDownloads(int $downloads) { $this->downloads = $downloads; } /** * Get sum of all version downloads * * @return int All downloads */ public function getDownloads(): int { return $this->downloads; } /** * Add downloads to all downloads sum * * @param int $downloads Count of downloads to add */ public function addDownloads(int $downloads) { $this->downloads += $downloads; } /** * @return \DateTime */ public function getExpire(): \DateTime { if ($this->expire === null) { $this->expire = new \DateTime(); $this->expire->setTimestamp(0); } return $this->expire; } /** * @param \DateTime|null $expire */ public function setExpire(\DateTime $expire = null) { $this->expire = $expire; } /** * Recalculate sum of all downloads */ public function recalculateDownloads() { $downloads = 0; $versions = $this->getVersions(); foreach ($versions as $version) { $downloads += $version->getAllDownloads(); } if (!empty($downloads)) { $this->setDownloads($downloads); } } /** * @param int $versionCount * @return string */ public function getDownloadsByVersionsAsJson($versionCount = 6): string { if (empty($this->versions)) { return ''; } $versions = []; foreach ($this->getReverseVersionsByVersionNumber($versionCount) as $version) { /** @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); } /** * @return \DateTime|null */ public function getDateOfFirstUpload() { $dateOfFirstUpload = null; $versions = array_reverse($this->getReverseVersionsByVersionNumber()); list($firstVersion) = $versions; if (!empty($firstVersion)) { $dateOfFirstUpload = date('d-m-Y', $firstVersion->getUploadDate()); } return $dateOfFirstUpload; } /** * @return string */ public function getComposerName() { return $this->composerName; } /** * @param string $composerName */ public function setComposerName($composerName) { $this->composerName = $composerName; } /** * @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--; } } }