chore: create base entities

This commit is contained in:
wpetit 2020-10-26 17:16:29 +01:00
parent 76ca83f8e1
commit 095dad7b4d
11 changed files with 728 additions and 0 deletions

View File

@ -0,0 +1,45 @@
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20201026161459 extends AbstractMigration
{
public function getDescription(): string
{
return 'Create base entities';
}
public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('CREATE TABLE apikey (id INT AUTO_INCREMENT NOT NULL, consumer_id INT NOT NULL, token VARCHAR(255) NOT NULL, INDEX IDX_B84757A137FDBD6D (consumer_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('CREATE TABLE consumer (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(255) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('CREATE TABLE resource (id INT AUTO_INCREMENT NOT NULL, type_id INT NOT NULL, owner_id INT NOT NULL, INDEX IDX_BC91F416C54C8C93 (type_id), INDEX IDX_BC91F4167E3C61F9 (owner_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('CREATE TABLE resource_type (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(255) NOT NULL, validation_schema LONGTEXT DEFAULT NULL COMMENT \'(DC2Type:json)\', PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('CREATE TABLE rule (id INT AUTO_INCREMENT NOT NULL, predicate VARCHAR(255) DEFAULT NULL, action VARCHAR(255) NOT NULL, name VARCHAR(255) DEFAULT NULL, type SMALLINT NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('ALTER TABLE apikey ADD CONSTRAINT FK_B84757A137FDBD6D FOREIGN KEY (consumer_id) REFERENCES consumer (id)');
$this->addSql('ALTER TABLE resource ADD CONSTRAINT FK_BC91F416C54C8C93 FOREIGN KEY (type_id) REFERENCES resource_type (id)');
$this->addSql('ALTER TABLE resource ADD CONSTRAINT FK_BC91F4167E3C61F9 FOREIGN KEY (owner_id) REFERENCES consumer (id)');
}
public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE apikey DROP FOREIGN KEY FK_B84757A137FDBD6D');
$this->addSql('ALTER TABLE resource DROP FOREIGN KEY FK_BC91F4167E3C61F9');
$this->addSql('ALTER TABLE resource DROP FOREIGN KEY FK_BC91F416C54C8C93');
$this->addSql('DROP TABLE apikey');
$this->addSql('DROP TABLE consumer');
$this->addSql('DROP TABLE resource');
$this->addSql('DROP TABLE resource_type');
$this->addSql('DROP TABLE rule');
}
}

59
src/Entity/APIKey.php Normal file
View File

@ -0,0 +1,59 @@
<?php
namespace App\Entity;
use App\Repository\APIKeyRepository;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=APIKeyRepository::class)
*/
class APIKey
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $token;
/**
* @ORM\ManyToOne(targetEntity=Consumer::class, inversedBy="apiKeys")
* @ORM\JoinColumn(nullable=false)
*/
private $consumer;
public function getId(): ?int
{
return $this->id;
}
public function getToken(): ?string
{
return $this->token;
}
public function setToken(string $token): self
{
$this->token = $token;
return $this;
}
public function getConsumer(): ?Consumer
{
return $this->consumer;
}
public function setConsumer(?Consumer $consumer): self
{
$this->consumer = $consumer;
return $this;
}
}

119
src/Entity/Consumer.php Normal file
View File

@ -0,0 +1,119 @@
<?php
namespace App\Entity;
use App\Repository\ConsumerRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=ConsumerRepository::class)
*/
class Consumer
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\OneToMany(targetEntity=APIKey::class, mappedBy="consumer", orphanRemoval=true)
*/
private $apiKeys;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @ORM\OneToMany(targetEntity=Resource::class, mappedBy="owner", orphanRemoval=true)
*/
private $resources;
public function __construct()
{
$this->apiKeys = new ArrayCollection();
$this->resources = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
/**
* @return Collection|APIKey[]
*/
public function getApiKeys(): Collection
{
return $this->apiKeys;
}
public function addApiKey(APIKey $apiKey): self
{
if (!$this->apiKeys->contains($apiKey)) {
$this->apiKeys[] = $apiKey;
$apiKey->setConsumer($this);
}
return $this;
}
public function removeApiKey(APIKey $apiKey): self
{
if ($this->apiKeys->removeElement($apiKey)) {
// set the owning side to null (unless already changed)
if ($apiKey->getConsumer() === $this) {
$apiKey->setConsumer(null);
}
}
return $this;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
/**
* @return Collection|Resource[]
*/
public function getResources(): Collection
{
return $this->resources;
}
public function addResource(Resource $resource): self
{
if (!$this->resources->contains($resource)) {
$this->resources[] = $resource;
$resource->setOwner($this);
}
return $this;
}
public function removeResource(Resource $resource): self
{
if ($this->resources->removeElement($resource)) {
// set the owning side to null (unless already changed)
if ($resource->getOwner() === $this) {
$resource->setOwner(null);
}
}
return $this;
}
}

60
src/Entity/Resource.php Normal file
View File

@ -0,0 +1,60 @@
<?php
namespace App\Entity;
use App\Repository\ResourceRepository;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=ResourceRepository::class)
*/
class Resource
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity=ResourceType::class, inversedBy="Resources")
* @ORM\JoinColumn(nullable=false)
*/
private $type;
/**
* @ORM\ManyToOne(targetEntity=Consumer::class, inversedBy="resources")
* @ORM\JoinColumn(nullable=false)
*/
private $owner;
public function getId(): ?int
{
return $this->id;
}
public function getType(): ?ResourceType
{
return $this->type;
}
public function setType(?ResourceType $type): self
{
$this->type = $type;
return $this;
}
public function getOwner(): ?Consumer
{
return $this->owner;
}
public function setOwner(?Consumer $owner): self
{
$this->owner = $owner;
return $this;
}
}

