<?php
declare(strict_types=1);
namespace App\Domain\Entity;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\Put;
use App\Application\ApiResources\State\Provider\MobileAdProvider;
use App\Application\Controller\Api\DuplicateAction;
use App\Application\Controller\Api\MobileAd\MobileAdClickAction;
use App\Domain\Entity\Behavior\Identifiable;
use App\Domain\Entity\PointOfInterest\Revision;
use DateTime;
use Gedmo\SoftDeleteable\Traits\SoftDeleteable;
use Gedmo\Timestampable\Traits\Timestampable;
#[ApiResource(
shortName: 'mobile',
operations: [
new Delete(security: "is_granted('ROLE_ADMIN')"),
new GetCollection(order: ['createdAt' => 'DESC'], security: "is_granted('ROLE_ADMIN')",
filters: [
'mobile_ad.query_filter',
'soft_delete_filter',
],
),
new Get(
uriTemplate: '/mobiles/{uuid}/click',
requirements: [
'uuid' => '^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$',
],
controller: MobileAdClickAction::class,
),
new Get(
requirements: [
'uuid' => '^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$',
],
security: "is_granted('ROLE_ADMIN')",
),
new Get(
uriTemplate: '/mobiles/current/banner',
provider: MobileAdProvider::class,
),
new Get(
uriTemplate: '/mobiles/current/map',
provider: MobileAdProvider::class,
),
new Post(security: "is_granted('ROLE_ADMIN')"),
new Post(
uriTemplate: '/mobiles/{uuid}/duplicate',
controller: DuplicateAction::class,
security: "is_granted('ROLE_ADMIN')",
),
new Put(security: "is_granted('ROLE_ADMIN')"),
],
normalizationContext: ['groups' => ['mobile_ad_read']],
denormalizationContext: ['groups' => ['mobile_ad_write']]
)]
class MobileAd
{
use Identifiable;
use SoftDeleteable;
use Timestampable;
public static string $IMAGE_TYPE_BANNER = 'banner';
public static string $IMAGE_TYPE_FULL = 'full';
public static string $TYPE_GPS = 'gps';
public static string $TYPE_POINT_OF_INTEREST = 'point_of_interest';
public static string $TYPE_TELEPHONE = 'telephone';
public static string $TYPE_URL = 'url';
public static string $TYPE_VIDEO = 'video';
public static string $TYPE_REDIRECT = 'redirect';
public function __construct(
public string $name,
public DateTime $startDate,
public DateTime $endDate,
public string $status,
public string $type,
public string $imageType,
public Locale $target,
public Media $photo,
public bool $useGps,
public ?float $useGpsLat,
public ?float $useGpsLng,
public ?float $useGpsRadius,
public ?string $url,
public ?PointOfInterest $pointOfInterest,
public ?float $lat,
public ?float $lng,
public ?string $video,
public ?string $phone,
public ?string $redirectName,
public ?string $redirectUuid,
public ?string $redirectSlideIndex,
public int $views = 0,
public int $clicks = 0,
// ! DTO ONLY
public ?Revision $revision = null,
) {}
public function __clone()
{
$this->id = null;
$this->uuid = null;
$this->createdAt = null;
$this->updatedAt = null;
return $this;
}
public function getImageSize(?string $imageType): array | null
{
return match ($imageType ?: $this->imageType) {
self::$IMAGE_TYPE_FULL => [1080, 1920],
self::$IMAGE_TYPE_BANNER => [700, 100],
default => null,
};
}
}