<?php
namespace AdminBundle\Entity;
use DateTime;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Validator\Constraints as Assert;
/**
* MobileAds
*/
#[ORM\Table(name: 'mobile_ads')]
#[ORM\Entity(repositoryClass: \AdminBundle\Repository\MobileAdsRepository::class)]
#[ORM\HasLifecycleCallbacks]
class MobileAds
{
const TYPE_CLIENT = 0;
const TYPE_DRIVER = 1;
const TYPES = [
self::TYPE_CLIENT => 'Client',
self::TYPE_DRIVER => 'Driver',
];
const FILE_FOLDER = 'upload/mobile-ads/';
/**
* @var int
*/
#[ORM\Column(name: 'id', type: 'integer')]
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'AUTO')]
private $id;
/**
* @var string
*/
#[ORM\Column(name: 'title', type: 'string', length: 255)]
protected $title;
/**
* @var string
*/
#[ORM\Column(name: 'description', type: 'text', nullable: true)]
protected $description;
/**
* @var int
*/
#[ORM\Column(name: 'type', type: 'integer', nullable: false)]
protected $type;
/**
* @var string
*/
#[ORM\Column(name: 'picture', type: 'string', length: 255, nullable: true)]
private $picture;
/**
* @var UploadedFile
*/
#[Assert\File(mimeTypes: ['image/png', 'image/jpeg', 'image/jpg', 'image/gif'])]
private $pictureFile;
/**
* @var string
*/
#[ORM\Column(name: 'url', type: 'string', length: 255, nullable: true)]
protected $url;
/**
* @var DateTime
*/
#[ORM\Column(name: 'start_date', type: 'datetime')]
protected $startDate;
/**
* @var DateTime
*/
#[ORM\Column(name: 'end_date', type: 'datetime')]
protected $endDate;
/**
* @var DateTime
*/
#[ORM\Column(name: 'updated_at', type: 'datetime')]
protected $updatedAt;
public function __construct()
{
$this->updatedAt = new DateTime();
}
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* @param string $title
*
* @return MobileAds
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* @param string $description
*
* @return MobileAds
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* @return int
*/
public function getType()
{
return $this->type;
}
/**
* @param int $type
*
* @return MobileAds
*/
public function setType($type)
{
$this->type = $type;
return $this;
}
/**
* @param UploadedFile $pictureFile
*/
public function setPictureFile(UploadedFile $pictureFile = null)
{
$this->pictureFile = $pictureFile;
return $this;
}
/**
* @return UploadedFile
*/
public function getPictureFile()
{
return $this->pictureFile;
}
public function getUploadFilePath()
{
if (!is_dir(self::FILE_FOLDER . $this->id)) {
mkdir(self::FILE_FOLDER . $this->id, 0777, true);
}
return self::FILE_FOLDER . $this->id . '/';
}
public function getPictureFilePath()
{
if (!$this->picture) {
return;
}
return $this->getUploadFilePath() . $this->picture;
}
public function uploadFile()
{
$pictureFile = $this->getPictureFile();
if (!$pictureFile) {
return;
}
$this->removePictureFile();
$nDate = new DateTime();
$filename = 'MOBILE_ADS_FILE_' . $nDate->format('Y-m-d-His');
$this->picture = $filename . '.' . $pictureFile->guessExtension();
$pictureFile->move(
$this->getUploadFilePath(),
$this->picture
);
$this->setPictureFile(null);
}
#[ORM\PreUpdate]
#[ORM\PrePersist]
public function lifecycleFileUpload()
{
$this->uploadFile();
}
#[ORM\PostRemove]
public function removeUpload()
{
$this->removePictureFile();
}
#[ORM\PostPersist]
public function onPostPersist()
{
rename(
self::FILE_FOLDER . $this->picture,
$this->getUploadFilePath() . $this->picture
);
}
public function removePictureFile()
{
if ($pictureFile = $this->getPictureFilePath()) {
@unlink($pictureFile);
}
}
/**
* Set url
*
* @param string $url
*
* @return MobileAds
*/
public function setUrl($url)
{
$this->url = $url;
return $this;
}
/**
* Get url
*
* @return string
*/
public function getUrl()
{
return $this->url;
}
/**
* @return DateTime
*/
public function getStartDate()
{
return $this->startDate;
}
/**
* @param DateTime $startDate
*
* @return MobileAds
*/
public function setStartDate(DateTime $startDate)
{
$this->startDate = $startDate;
return $this;
}
/**
* @return DateTime
*/
public function getEndDate()
{
return $this->endDate;
}
/**
* @param DateTime $endDate
*
* @return MobileAds
*/
public function setEndDate(DateTime $endDate)
{
$this->endDate = $endDate;
return $this;
}
/**
* @return DateTime
*/
public function getUpdatedAt()
{
return $this->updatedAt;
}
/**
* @param DateTime $updatedAt
*
* @return MobileAds
*/
public function setUpdatedAt(DateTime $updatedAt)
{
$this->updatedAt = $updatedAt;
return $this;
}
}