Skip to content

Commit

Permalink
refactor: use attributes over annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Nogueira Berthier da Silva committed May 15, 2023
1 parent 6ad3e81 commit cbc5fdf
Show file tree
Hide file tree
Showing 17 changed files with 203 additions and 231 deletions.
2 changes: 1 addition & 1 deletion app/Definitions/connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

$mode = $_ENV['MODE'] ?? '';

$scanDatabaseValues = function (): array {
$scanDatabaseValues = static function (): array {
if (isset($_ENV['DATABASE_URL'])) {
return ['url' => $_ENV['DATABASE_URL']];
}
Expand Down
22 changes: 11 additions & 11 deletions app/Definitions/database.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,28 @@

use Doctrine\DBAL\Types\Type;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
use Doctrine\ORM\Tools\Setup;
use Psr\Container\ContainerInterface;
use Doctrine\Common\Cache\Psr6\DoctrineProvider;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;

return [
EntityManager::class => function (ContainerInterface $container): EntityManager {
EntityManager::class => static function (ContainerInterface $container): EntityManager {
$settings = $container->get('settings');
$doctrine = $settings['doctrine'];

$config = Setup::createAnnotationMetadataConfiguration(
$cache = $doctrine['dev_mode'] ?
DoctrineProvider::wrap(new ArrayAdapter()) :
DoctrineProvider::wrap(new FilesystemAdapter(directory: $doctrine['cache_dir']));

$config = Setup::createAttributeMetadataConfiguration(
$doctrine['metadata_dirs'],
$doctrine['dev_mode'],
useSimpleAnnotationReader: false
null,
$cache
);


// $config->setMetadataCacheImpl(
// new FilesystemCache(
// $doctrine['cache_dir']
// )
// );

if (!Type::hasType('uuid')) {
Type::addType('uuid', 'Ramsey\Uuid\Doctrine\UuidType');
}
Expand Down
8 changes: 4 additions & 4 deletions app/Definitions/dependencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
$encrypter = new Encrypter($_SERVER['ENCRYPTION_KEY'] ?? '');

return [
LoggerInterface::class => function (ContainerInterface $c) {
LoggerInterface::class => static function (ContainerInterface $c) {
$settings = $c->get('settings');

$loggerSettings = $settings['logger'];
Expand All @@ -53,7 +53,7 @@
DataEncrypter::class => $encrypter,
AsymmetricEncrypter::class => new OpenSSLAsymmetricEncrypter(),
AsymmetricVerifier::class => new AsymmetricOpenSSLVerifier(),
StreamCollectorInterface::class => function (ContainerInterface $c): StreamCollectorInterface {
StreamCollectorInterface::class => static function (ContainerInterface $c): StreamCollectorInterface {
$factory = new S3AsyncDownloaderFactory();
$key = $_ENV['S3KEY'];
$secret = $_ENV['S3SECRET'];
Expand All @@ -62,15 +62,15 @@

return $factory->create($key, $secret, $region, $version);
},
S3StreamObjectsZipDownloader::class => function (ContainerInterface $container): S3StreamObjectsZipDownloader {
S3StreamObjectsZipDownloader::class => static function (ContainerInterface $container): S3StreamObjectsZipDownloader {
/**
* @var StreamCollectorInterface
*/
$streamCollector = $container->get(StreamCollectorInterface::class);

return new S3StreamObjectsZipDownloader($streamCollector);
},
UploadCollectorInterface::class => function (ContainerInterface $c): UploadCollectorInterface {
UploadCollectorInterface::class => static function (ContainerInterface $c): UploadCollectorInterface {
$factory = new S3AsyncUploadingFactory();
$key = $_ENV['S3KEY'];
$secret = $_ENV['S3SECRET'];
Expand Down
41 changes: 14 additions & 27 deletions src/Domain/Models/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,36 @@

use App\Domain\Models\Traits\TimestampsTrait;
use App\Domain\Models\Traits\UuidTrait;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\HasLifecycleCallbacks;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\Table;

use DateTime;
use Doctrine\ORM\Mapping as ORM;
use JsonSerializable;

/**
* @ORM\Entity
* @ORM\HasLifecycleCallbacks
* @ORM\Table(name="accounts")
*/

#[Entity, Table(name: 'accounts'), HasLifecycleCallbacks]
class Account implements JsonSerializable
{
use TimestampsTrait;
use UuidTrait;

/**
* @var int
*
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
#[Id, Column(type: 'integer'), GeneratedValue(strategy: 'AUTO')]
protected $id;



/**
* @ORM\Column(type="string", unique=true, nullable=false)
*/
#[Column(type: 'string', unique: true, nullable: false)]
private string $email;

/**
* @ORM\Column(type="string")
*/
#[Column(type: 'string')]
private string $username;

/**
* @ORM\Column(type="string")
*/
#[Column(type: 'string')]
private string $password;

/**
* @ORM\Column(type="string")
*/
#[Column(type: 'string')]
private ?string $role = 'common';

public function __construct(
Expand Down
58 changes: 35 additions & 23 deletions src/Domain/Models/Assets/AbstractAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,35 @@
use App\Domain\Models\Traits\UuidTrait;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\InheritanceType;
use Doctrine\ORM\Mapping\DiscriminatorColumn;
use Doctrine\ORM\Mapping\DiscriminatorMap;
use Doctrine\ORM\Mapping\HasLifecycleCallbacks;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Table;
use Doctrine\ORM\Mapping\ManyToOne;
use Doctrine\ORM\Mapping\OneToMany;
use Exception;

/**
* Abstract resource comprehends the entities which hold data such as medias, assets, etc.
*
* @ORM\Entity
* @ORM\Table(name="assets")
* @ORM\HasLifecycleCallbacks
* @ORM\InheritanceType("SINGLE_TABLE")
* @ORM\DiscriminatorColumn(name="asset_type", type="string")
* @ORM\DiscriminatorMap({
* "3dObject" = "ThreeDimensionalAsset",
* "texture" = "TextureAsset",
* "video" = "VideoAsset",
* "picture" = "PictureAsset"
* })
*/
#[
Entity,
Table(name: 'markers'),
HasLifecycleCallbacks,
InheritanceType("SINGLE_TABLE"),
DiscriminatorColumn(name: "asset_type", type: "string"),
DiscriminatorMap([
"3dObject" => "ThreeDimensionalAsset",
"texture" => "TextureAsset",
"video" => "VideoAsset",
"picture" => "PictureAsset"
])
]
abstract class AbstractAsset implements ModelInterface
{
use TimestampsTrait;
Expand All @@ -38,34 +49,35 @@ abstract class AbstractAsset implements ModelInterface
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
#[Id, Column(type: 'integer'), GeneratedValue(strategy: 'AUTO')]
protected ?int $id;
/** @ORM\Column(type="string", unique=true) */
#[Column(type: 'string', unique: true)]
private string $path;
/** @ORM\Column(type="string", unique=true) */
#[Column(type: 'string', unique: true)]
private string $fileName;
/** @ORM\Column(type="string", nullable=true) */
#[Column(type: 'string', nullable: true)]
private ?string $url;
/** @ORM\Column(type="string") */
#[Column(type: 'string')]
private string $mediaType;
/** @ORM\Column(type="string") */
#[Column(type: 'string')]
private string $originalName;
/** @ORM\Column(type="string") */
#[Column(type: 'string')]
private string $mimeType;

private ?string $temporaryLocation = null;

/**
* One Asset may have a set of sub assets, e.g., a 3D object can have many textures.
*
* @ORM\OneToMany(targetEntity="AbstractAsset", mappedBy="parent")
* @param Collection<AbstractAsset> $children
*/
#[OneToMany(targetEntity: AbstractAsset::class, mappedBy: "parent")]
private Collection $children;

/**
* Many sub assets have a single parent.
*
* @ORM\ManyToOne(targetEntity="AbstractAsset", inversedBy="children")
*/
#[ManyToOne(targetEntity: AbstractAsset::class, inversedBy: "children")]
private self $parent;

public function __construct(string $mediaType)
Expand Down Expand Up @@ -319,4 +331,4 @@ public function fromCommand(CreateAsset $createAsset)
$this->originalName = $createAsset->originalName;
$this->mimeType = $createAsset->mimeType();
}
}
}
6 changes: 2 additions & 4 deletions src/Domain/Models/Assets/PictureAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
namespace App\Domain\Models\Assets;

use App\Domain\Models\Assets\AbstractAsset;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\Entity;

/**
* @ORM\Entity
*/
#[Entity]
class PictureAsset extends AbstractAsset
{
public function __construct()
Expand Down
6 changes: 2 additions & 4 deletions src/Domain/Models/Assets/TextureAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
namespace App\Domain\Models\Assets;

use App\Domain\Models\Assets\AbstractAsset;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\Entity;

/**
* @ORM\Entity
*/
#[Entity]
class TextureAsset extends AbstractAsset
{
public function __construct()
Expand Down
7 changes: 3 additions & 4 deletions src/Domain/Models/Assets/ThreeDimensionalAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
use App\Domain\Models\Assets\AbstractAsset;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\Table;
use Doctrine\ORM\Mapping\Entity;

/**
* @ORM\Entity
* @ORM\Table(name="three_dimensional_assets")
*/
#[Entity, Table(name="three_dimensional_assets"]
class ThreeDimensionalAsset extends AbstractAsset
{
public function __construct()
Expand Down
6 changes: 2 additions & 4 deletions src/Domain/Models/Assets/VideoAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
namespace App\Domain\Models\Assets;

use App\Domain\Models\Assets\AbstractAsset;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\Entity;

/**
* @ORM\Entity
*/
#[Entity]
class VideoAsset extends AbstractAsset
{
public function __construct()
Expand Down
52 changes: 29 additions & 23 deletions src/Domain/Models/Marker/Marker.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,47 +15,53 @@
use App\Domain\Models\Traits\UuidTrait;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity
* @ORM\HasLifecycleCallbacks
* @ORM\Table(name="markers")
*/
use Doctrine\ORM\Mapping\OneToOne;
use Doctrine\ORM\Mapping\OneToMany;
use Doctrine\ORM\Mapping\ManyToOne;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\HasLifecycleCallbacks;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\JoinColumn;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Table;

#[Entity, Table(name: 'markers'), HasLifecycleCallbacks]
class Marker implements ModelInterface, MediaHostParentInterface
{
use TimestampsTrait;
use UuidTrait;
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/

#[Id, Column(type: 'integer'), GeneratedValue(strategy: 'AUTO')]
protected ?int $id;
/**
* Many markers have one museum. This is the owning side.
*
* @ORM\ManyToOne(targetEntity="App\Domain\Models\Museum", inversedBy="markers")
* @ORM\JoinColumn(name="museum_id", referencedColumnName="id")
*/

#[
ManyToOne(targetEntity: Museum::class, inversedBy: "markers"),
JoinColumn(name: "museum_id", referencedColumnName: "id")
]
private ?Museum $museum;
/** @ORM\Column(type="string", nullable=false) */

#[Column(type: "string", nullable: false)]
private string $name;
/** @ORM\Column(type="text", nullable=true) */

#[Column(type: "text", nullable: true)]
private ?string $text;
/** @ORM\Column(type="string", nullable=true) */

#[Column(type: "string", nullable: true)]
private ?string $title;
/** @ORM\OneToOne(targetEntity="MarkerAsset", mappedBy="marker", cascade={"persist", "remove"}) */

#[OneToOne(targetEntity: MarkerAsset::class, mappedBy: "marker", cascade: ["persist", "remove"])]
private ?MarkerAsset $asset = null;

/** @ORM\Column(type="boolean", nullable=false) */
#[Column(type: "boolean", nullable: false)]
private bool $isActive = true;

/**
* One marker has one or many resources. This is the inverse side.
*
* @ORM\OneToMany(targetEntity="App\Domain\Models\PlacementObject\PlacementObject", mappedBy="marker", cascade={"persist", "remove"})
*/
#[OneToMany(targetEntity: PlacementObject::class, mappedBy: "marker", cascade: ["persist", "remove"])]
private Collection $resources;

public function __construct()
Expand Down
Loading

0 comments on commit cbc5fdf

Please sign in to comment.