Initial commit: CloudOps infrastructure platform
This commit is contained in:
@@ -0,0 +1,164 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Mautic\ProjectBundle\Entity;
|
||||
|
||||
use ApiPlatform\Metadata\ApiResource;
|
||||
use ApiPlatform\Metadata\Delete;
|
||||
use ApiPlatform\Metadata\Get;
|
||||
use ApiPlatform\Metadata\GetCollection;
|
||||
use ApiPlatform\Metadata\Patch;
|
||||
use ApiPlatform\Metadata\Post;
|
||||
use ApiPlatform\Metadata\Put;
|
||||
use Doctrine\DBAL\Types\Types;
|
||||
use Doctrine\ORM\Mapping\ClassMetadata as OrmClassMetadata;
|
||||
use Mautic\ApiBundle\Serializer\Driver\ApiMetadataDriver;
|
||||
use Mautic\CoreBundle\Doctrine\Mapping\ClassMetadataBuilder;
|
||||
use Mautic\CoreBundle\Entity\FormEntity;
|
||||
use Mautic\CoreBundle\Entity\UuidInterface;
|
||||
use Mautic\CoreBundle\Entity\UuidTrait;
|
||||
use Symfony\Component\Serializer\Attribute\Groups;
|
||||
use Symfony\Component\Validator\Constraints\NotBlank;
|
||||
use Symfony\Component\Validator\Mapping\ClassMetadata;
|
||||
|
||||
#[ApiResource(
|
||||
operations: [
|
||||
new GetCollection(security: "is_granted('project:projects:view')"),
|
||||
new Post(security: "is_granted('project:projects:create')"),
|
||||
new Get(security: "is_granted('project:projects:view')"),
|
||||
new Put(security: "is_granted('project:projects:edit')"),
|
||||
new Patch(security: "is_granted('project:projects:edit')"),
|
||||
new Delete(security: "is_granted('project:projects:delete')"),
|
||||
],
|
||||
normalizationContext: [
|
||||
'groups' => ['project:read'],
|
||||
'swagger_definition_name' => 'Read',
|
||||
],
|
||||
denormalizationContext: [
|
||||
'groups' => ['project:write'],
|
||||
'swagger_definition_name' => 'Write',
|
||||
]
|
||||
)]
|
||||
class Project extends FormEntity implements UuidInterface
|
||||
{
|
||||
use UuidTrait;
|
||||
|
||||
public const TABLE_NAME = 'projects';
|
||||
|
||||
#[Groups(['project:read'])]
|
||||
private ?int $id = null;
|
||||
|
||||
#[Groups(['project:read', 'project:write'])]
|
||||
private ?string $description = null;
|
||||
|
||||
#[Groups(['project:read', 'project:write'])]
|
||||
private ?string $name = null;
|
||||
|
||||
/**
|
||||
* @var mixed[]
|
||||
*/
|
||||
#[Groups(['project:read', 'project:write'])]
|
||||
private array $properties = [];
|
||||
|
||||
/**
|
||||
* Transient property to store the count of entities associated with this project.
|
||||
* This is not persisted to the database.
|
||||
*/
|
||||
public int $entitiesCount = 0;
|
||||
|
||||
public function __clone()
|
||||
{
|
||||
$this->id = null;
|
||||
|
||||
parent::__clone();
|
||||
}
|
||||
|
||||
public static function loadMetadata(OrmClassMetadata $metadata): void
|
||||
{
|
||||
$builder = new ClassMetadataBuilder($metadata);
|
||||
|
||||
$builder->setTable(self::TABLE_NAME)
|
||||
->setCustomRepositoryClass(ProjectRepository::class)
|
||||
->addIndex(['name'], 'project_name');
|
||||
|
||||
$builder->addIdColumns();
|
||||
|
||||
$builder->addField('properties', Types::JSON);
|
||||
|
||||
static::addUuidField($builder);
|
||||
}
|
||||
|
||||
public static function loadApiMetadata(ApiMetadataDriver $metadata): void
|
||||
{
|
||||
$metadata->setGroupPrefix('project')
|
||||
->addListProperties(
|
||||
[
|
||||
'id',
|
||||
'name',
|
||||
]
|
||||
)
|
||||
->addProperties(
|
||||
[
|
||||
'description',
|
||||
'properties',
|
||||
]
|
||||
)
|
||||
->build();
|
||||
}
|
||||
|
||||
public static function loadValidatorMetadata(ClassMetadata $metadata): void
|
||||
{
|
||||
$metadata->addPropertyConstraint(
|
||||
'name',
|
||||
new NotBlank(['message' => 'mautic.core.name.required'])
|
||||
);
|
||||
}
|
||||
|
||||
public function getId(): ?int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function getDescription(): ?string
|
||||
{
|
||||
return $this->description;
|
||||
}
|
||||
|
||||
public function setDescription(?string $description): void
|
||||
{
|
||||
$this->isChanged('description', $description);
|
||||
|
||||
$this->description = $description;
|
||||
}
|
||||
|
||||
public function getName(): ?string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
public function setName(?string $name): void
|
||||
{
|
||||
$this->isChanged('name', $name);
|
||||
|
||||
$this->name = $name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed[]
|
||||
*/
|
||||
public function getProperties(): array
|
||||
{
|
||||
return $this->properties;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed[] $properties
|
||||
*/
|
||||
public function setProperties(array $properties): void
|
||||
{
|
||||
$this->isChanged('properties', $properties);
|
||||
|
||||
$this->properties = $properties;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Mautic\ProjectBundle\Entity;
|
||||
|
||||
use Mautic\CoreBundle\Entity\CommonRepository;
|
||||
|
||||
class ProjectRepository extends CommonRepository
|
||||
{
|
||||
/**
|
||||
* @return array<string[]>
|
||||
*/
|
||||
protected function getDefaultOrder(): array
|
||||
{
|
||||
return [
|
||||
['p.date_modified', 'ASC'],
|
||||
];
|
||||
}
|
||||
|
||||
public function getTableAlias(): string
|
||||
{
|
||||
return 'p';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Mautic\ProjectBundle\Entity;
|
||||
|
||||
use Doctrine\DBAL\Query\QueryBuilder;
|
||||
|
||||
trait ProjectRepositoryTrait
|
||||
{
|
||||
/**
|
||||
* @return array{0: string, 1: array<string, array<int|string>>}
|
||||
*/
|
||||
private function handleProjectFilter(QueryBuilder $queryBuilder, string $idColumn, string $xrefTable, string $parentTableAlias, string $projectName, bool $negation): array
|
||||
{
|
||||
$queryBuilder->select($idColumn);
|
||||
$queryBuilder->from(MAUTIC_TABLE_PREFIX.$xrefTable, 'projectxref');
|
||||
$queryBuilder->innerJoin(
|
||||
'projectxref',
|
||||
MAUTIC_TABLE_PREFIX.'projects',
|
||||
'project',
|
||||
'project.id = projectxref.project_id'
|
||||
);
|
||||
$queryBuilder->where($queryBuilder->expr()->eq('project.name', ':name'));
|
||||
$queryBuilder->setParameter('name', $projectName);
|
||||
$ids = $queryBuilder->executeQuery()->fetchFirstColumn() ?: [0];
|
||||
$ids = array_map(fn ($value) => "'$value'", $ids);
|
||||
|
||||
if ($negation) {
|
||||
$expr = $queryBuilder->expr()->notIn("{$parentTableAlias}.id", $ids);
|
||||
} else {
|
||||
$expr = $queryBuilder->expr()->in("{$parentTableAlias}.id", $ids);
|
||||
}
|
||||
|
||||
return [$expr, []];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Mautic\ProjectBundle\Entity;
|
||||
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Doctrine\Common\Collections\Collection;
|
||||
use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder;
|
||||
use Mautic\ApiBundle\Serializer\Driver\ApiMetadataDriver;
|
||||
|
||||
trait ProjectTrait
|
||||
{
|
||||
/**
|
||||
* @var Collection<int, Project>
|
||||
*/
|
||||
private Collection $projects;
|
||||
|
||||
private function initializeProjects(): void
|
||||
{
|
||||
$this->projects = new ArrayCollection();
|
||||
}
|
||||
|
||||
private static function addProjectsField(ClassMetadataBuilder $builder, string $tableName, string $columnName): void
|
||||
{
|
||||
$builder->createManyToMany('projects', Project::class)
|
||||
->setJoinTable($tableName)
|
||||
->addInverseJoinColumn('project_id', 'id', false, false, 'CASCADE')
|
||||
->addJoinColumn($columnName, 'id', false, false, 'CASCADE')
|
||||
->setOrderBy(['name' => 'ASC'])
|
||||
->setIndexBy('name')
|
||||
->fetchLazy()
|
||||
->cascadeMerge()
|
||||
->cascadePersist()
|
||||
->cascadeDetach()
|
||||
->build();
|
||||
}
|
||||
|
||||
private static function addProjectsInLoadApiMetadata(ApiMetadataDriver $metadata, string $groupPrefix): void
|
||||
{
|
||||
$metadata->setGroupPrefix($groupPrefix)->addProperties(['projects'])->build();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $prop
|
||||
* @param mixed $val
|
||||
*/
|
||||
protected function isChanged($prop, $val): void
|
||||
{
|
||||
if ('projects' === $prop) {
|
||||
if ($val instanceof Project) {
|
||||
$this->changes['projects']['added'][] = $val->getName();
|
||||
} else {
|
||||
$this->changes['projects']['removed'][] = $val;
|
||||
}
|
||||
} else {
|
||||
parent::isChanged($prop, $val);
|
||||
}
|
||||
}
|
||||
|
||||
public function addProject(Project $project): void
|
||||
{
|
||||
$this->isChanged('projects', $project);
|
||||
$this->projects[] = $project;
|
||||
}
|
||||
|
||||
public function removeProject(Project $project): bool
|
||||
{
|
||||
$this->isChanged('projects', $project->getName());
|
||||
|
||||
return $this->projects->removeElement($project);
|
||||
}
|
||||
|
||||
public function getProjects(): Collection
|
||||
{
|
||||
return $this->projects;
|
||||
}
|
||||
|
||||
public function setProjects(Collection $projects): self
|
||||
{
|
||||
$this->projects = $projects;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user