Skip to content

Commit

Permalink
[TASK] Add BasePackageVersionSupport validation
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminkott committed Nov 28, 2024
1 parent c839517 commit 0116c83
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 5 deletions.
7 changes: 2 additions & 5 deletions src/Entity/SitePackage.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,16 @@
namespace App\Entity;

use App\Entity\SitePackage\Author;
use App\Validator\BasePackageVersionSupport;
use Symfony\Component\Validator\Constraints as Assert;

/**
* SitePackage.
*/
#[BasePackageVersionSupport]
class SitePackage implements \JsonSerializable
{
#[Assert\NotBlank]
#[Assert\Choice(['bootstrap_package', 'fluid_styled_content'])]
private string $basePackage = 'bootstrap_package';

#[Assert\NotBlank]
#[Assert\Choice([10.4, 11.5, 12.4, 13.4])]
private float $typo3Version = 13.4;

private string $vendorName;
Expand Down
11 changes: 11 additions & 0 deletions src/Entity/SitePackage/BasePackage.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,15 @@ public function removeVersion(BasePackageVersion $version): static

return $this;
}

public function hasVersionSupport(float $version): bool
{
$result = $this->getVersions()->filter(function (BasePackageVersion $basePackageVersion) use ($version) {
return $basePackageVersion->getVersion() === $version;
});
$resultArray = $result->toArray();
$result = reset($resultArray);

return $result === false ? false : true;
}
}
11 changes: 11 additions & 0 deletions src/Service/SitePackageBaseService.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ public function getPackages(): ArrayCollection
return $data;
}

public function getPackage(string $identifier): ?BasePackage
{
$result = $this->getPackages()->filter(function (BasePackage $package) use ($identifier) {
return $package->getIdentifier() === $identifier;
});
$resultArray = $result->toArray();
$result = reset($resultArray);

return $result === false ? null : $result;
}

/**
* @return array<string, string>
*/
Expand Down
35 changes: 35 additions & 0 deletions src/Validator/BasePackageVersionSupport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/*
* This file is part of the package t3o/get.typo3.org.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/

namespace App\Validator;

use Symfony\Component\Validator\Constraint;

#[\Attribute(\Attribute::TARGET_CLASS)]
class BasePackageVersionSupport extends Constraint
{
public string $message = 'The Base Package "{{ basePackage }}" is not compatible with TYPO3 Version "{{ typo3Version }}".';

public function getTargets(): string
{
return self::CLASS_CONSTRAINT;
}
}
59 changes: 59 additions & 0 deletions src/Validator/BasePackageVersionSupportValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

/*
* This file is part of the package t3o/get.typo3.org.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/

namespace App\Validator;

use App\Entity\SitePackage;
use App\Service\SitePackageBaseService;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;

class BasePackageVersionSupportValidator extends ConstraintValidator
{
private SitePackageBaseService $sitePackageBaseService;

public function __construct(SitePackageBaseService $sitePackageBaseService)
{
$this->sitePackageBaseService = $sitePackageBaseService;
}

public function validate(mixed $object, Constraint $constraint)
{
if (!$constraint instanceof BasePackageVersionSupport) {
throw new \LogicException('Invalid constraint type.');
}

if (!$object instanceof SitePackage) {
throw new \LogicException('Object must be of type ' . SitePackage::class);
}

$basePackage = $object->getBasePackage();
$typo3Version = $object->getTypo3Version();

$package = $this->sitePackageBaseService->getPackage($basePackage);
if ($package === null || !$package->hasVersionSupport($typo3Version)) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ basePackage }}', $basePackage)
->setParameter('{{ typo3Version }}', (string)$typo3Version)
->addViolation();
}
}
}

0 comments on commit 0116c83

Please sign in to comment.