100
src/Entity/ResourceType.php Normal file
View File

@ -0,0 +1,100 @@
<?php
namespace App\Entity;
use App\Repository\ResourceTypeRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=ResourceTypeRepository::class)
*/
class ResourceType
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @ORM\Column(type="json", nullable=true)
*/
private $validationSchema = [];
/**
* @ORM\OneToMany(targetEntity=Resource::class, mappedBy="type", orphanRemoval=true)
*/
private $Resources;
public function __construct()
{
$this->Resources = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getValidationSchema(): ?array
{
return $this->validationSchema;
}
public function setValidationSchema(?array $validationSchema): self
{
$this->validationSchema = $validationSchema;
return $this;
}
/**
* @return Collection|Resource[]
*/
public function getResources(): Collection
{
return $this->Resources;
}
public function addResource(Resource $resource): self
{
if (!$this->Resources->contains($resource)) {
$this->Resources[] = $resource;
$resource->setType($this);
}
return $this;
}
public function removeResource(Resource $resource): self
{
if ($this->Resources->removeElement($resource)) {
// set the owning side to null (unless already changed)
if ($resource->getType() === $this) {
$resource->setType(null);
}
}
return $this;
}
}

95
src/Entity/Rule.php Normal file
View File

@ -0,0 +1,95 @@
<?php
namespace App\Entity;
use App\Repository\RuleRepository;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=RuleRepository::class)
*/
class Rule
{
const TYPE_RESOURCE_RULE = 0;
const TYPE_GROUP_RULE = 1;
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $predicate;
/**
* @ORM\Column(type="string", length=255)
*/
private $action;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $name;
/**
* @ORM\Column(type="smallint")
*/
private $type;
public function getId(): ?int
{
return $this->id;
}
public function getPredicate(): ?string
{
return $this->predicate;
}
public function setPredicate(?string $predicate): self
{
$this->predicate = $predicate;
return $this;
}
public function getAction(): ?string
{
return $this->action;
}
public function setAction(string $action): self
{
$this->action = $action;
return $this;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(?string $name): self
{
$this->name = $name;
return $this;
}
public function getType(): ?int
{
return $this->type;
}
public function setType(int $type): self
{
$this->type = $type;
return $this;
}
}

View File

@ -0,0 +1,50 @@
<?php
namespace App\Repository;
use App\Entity\APIKey;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @method APIKey|null find($id, $lockMode = null, $lockVersion = null)
* @method APIKey|null findOneBy(array $criteria, array $orderBy = null)
* @method APIKey[] findAll()
* @method APIKey[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class APIKeyRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, APIKey::class);
}
// /**
// * @return APIKey[] Returns an array of APIKey objects
// */
/*
public function findByExampleField($value)
{
return $this->createQueryBuilder('a')
->andWhere('a.exampleField = :val')
->setParameter('val', $value)
->orderBy('a.id', 'ASC')
->setMaxResults(10)
->getQuery()
->getResult()
;
}
*/
/*
public function findOneBySomeField($value): ?APIKey
{
return $this->createQueryBuilder('a')
->andWhere('a.exampleField = :val')
->setParameter('val', $value)
->getQuery()
->getOneOrNullResult()
;
}
*/
}

