src/Entity/File.php line 22

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use DateTime;
  4. use Doctrine\Common\Collections\Collection;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Symfony\Component\HttpFoundation\File\UploadedFile;
  7. use Doctrine\Common\Collections\ArrayCollection;
  8. use App\Constant\FileActionConstant;
  9. use App\Constant\FilePlatformConstant;
  10. use App\Constant\FileStatusConstant;
  11. use App\Constant\FileTypeConstant;
  12. /**
  13.  * File
  14.  *
  15.  * @ORM\Table(name="file")
  16.  * @ORM\Entity(repositoryClass="App\Repository\FileRepository")
  17.  * @ORM\HasLifecycleCallbacks
  18.  */
  19. class File
  20. {
  21.     /**
  22.      * @var int
  23.      *
  24.      * @ORM\Column(name="id", type="integer")
  25.      * @ORM\Id
  26.      * @ORM\GeneratedValue(strategy="AUTO")
  27.      */
  28.     private $id;
  29.     /**
  30.      * @var string
  31.      *
  32.      * @ORM\Column(name="name", type="string", length=255)
  33.      */
  34.     private $name;
  35.     /**
  36.      * @var DateTime
  37.      *
  38.      * @ORM\Column(name="created_at", type="datetime")
  39.      */
  40.     private $createdAt;
  41.     /**
  42.      * @var bool
  43.      *
  44.      * @ORM\Column(name="deleted", type="boolean")
  45.      */
  46.     private $deleted false;
  47.     /**
  48.      * @ORM\Column(name="type", type="string")
  49.      */
  50.     private $type;
  51.     /**
  52.      * @var Folder $folder
  53.      *
  54.      * @ORM\ManyToOne(targetEntity="Folder", inversedBy="files")
  55.      * @ORM\JoinColumn(nullable=false)
  56.      */
  57.     private $folder;
  58.     /**
  59.      * @ORM\ManyToOne(targetEntity="Project", inversedBy="files")
  60.      * @ORM\JoinColumn(nullable=true, onDelete="SET NULL")
  61.      */
  62.     private $project;
  63.     /**
  64.      * @ORM\Column(name="status", type="string", length=255)
  65.      */
  66.     private $status FileStatusConstant::STATUS_PRIVATE;
  67.     /**
  68.      * @ORM\OneToMany(targetEntity="FileAction", mappedBy="file", cascade={"remove"})
  69.      */
  70.     private $fileActions;
  71.     /**
  72.      * @ORM\Column(name="extension", type="string", length=255, nullable=true)
  73.      */
  74.     private $extension;
  75.     /**
  76.      * @ORM\Column(name="mime_type", type="string", length=255, nullable=true)
  77.      */
  78.     private $mimeType;
  79.     /**
  80.      * @ORM\Column(name="real_name", type="string", length=255, nullable=true)
  81.      */
  82.     private $realName;
  83.     /**
  84.      * @ORM\Column(name="description", type="text", nullable=true)
  85.      */
  86.     private $description '';
  87.     private $tempFilename;
  88.     /**
  89.      * @var UploadedFile $file
  90.      */
  91.     private $file;
  92.     /**
  93.      * @ORM\OneToOne(targetEntity="Playable", cascade={"all"})
  94.      * @ORM\JoinColumn(nullable=true)
  95.      */
  96.     private $playable;
  97.     /**
  98.      * @ORM\OneToMany(targetEntity="FileBandMember", mappedBy="file", cascade={"remove"}))
  99.      */
  100.     private $bandMembers;
  101.     /**
  102.      * @ORM\OneToOne(targetEntity="App\Entity\Picture", cascade={"all"})
  103.      * @ORM\joinColumn(onDelete="SET NULL")
  104.      */
  105.     protected $picture;
  106.     /**
  107.      * @var string
  108.      *
  109.      * @ORM\Column(name="url", type="string", length=255, nullable=true)
  110.      */
  111.     private $url;
  112.     /**
  113.      * Constructor
  114.      */
  115.     public function __construct()
  116.     {
  117.         $this->fileActions = new ArrayCollection();
  118.         $this->bandMembers = new ArrayCollection();
  119.     }
  120.     /**
  121.      * get entity to string
  122.      */
  123.     public function __toString()
  124.     {
  125.         return $this->name;
  126.     }
  127.     public function getFile(): ?UploadedFile
  128.     {
  129.         return $this->file;
  130.     }
  131.     public function setFile(UploadedFile $file)
  132.     {
  133.         $this->file $file;
  134.         if ($this->file->getMimeType()) {
  135.             $this->mimeType $this->file->getMimeType();
  136.         } else {
  137.             $this->mimeType $this->file->getClientMimeType();
  138.         }
  139.         $this->extension $this->file->getClientOriginalExtension();
  140.         $this->createdAt = new DateTime();
  141.         $this->realName uniqid();
  142.     }
  143.     /**
  144.      * @ORM\PostPersist()
  145.      * @ORM\PostUpdate()
  146.      */
  147.     public function upload()
  148.     {
  149.         if (null === $this->file) {
  150.             return;
  151.         }
  152.         $this->file->move(
  153.             $this->getUploadDirectory(),
  154.             $this->getFullName()
  155.         );
  156.     }
  157.     /**
  158.      * @ORM\PreRemove()
  159.      */
  160.     public function preRemoveUpload()
  161.     {
  162.         $this->tempFilename $this->getPath();
  163.     }
  164.     /**
  165.      * @ORM\PostRemove()
  166.      */
  167.     public function removeUpload()
  168.     {
  169.         if ($this->url == null && file_exists($this->tempFilename)) {
  170.             unlink($this->tempFilename);
  171.         }
  172.     }
  173.     public function getUploadDirectory(): string
  174.     {
  175.         if ($this->getType() == FileTypeConstant::PICTURE){
  176.             return 'uploads/pictures/bands/';
  177.         }else{
  178.             return 'uploads/files/';
  179.         }
  180.     }
  181.     public function getFullName(): string
  182.     {
  183.         return $this->realName '.' $this->extension;
  184.     }
  185.     public function getPath(): string
  186.     {
  187.         return $this->getUploadDirectory() . $this->getFullName();
  188.     }
  189.     /**
  190.      * Get id
  191.      *
  192.      * @return integer
  193.      */
  194.     public function getId(): int
  195.     {
  196.         return $this->id;
  197.     }
  198.     /**
  199.      * Set name
  200.      *
  201.      * @param string $name
  202.      *
  203.      * @return File
  204.      */
  205.     public function setName(string $name): File
  206.     {
  207.         $this->name $name;
  208.         return $this;
  209.     }
  210.     /**
  211.      * Get name
  212.      *
  213.      * @return string|null
  214.      */
  215.     public function getName(): ?string
  216.     {
  217.         return $this->name;
  218.     }
  219.     /**
  220.      * Get file size in Mo, return 0 if not stored on server
  221.      *
  222.      * @return numeric
  223.      */
  224.     public function getSize()
  225.     {
  226.         if ($this->getUrl() !== null || !file_exists($this->getPath())) {
  227.             return 0;
  228.         }
  229.         return (float)number_format(filesize($this->getPath()) / (1024 1024), 2);
  230.     }
  231.     /**
  232.      * Set extension
  233.      *
  234.      * @param string $extension
  235.      *
  236.      * @return File
  237.      */
  238.     public function setExtension(string $extension): File
  239.     {
  240.         $this->extension $extension;
  241.         return $this;
  242.     }
  243.     /**
  244.      * Get extension
  245.      *
  246.      * @return string
  247.      */
  248.     public function getExtension(): string
  249.     {
  250.         return $this->extension;
  251.     }
  252.     /**
  253.      * Set realName
  254.      *
  255.      * @param string $realName
  256.      *
  257.      * @return File
  258.      */
  259.     public function setRealName(string $realName): File
  260.     {
  261.         $this->realName $realName;
  262.         return $this;
  263.     }
  264.     /**
  265.      * Get realName
  266.      *
  267.      * @return string
  268.      */
  269.     public function getRealName(): string
  270.     {
  271.         return $this->realName;
  272.     }
  273.     /**
  274.      * Set description
  275.      *
  276.      * @param string|null $description
  277.      *
  278.      * @return File
  279.      */
  280.     public function setDescription(?string $description): File
  281.     {
  282.         $this->description $description;
  283.         return $this;
  284.     }
  285.     /**
  286.      * Get description
  287.      *
  288.      * @return string|null
  289.      */
  290.     public function getDescription(): ?string
  291.     {
  292.         return $this->description;
  293.     }
  294.     /**
  295.      * Get band
  296.      *
  297.      * @return Band
  298.      */
  299.     public function getBand(): Band
  300.     {
  301.         return $this->folder->getOwner();
  302.     }
  303.     /**
  304.      * Set folder
  305.      *
  306.      * @param Folder $folder
  307.      *
  308.      * @return File
  309.      */
  310.     public function setFolder(Folder $folder): File
  311.     {
  312.         $this->folder $folder;
  313.         return $this;
  314.     }
  315.     /**
  316.      * Get folder
  317.      *
  318.      * @return Folder
  319.      */
  320.     public function getFolder(): Folder
  321.     {
  322.         return $this->folder;
  323.     }
  324.     /**
  325.      * Add fileAction
  326.      *
  327.      * @param FileAction $fileAction
  328.      *
  329.      * @return File
  330.      */
  331.     public function addFileAction(FileAction $fileAction): File
  332.     {
  333.         $this->fileActions[] = $fileAction;
  334.         return $this;
  335.     }
  336.     /**
  337.      * Remove fileAction
  338.      *
  339.      * @param FileAction $fileAction
  340.      */
  341.     public function removeFileAction(FileAction $fileAction)
  342.     {
  343.         $this->fileActions->removeElement($fileAction);
  344.     }
  345.     /**
  346.      * Get fileActions
  347.      *
  348.      * @return Collection
  349.      */
  350.     public function getFileActions()
  351.     {
  352.         return $this->fileActions;
  353.     }
  354.     /**
  355.      * Get file likes
  356.      *
  357.      * @return Collection
  358.      */
  359.     public function getLikes(): Collection
  360.     {
  361.         return $this->fileActions->filter(
  362.             function ($action) {
  363.                 return $action->getAction() == FileActionConstant::LIKE;
  364.             }
  365.         );
  366.     }
  367.     /**
  368.      * Get file likes
  369.      *
  370.      * @return Action|null
  371.      */
  372.     public function getPublishAction(): ?Action
  373.     {
  374.         $action $this->fileActions->filter(
  375.             function ($action) {
  376.                 return $action->getAction() == FileActionConstant::PUBLISH;
  377.             }
  378.         )->first();
  379.         return ($action instanceof Action) ? $action null;
  380.     }
  381.     /**
  382.      * Know if specified user like this file
  383.      *
  384.      * @param User $user
  385.      * @return boolean
  386.      */
  387.     public function isLikedByUser(User $user): bool
  388.     {
  389.         $likes $this->fileActions->filter(
  390.             function ($action) use ($user) {
  391.                 return $action->getAction() === FileActionConstant::LIKE
  392.                     && $action->getUser() === $user;
  393.             }
  394.         );
  395.         return (bool)count($likes);
  396.     }
  397.     /**
  398.      * Set playable
  399.      *
  400.      * @param Playable|null $playable
  401.      *
  402.      * @return File
  403.      */
  404.     public function setPlayable(Playable $playable null): File
  405.     {
  406.         $this->playable $playable;
  407.         return $this;
  408.     }
  409.     /**
  410.      * Get playable
  411.      *
  412.      * @return Playable|null
  413.      */
  414.     public function getPlayable(): ?Playable
  415.     {
  416.         return $this->playable;
  417.     }
  418.     /**
  419.      * Add bandMember
  420.      *
  421.      * @param FileBandMember $bandMember
  422.      *
  423.      * @return File
  424.      */
  425.     public function addBandMember(FileBandMember $bandMember): File
  426.     {
  427.         $this->bandMembers[] = $bandMember;
  428.         return $this;
  429.     }
  430.     /**
  431.      * Remove bandMember
  432.      *
  433.      * @param FileBandMember $bandMember
  434.      */
  435.     public function removeBandMember(FileBandMember $bandMember)
  436.     {
  437.         $this->bandMembers->removeElement($bandMember);
  438.     }
  439.     /**
  440.      * Get bandMembers
  441.      *
  442.      * @return Collection
  443.      */
  444.     public function getBandMembers()
  445.     {
  446.         return $this->bandMembers;
  447.     }
  448.     /**
  449.      * Set picture
  450.      *
  451.      * @param Picture|null $picture
  452.      *
  453.      * @return File
  454.      */
  455.     public function setPicture(Picture $picture null): File
  456.     {
  457.         $this->picture $picture;
  458.         return $this;
  459.     }
  460.     /**
  461.      * Get picture
  462.      *
  463.      * @return Picture|null
  464.      */
  465.     public function getPicture(): ?Picture
  466.     {
  467.         return $this->picture;
  468.     }
  469.     /**
  470.      * @param boolean $deleted
  471.      */
  472.     public function setDeleted(bool $deleted)
  473.     {
  474.         $this->deleted $deleted;
  475.     }
  476.     /**
  477.      * Set mime type
  478.      *
  479.      * @param string $mimeType
  480.      *
  481.      * @return File
  482.      */
  483.     public function setMimeType(string $mimeType): File
  484.     {
  485.         $this->mimeType $mimeType;
  486.         return $this;
  487.     }
  488.     /**
  489.      * Get mime type
  490.      *
  491.      * @return string
  492.      */
  493.     public function getMimeType(): string
  494.     {
  495.         return $this->mimeType;
  496.     }
  497.     /**
  498.      * Set status
  499.      *
  500.      * @param string $status
  501.      *
  502.      * @return File
  503.      */
  504.     public function setStatus(string $status): File
  505.     {
  506.         $this->status $status;
  507.         return $this;
  508.     }
  509.     /**
  510.      * Get status
  511.      *
  512.      * @return string
  513.      */
  514.     public function getStatus(): string
  515.     {
  516.         return $this->status;
  517.     }
  518.     /**
  519.      * Set status
  520.      *
  521.      * @param string $type
  522.      *
  523.      * @return File
  524.      */
  525.     public function setType(string $type): File
  526.     {
  527.         $this->type $type;
  528.         return $this;
  529.     }
  530.     /**
  531.      * Get status
  532.      *
  533.      * @return string|null
  534.      */
  535.     public function getType(): ?string
  536.     {
  537.         return $this->type;
  538.     }
  539.     /**
  540.      * Get deleted
  541.      *
  542.      * @return boolean
  543.      */
  544.     public function getDeleted(): bool
  545.     {
  546.         return $this->deleted;
  547.     }
  548.     /**
  549.      * Set createdAt
  550.      *
  551.      * @param DateTime $createdAt
  552.      *
  553.      * @return File
  554.      */
  555.     public function setCreatedAt(DateTime $createdAt): File
  556.     {
  557.         $this->createdAt $createdAt;
  558.         return $this;
  559.     }
  560.     /**
  561.      * Get createdAt
  562.      *
  563.      * @return DateTime
  564.      */
  565.     public function getCreatedAt(): DateTime
  566.     {
  567.         return $this->createdAt;
  568.     }
  569.     /**
  570.      * @return User
  571.      */
  572.     public function getCreatedBy(): User
  573.     {
  574.         /** @var FileAction $addAction */
  575.         $addAction $this->fileActions->filter(
  576.             function ($action) {
  577.                 return $action->getAction() == FileActionConstant::ADD;
  578.             }
  579.         )->first();
  580.         return $addAction->getUser();
  581.     }
  582.     /**
  583.      * Set url
  584.      *
  585.      * @param string $url
  586.      *
  587.      * @return File
  588.      */
  589.     public function setUrl(string $url): File
  590.     {
  591.         $this->url $url;
  592.         return $this;
  593.     }
  594.     /**
  595.      * Get real url in db field
  596.      *
  597.      * @return string|null
  598.      */
  599.     public function getUrl(): ?string
  600.     {
  601.         return $this->url;
  602.     }
  603.     /**
  604.      * Get full url
  605.      *
  606.      * @return string
  607.      */
  608.     public function getFullUrl(): string
  609.     {
  610.         $explodedUrl explode('#!'$this->url);
  611.         if (count($explodedUrl) > && $explodedUrl[0] == FilePlatformConstant::YOUTUBE){
  612.            return 'https://www.youtube.com/embed/'.$explodedUrl[1];
  613.         }
  614.         return $this->url;
  615.     }
  616.     /**
  617.      * Get platform
  618.      *
  619.      * @return string
  620.      */
  621.     public function getPlatform(): ?string
  622.     {
  623.         $explodedUrl explode('#!'$this->url);
  624.         if (count($explodedUrl) > && $explodedUrl[0] == FilePlatformConstant::YOUTUBE){
  625.            return FilePlatformConstant::YOUTUBE;
  626.         }
  627.         return null;
  628.     }
  629.     /**
  630.      * Know if file is playable
  631.      *
  632.      * @return boolean
  633.      */
  634.     public function isPlayable(): bool
  635.     {
  636.         return $this->playable !== null;
  637.     }
  638.     /**
  639.      * Set linked project
  640.      *
  641.      * @param Project|null $project
  642.      *
  643.      * @return File
  644.      */
  645.     public function setProject(?Project $project): File
  646.     {
  647.         $this->project $project;
  648.         return $this;
  649.     }
  650.     /**
  651.      * Get linked project
  652.      *
  653.      * @return Project|null
  654.      */
  655.     public function getProject(): ?Project
  656.     {
  657.         return $this->project;
  658.     }
  659.     /**
  660.      * Get instruments played by $bandMembers
  661.      *
  662.      * @return array
  663.      */
  664.     public function getInstruments(): array
  665.     {
  666.         $instrumentsOnFile = [];
  667.         $fileBandMembers $this->getBandMembers();
  668.         /** @var FileBandMember $fileBandMember */
  669.         foreach ($fileBandMembers as $fileBandMember) {
  670.             $instrumentsOnFile array_merge($instrumentsOnFile$fileBandMember->getInstruments()->toArray());
  671.         }
  672.         sort($instrumentsOnFile);
  673.         return $instrumentsOnFile;
  674.     }
  675. }