src/Entity/Saloon/Saloon.php line 40

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by simpson <simpsonwork@gmail.com>
  4.  * Date: 2019-04-15
  5.  * Time: 19:50
  6.  */
  7. namespace App\Entity\Saloon;
  8. use AngelGamez\TranslatableBundle\Entity\TranslatableValue;
  9. use App\Entity\Account\Advertiser;
  10. use App\Entity\ApartmentsPricing;
  11. use App\Entity\Account\Customer;
  12. use App\Entity\ExpressPricing;
  13. use App\Entity\IProvidedService;
  14. use App\Entity\IProvidesServices;
  15. use App\Entity\Location\City;
  16. use App\Entity\Location\Station;
  17. use App\Entity\Location\MapCoordinate;
  18. use App\Entity\Messengers;
  19. use App\Entity\PhoneCallRestrictions;
  20. use App\Entity\Sales\Saloon\AdBoardPlacement;
  21. use App\Entity\Saloon\Comment\CommentByCustomer;
  22. use App\Entity\Service;
  23. use App\Entity\Sales\Saloon\PlacementHiding;
  24. use App\Entity\ProvidedServiceTrait;
  25. use App\Entity\TakeOutPricing;
  26. use App\Repository\SaloonRepository;
  27. use Carbon\Carbon;
  28. use Carbon\CarbonImmutable;
  29. use Doctrine\Common\Collections\ArrayCollection;
  30. use Doctrine\Common\Collections\Collection;
  31. use Doctrine\ORM\Mapping as ORM;
  32. use Gedmo\Mapping\Annotation as Gedmo;
  33. use Gedmo\SoftDeleteable\Traits\SoftDeleteableEntity;
  34. #[Gedmo\SoftDeleteable(fieldName"deletedAt"timeAwaretrue)]
  35. #[ORM\Table(name'saloons')]
  36. #[ORM\Entity(repositoryClassSaloonRepository::class)]
  37. class Saloon implements IProvidesServices
  38. {
  39.     use SoftDeleteableEntity;
  40.     use ProvidedServiceTrait;
  41.     #[ORM\Id]
  42.     #[ORM\Column(name'id'type'integer')]
  43.     #[ORM\GeneratedValue(strategy'AUTO')]
  44.     protected int $id;
  45.     #[ORM\JoinColumn(name'user_id'referencedColumnName'id'nullabletrue)]
  46.     #[ORM\ManyToOne(targetEntityAdvertiser::class, inversedBy'saloons')]
  47.     protected ?Advertiser $owner;
  48.     #[ORM\OneToOne(targetEntityAdBoardPlacement::class, mappedBy'saloon'cascade: ['all'])]
  49.     protected ?AdBoardPlacement $adBoardPlacement;
  50.     #[ORM\OneToOne(targetEntityPlacementHiding::class, mappedBy'saloon'cascade: ['all'])]
  51.     protected ?PlacementHiding $placementHiding;
  52.     #[ORM\Column(name'uri_identity'type'string'length128)]
  53.     protected string $uriIdentity;
  54.     #[ORM\Column(name'name'type'translatable')]
  55.     protected TranslatableValue $name;
  56.     #[ORM\Column(name'description'type'translatable')]
  57.     protected TranslatableValue $description;
  58.     /** @var SaloonService[] */
  59.     #[ORM\OneToMany(targetEntitySaloonService::class, mappedBy'saloon'cascade: ['all'], orphanRemovaltrue)]
  60.     #[ORM\Cache(usage'NONSTRICT_READ_WRITE')]
  61.     protected Collection $providedServices;
  62.     /** @var int[] */
  63.     #[ORM\Column(name'client_types'type'simple_array'nullabletrue)]
  64.     protected ?array $clientTypes;
  65.     /**
  66.      * Номера телефонов в свободной форме написания
  67.      */
  68.     #[ORM\Column(name'phone_number'type'string'length255)]
  69.     protected string $phoneNumber;
  70.     #[ORM\Embedded(class: Messengers::class, columnPrefixfalse)]
  71.     protected ?Messengers $messengers;
  72.     #[ORM\Embedded(class: PhoneCallRestrictions::class, columnPrefixfalse)]
  73.     protected ?PhoneCallRestrictions $phoneCallRestrictions;
  74.     #[ORM\Embedded(class: WorkingHours::class, columnPrefix'working_hours_')]
  75.     protected ?WorkingHours $workingHours;
  76.     /**
  77.      * Расценки в свободной форме написания
  78.      */
  79.     #[ORM\Column(name'prices'type'text'nullabletrue)]
  80.     protected ?string $prices;
  81.     #[ORM\Embedded(class: ApartmentsPricing::class, columnPrefixfalse)]
  82.     protected ?ApartmentsPricing $apartmentsPricing;
  83.     #[ORM\Embedded(class: TakeOutPricing::class, columnPrefixfalse)]
  84.     protected ?TakeOutPricing $takeOutPricing;
  85.     #[ORM\Embedded(class: ExpressPricing::class, columnPrefixfalse)]
  86.     protected ?ExpressPricing $expressPricing;
  87.     #[ORM\Column(name'extra_charge'type'integer'nullabletrue)]
  88.     protected ?int $extraCharge;
  89.     #[ORM\OneToOne(targetEntityThumbnail::class, mappedBy'saloon'cascade: ['all'], orphanRemovaltrue)]
  90.     protected ?Thumbnail $thumbnail null;
  91.     /** @var Photo[] */
  92.     #[ORM\OneToMany(targetEntityPhoto::class, mappedBy'saloon'cascade: ['all'], orphanRemovaltrue)]
  93.     protected Collection $photos;
  94.     /** @var Video[] */
  95.     #[ORM\OneToMany(targetEntityVideo::class, mappedBy'saloon'cascade: ['all'], orphanRemovaltrue)]
  96.     protected Collection $videos;
  97.     /** @var CommentByCustomer[] */
  98.     #[ORM\OneToMany(targetEntityCommentByCustomer::class, mappedBy'saloon')]
  99.     protected Collection $comments;
  100.     #[ORM\JoinColumn(name'city_id'referencedColumnName'id')]
  101.     #[ORM\ManyToOne(targetEntityCity::class)]
  102.     protected City $city;
  103.     /** @var Station[] */
  104.     #[ORM\JoinTable(name'saloon_stations')]
  105.     #[ORM\JoinColumn(name'saloon_id'referencedColumnName'id')]
  106.     #[ORM\InverseJoinColumn(name'station_id'referencedColumnName'id')]
  107.     #[ORM\ManyToMany(targetEntityStation::class)]
  108.     protected Collection $stations;
  109.     /**
  110.      * Районы в свободной форме написания
  111.      */
  112.     #[ORM\Column(name'districts'type'string'length255nullabletrue)]
  113.     protected ?string $districts;
  114.     #[ORM\Column(name'address'type'translatable'nullabletrue)]
  115.     protected ?TranslatableValue $address;
  116.     #[ORM\Embedded(class: MapCoordinate::class, columnPrefixfalse)]
  117.     protected MapCoordinate $mapCoordinate;
  118.     #[ORM\Column(name'created_at'type'datetimetz_immutable'nullabletrue)]
  119.     protected ?\DateTimeImmutable $createdAt;
  120.     #[Gedmo\Timestampable(on"update")]
  121.     #[ORM\Column(name'updated_at'type'datetimetz_immutable'nullabletrue)]
  122.     protected ?\DateTimeImmutable $updatedAt;
  123.     #[ORM\Column(name'inactivated_at'type'datetimetz_immutable'nullabletrue)]
  124.     protected ?\DateTimeImmutable $inactivatedAt;
  125.     #[ORM\Column(name'email'type'string'length180)]
  126.     protected string $email;
  127.     private bool $draft false;
  128.     #[ORM\Column(name'seo'type'json'nullabletrue)]
  129.     private ?array $seo null;
  130.     public static function draft(?\DateTimeImmutable $createdAt null): self
  131.     {
  132.         $saloon = new static($createdAt);
  133.         return $saloon;
  134.     }
  135.     public function __construct(string $uriIdentity, ?\DateTimeImmutable $createdAt null)
  136.     {
  137.         $this->draft true;
  138.         $this->uriIdentity $uriIdentity;
  139.         $this->createdAt $createdAt ?? CarbonImmutable::now();
  140.         $this->photos = new ArrayCollection();
  141.         $this->videos = new ArrayCollection();
  142.         $this->providedServices = new ArrayCollection();
  143.     }
  144.     public function defineUriIdentity(string $uriIdentity): void
  145.     {
  146.         if (!$this->isDraft()) {
  147.             throw new \DomainException('Saloon is already created and can\'t change its URI.');
  148.         }
  149.         $this->uriIdentity $uriIdentity;
  150.         $this->draft false;
  151.     }
  152.     public function setOwner(Advertiser $owner): void
  153.     {
  154.         $this->owner $owner;
  155.     }
  156.     public function hasOwner(): bool
  157.     {
  158.         return null !== $this->owner;
  159.     }
  160.     public function setNameAndDescription(TranslatableValue $nameTranslatableValue $description): void
  161.     {
  162.         if (!empty($name)) {
  163.             $this->name $name;
  164.         }
  165.         if (!empty($description)) {
  166.             $this->description $description;
  167.         }
  168.     }
  169.     public function setLocation(City $city$stations$districts$address, ?MapCoordinate $mapCoordinate): void
  170.     {
  171.         $this->city $city;
  172.         if (null !== $stations) {
  173.             if (is_array($stations)) {
  174.                 $stations = new ArrayCollection($stations);
  175.             } elseif (!$stations instanceof ArrayCollection) {
  176.                 if (is_iterable($stations)) {
  177.                     $stations = new ArrayCollection(iterator_to_array($stations));
  178.                 } else {
  179.                     throw new \InvalidArgumentException('Stations list should be either an array or an ArrayCollection');
  180.                 }
  181.             }
  182.             $this->stations $stations;
  183.         }
  184.         if (!empty($districts)) {
  185.             $this->districts $districts;
  186.         }
  187.         if (!empty($address)) {
  188.             $this->address $address;
  189.         }
  190.         $this->mapCoordinate $mapCoordinate;
  191.     }
  192.     public function setPhoneCallOptions(string $phoneNumber, ?PhoneCallRestrictions $restrictions, ?Messengers $messengers): void
  193.     {
  194.         $this->phoneNumber $phoneNumber;
  195.         $this->phoneCallRestrictions $restrictions;
  196.         $this->messengers $messengers;
  197.     }
  198.     public function setWorkingHours(?WorkingHours $workingHours): void
  199.     {
  200. //        $this->phoneNumber = $phoneNumber;
  201.         $this->workingHours $workingHours;
  202.     }
  203.     public function setEmail(string $email): void
  204.     {
  205.         $this->email $email;
  206.     }
  207.     public function setPricing(?ApartmentsPricing $apartmentsPricing, ?TakeOutPricing $takeOutPricing, ?int $extraCharge, ?ExpressPricing $expressPricing null): void
  208.     {
  209. //        $this->prices = $pricing;
  210.         $this->apartmentsPricing $apartmentsPricing;
  211.         $this->takeOutPricing $takeOutPricing;
  212.         $this->extraCharge $extraCharge;
  213.         $this->expressPricing $expressPricing;
  214.     }
  215.     public function setUpdatedAt(\DateTimeImmutable $updatedAt): void
  216.     {
  217.         $this->updatedAt $updatedAt;
  218.     }
  219.     public function isDraft(): bool
  220.     {
  221.         return $this->draft;
  222.     }
  223.     public function isOwnedBy(Advertiser $account): bool
  224.     {
  225.         return $account->getId() === $this->owner->getId();
  226.     }
  227.     public function getId(): int
  228.     {
  229.         return $this->id;
  230.     }
  231.     public function getOwner(): ?Advertiser
  232.     {
  233.         return $this->owner;
  234.     }
  235.     public function getAdBoardPlacement(): ?AdBoardPlacement
  236.     {
  237.         return $this->adBoardPlacement;
  238.     }
  239.     /**
  240.      * Салон оплачен и выводится в общих списках на сайте
  241.      */
  242.     public function isActive(): bool
  243.     {
  244.         return null !== $this->adBoardPlacement;
  245.     }
  246.     public function getUriIdentity(): string
  247.     {
  248.         return $this->uriIdentity;
  249.     }
  250.     public function getName(): TranslatableValue
  251.     {
  252.         return $this->name;
  253.     }
  254.     public function getDescription(): TranslatableValue
  255.     {
  256.         return $this->description;
  257.     }
  258.     public function getPhoneNumber(): string
  259.     {
  260.         return $this->phoneNumber;
  261.     }
  262.     //TODO return type
  263.     public function getWorkingHours(): ?WorkingHours
  264.     {
  265.         return $this->workingHours;
  266.     }
  267.     public function getEmail(): ?string
  268.     {
  269.         return $this->email;
  270.     }
  271.     public function getPrices(): ?string
  272.     {
  273.         return $this->prices;
  274.     }
  275.     //TODO return type
  276.     public function getApartmentsPricing(): ?ApartmentsPricing
  277.     {
  278.         return $this->apartmentsPricing;
  279.     }
  280.     //TODO return type
  281.     public function getTakeOutPricing(): ?TakeOutPricing
  282.     {
  283.         return $this->takeOutPricing;
  284.     }
  285.     public function getExtraCharge(): ?int
  286.     {
  287.         return $this->extraCharge;
  288.     }
  289.     public function getThumbnail(): ?Thumbnail
  290.     {
  291.         return $this->thumbnail;
  292.     }
  293.     public function setThumbnail(string $path): void
  294.     {
  295.         $this->thumbnail = new Thumbnail($this$path);
  296.     }
  297.     /**
  298.      * @return Photo[]
  299.      */
  300.     public function getPhotos(): Collection
  301.     {
  302.         return $this->photos;
  303.     }
  304.     /**
  305.      * @return Photo[]
  306.      */
  307.     public function getVideos(): Collection
  308.     {
  309.         return $this->videos;
  310.     }
  311.     public function getCity(): City
  312.     {
  313.         return $this->city;
  314.     }
  315.     /**
  316.      * @return Station[]
  317.      */
  318.     public function getStations(): Collection
  319.     {
  320.         return $this->stations;
  321.     }
  322.     public function getDistricts(): ?string
  323.     {
  324.         return $this->districts;
  325.     }
  326.     public function getAddress(): TranslatableValue
  327.     {
  328.         return $this->address;
  329.     }
  330.     //TODO return type
  331.     public function getMapCoordinate(): ?MapCoordinate
  332.     {
  333.         return $this->mapCoordinate;
  334.     }
  335.     public function getCreatedAt(): ?\DateTimeImmutable
  336.     {
  337.         return $this->createdAt;
  338.     }
  339.     public function getUpdatedAt(): ?\DateTimeImmutable
  340.     {
  341.         return $this->updatedAt;
  342.     }
  343.     public function addPhoto(string $path): Photo
  344.     {
  345.         $photos $this->getPhotos();
  346.         $found $photos->filter(function (Photo $photo) use ($path): bool {
  347.             return $path === $photo->getPath();
  348.         });
  349.         if (!$found->isEmpty()) {
  350.             return $found->first();
  351.         }
  352.         $photo = new Photo($this$path);
  353.         $this->photos->add($photo);
  354.         return $photo;
  355.     }
  356.     public function removePhoto(string $path): void
  357.     {
  358.         foreach ($this->getPhotos() as $photo) {
  359.             if ($path === $photo->getPath()) {
  360.                 $this->photos->removeElement($photo);
  361.             }
  362.         }
  363.     }
  364.     public function addVideo(string $path): Video
  365.     {
  366.         $found $this->getVideos()->filter(function (Video $video) use ($path): bool {
  367.             return $path === $video->getPath();
  368.         });
  369.         if (!$found->isEmpty())
  370.             return $found->first();
  371.         $video = new Video($this$path);
  372.         //теперь разрешаем много видео
  373.         //$this->videos->clear();
  374.         $this->videos->add($video);
  375.         return $video;
  376.     }
  377.     public function removeVideo(string $path): void
  378.     {
  379.         foreach ($this->getVideos() as $video) {
  380.             if ($path === $video->getPath()) {
  381.                 $this->videos->removeElement($video);
  382.             }
  383.         }
  384.     }
  385.     public function isMediaProcessed(): bool
  386.     {
  387.         foreach ($this->videos as $video)
  388.             if(null === $video->getPreviewPath())
  389.                 return false;
  390.         return true;
  391.     }
  392.     public function delete(): void
  393.     {
  394.         $this->deletePlacementHiding();
  395.         $this->deleteFromAdBoard();
  396.         $this->setDeletedAt(Carbon::now());
  397.     }
  398.     public function undoDelete(): void
  399.     {
  400.         $this->setDeletedAt(); // will pass null by default
  401.     }
  402.     public function deleteFromAdBoard(): void
  403.     {
  404.         $this->adBoardPlacement null;
  405.     }
  406.     //TODO return type
  407.     public function getPhoneCallRestrictions(): ?PhoneCallRestrictions
  408.     {
  409.         return $this->phoneCallRestrictions;
  410.     }
  411.     //TODO return type
  412.     public function getExpressPricing(): ?ExpressPricing
  413.     {
  414.         return $this->expressPricing;
  415.     }
  416.     public function getClientTypes(): ?array
  417.     {
  418.         return $this->clientTypes;
  419.     }
  420.     /**
  421.      * @param int[] $clientTypes
  422.      */
  423.     public function setClientTypes(?array $clientTypes): void
  424.     {
  425.         $this->clientTypes $clientTypes;
  426.     }
  427.     //TODO return type
  428.     public function getMessengers(): ?Messengers
  429.     {
  430.         return $this->messengers;
  431.     }
  432.     public function getInactivatedAt(): ?\DateTimeImmutable
  433.     {
  434.         return $this->inactivatedAt;
  435.     }
  436.     public function setInactive(): void
  437.     {
  438.         $this->inactivatedAt CarbonImmutable::now();
  439.     }
  440.     public function undoInactive(): void
  441.     {
  442.         $this->inactivatedAt null;
  443.     }
  444.     public function getPlacementHiding(): ?PlacementHiding
  445.     {
  446.         return $this->placementHiding;
  447.     }
  448.     public function isHidden(): bool
  449.     {
  450.         return null !== $this->getPlacementHiding();
  451.     }
  452.     public function deletePlacementHiding(): void
  453.     {
  454.         $this->placementHiding null;
  455.     }
  456.     /**
  457.      * @return CommentByCustomer[]
  458.      */
  459.     public function getComments(): Collection
  460.     {
  461.         return $this->comments->filter(function(CommentByCustomer $comment): bool {
  462.             return null == $comment->getParent();
  463.         });
  464.     }
  465.     /**
  466.      * @return CommentByCustomer[]
  467.      */
  468.     public function getCommentsOrderedByNotReplied(): array
  469.     {
  470.         $comments $this->comments->filter(function(CommentByCustomer $comment): bool {
  471.             return null == $comment->getParent();
  472.         })->toArray();
  473.         usort($comments, function (CommentByCustomer $commentACommentByCustomer $commentB): int {
  474.             if((null == $commentA->getLastCommentByAdvertiser() && null == $commentB->getLastCommentByAdvertiser())
  475.                 || (null != $commentA->getLastCommentByAdvertiser() && null != $commentB->getLastCommentByAdvertiser())) {
  476.                 if($commentA->getCreatedAt() == $commentB->getCreatedAt())
  477.                     return $commentA->getId() > $commentB->getId() ? -1;
  478.                 else
  479.                     return $commentA->getCreatedAt() > $commentB->getCreatedAt() ? -1;
  480.             }
  481.             if(null == $commentA->getLastCommentByAdvertiser() && null != $commentB->getLastCommentByAdvertiser())
  482.                 return -1;
  483.             else
  484.                 return 1;
  485.         });
  486.         return $comments;
  487.     }
  488.     /**
  489.      * @return CommentByCustomer[]
  490.      */
  491.     public function getCommentsWithoutReply(): Collection
  492.     {
  493.         return $this->comments->filter(function(CommentByCustomer $comment): bool {
  494.             return null == $comment->getParent() && false == $comment->isCommentedByAdvertiser();
  495.         });
  496.     }
  497.     /**
  498.      * @return CommentByCustomer[]
  499.      */
  500.     public function getCommentsWithReply(): Collection
  501.     {
  502.         return $this->comments->filter(function(CommentByCustomer $comment): bool {
  503.             return null == $comment->getParent() && true == $comment->isCommentedByAdvertiser();
  504.         });
  505.     }
  506.     /**
  507.      * @return CommentByCustomer[]
  508.      */
  509.     public function getNewComments(): Collection
  510.     {
  511.         $weekAgo CarbonImmutable::now()->sub('7 days');
  512.         return $this->comments->filter(function(CommentByCustomer $comment) use ($weekAgo): bool {
  513.             return null == $comment->getParent()
  514.                 && (
  515.                     $comment->getCreatedAt() >= $weekAgo
  516.                     || null == $this->getCommentReply($comment)
  517.                 );
  518.         });
  519.     }
  520.     public function getOldComments(): Collection
  521.     {
  522.         $weekAgo CarbonImmutable::now()->sub('7 days');
  523.         return $this->comments->filter(function(CommentByCustomer $comment) use ($weekAgo): bool {
  524.             return null == $comment->getParent()
  525.                 && false == (
  526.                     $comment->getCreatedAt() >= $weekAgo
  527.                     || null == $this->getCommentReply($comment)
  528.                 );
  529.         });
  530.     }
  531.     private function getCommentReply(CommentByCustomer $parent): ?CommentByCustomer
  532.     {
  533.         foreach ($this->comments as $comment)
  534.             if($comment->getParent() == $parent)
  535.                 return $comment;
  536.         return null;
  537.     }
  538.     public function getCommentFromUser(Customer $user): ?CommentByCustomer
  539.     {
  540.         foreach ($this->comments as $comment)
  541.             if(null == $comment->getParent() && null != $comment->getUser() && $user->getId() == $comment->getUser()->getId())
  542.                 return $comment;
  543.         return null;
  544.     }
  545.     public function seo(): ?array
  546.     {
  547.         return $this->seo;
  548.     }
  549.     public function seoPhoneNumber(): ?string
  550.     {
  551.         return $this->seo['phone'] ?? null;
  552.     }
  553.     public function setSeoPhoneNumber(string $phoneNumber): void
  554.     {
  555.         if(null === $this->seo) {
  556.             $this->seo = [];
  557.         }
  558.         $this->seo['phone'] = $phoneNumber;
  559.     }
  560. }