View File

@ -0,0 +1,50 @@
<?php
namespace App\Repository;
use App\Entity\Consumer;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @method Consumer|null find($id, $lockMode = null, $lockVersion = null)
* @method Consumer|null findOneBy(array $criteria, array $orderBy = null)
* @method Consumer[] findAll()
* @method Consumer[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class ConsumerRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Consumer::class);
}
// /**
// * @return Consumer[] Returns an array of Consumer objects
// */
/*
public function findByExampleField($value)
{
return $this->createQueryBuilder('c')
->andWhere('c.exampleField = :val')
->setParameter('val', $value)
->orderBy('c.id', 'ASC')
->setMaxResults(10)
->getQuery()
->getResult()
;
}
*/
/*
public function findOneBySomeField($value): ?Consumer
{
return $this->createQueryBuilder('c')
->andWhere('c.exampleField = :val')
->setParameter('val', $value)
->getQuery()
->getOneOrNullResult()
;
}
*/
}

View File

@ -0,0 +1,50 @@
<?php
namespace App\Repository;
use App\Entity\Resource;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @method Resource|null find($id, $lockMode = null, $lockVersion = null)
* @method Resource|null findOneBy(array $criteria, array $orderBy = null)
* @method Resource[] findAll()
* @method Resource[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class ResourceRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Resource::class);
}
// /**
// * @return Resource[] Returns an array of Resource objects
// */
/*
public function findByExampleField($value)
{
return $this->createQueryBuilder('r')
->andWhere('r.exampleField = :val')
->setParameter('val', $value)
->orderBy('r.id', 'ASC')
->setMaxResults(10)
->getQuery()
->getResult()
;
}
*/
/*
public function findOneBySomeField($value): ?Resource
{
return $this->createQueryBuilder('r')
->andWhere('r.exampleField = :val')
->setParameter('val', $value)
->getQuery()
->getOneOrNullResult()
;
}
*/
}

View File

@ -0,0 +1,50 @@
<?php
namespace App\Repository;
use App\Entity\ResourceType;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @method ResourceType|null find($id, $lockMode = null, $lockVersion = null)
* @method ResourceType|null findOneBy(array $criteria, array $orderBy = null)
* @method ResourceType[] findAll()
* @method ResourceType[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class ResourceTypeRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, ResourceType::class);
}
// /**
// * @return ResourceType[] Returns an array of ResourceType objects
// */
/*
public function findByExampleField($value)
{
return $this->createQueryBuilder('r')
->andWhere('r.exampleField = :val')
->setParameter('val', $value)
->orderBy('r.id', 'ASC')
->setMaxResults(10)
->getQuery()
->getResult()
;
}
*/
/*
public function findOneBySomeField($value): ?ResourceType
{
return $this->createQueryBuilder('r')
->andWhere('r.exampleField = :val')
->setParameter('val', $value)
->getQuery()
->getOneOrNullResult()
;
}
*/
}

View File

@ -0,0 +1,50 @@
<?php
namespace App\Repository;
use App\Entity\Rule;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @method Rule|null find($id, $lockMode = null, $lockVersion = null)
* @method Rule|null findOneBy(array $criteria, array $orderBy = null)
* @method Rule[] findAll()
* @method Rule[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class RuleRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Rule::class);
}
// /**
// * @return Rule[] Returns an array of Rule objects
// */
/*
public function findByExampleField($value)
{
return $this->createQueryBuilder('r')
->andWhere('r.exampleField = :val')
->setParameter('val', $value)
->orderBy('r.id', 'ASC')
->setMaxResults(10)
->getQuery()
->getResult()
;
}
*/
/*
public function findOneBySomeField($value): ?Rule
{
return $this->createQueryBuilder('r')
->andWhere('r.exampleField = :val')
->setParameter('val', $value)
->getQuery()
->getOneOrNullResult()
;
}
*/
}