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.     #[ORM\ManyToOne(targetEntityStation::class)]
  131.     #[ORM\JoinColumn(name'primary_station_id'referencedColumnName'id'nullabletrueonDelete'SET NULL')]
  132.     private ?Station $primaryStation null;
  133.     public function __construct(string $uriIdentity, ?\DateTimeImmutable $createdAt null)
  134.     {
  135.         $this->draft true;
  136.         $this->uriIdentity $uriIdentity;
  137.         $this->createdAt $createdAt ?? CarbonImmutable::now();
  138.         $this->photos = new ArrayCollection();
  139.         $this->videos = new ArrayCollection();
  140.         $this->providedServices = new ArrayCollection();
  141.     }
  142.     public static function draft(?\DateTimeImmutable $createdAt null): self
  143.     {
  144.         $saloon = new static($createdAt);
  145.         return $saloon;
  146.     }
  147.     public function defineUriIdentity(string $uriIdentity): void
  148.     {
  149.         if (!$this->isDraft()) {
  150.             throw new \DomainException('Saloon is already created and can\'t change its URI.');
  151.         }
  152.         $this->uriIdentity $uriIdentity;
  153.         $this->draft false;
  154.     }
  155.     public function isDraft(): bool
  156.     {
  157.         return $this->draft;
  158.     }
  159.     public function hasOwner(): bool
  160.     {
  161.         return null !== $this->owner;
  162.     }
  163.     public function setNameAndDescription(TranslatableValue $nameTranslatableValue $description): void
  164.     {
  165.         if (!empty($name)) {
  166.             $this->name $name;
  167.         }
  168.         if (!empty($description)) {
  169.             $this->description $description;
  170.         }
  171.     }
  172.     public function setLocation(City $city$stations$districts$address, ?MapCoordinate $mapCoordinate): void
  173.     {
  174.         $this->city $city;
  175.         if (null !== $stations) {
  176.             if (is_array($stations)) {
  177.                 $stations = new ArrayCollection($stations);
  178.             } elseif (!$stations instanceof ArrayCollection) {
  179.                 if (is_iterable($stations)) {
  180.                     $stations = new ArrayCollection(iterator_to_array($stations));
  181.                 } else {
  182.                     throw new \InvalidArgumentException('Stations list should be either an array or an ArrayCollection');
  183.                 }
  184.             }
  185.             $this->stations $stations;
  186.         }
  187.         if (!empty($districts)) {
  188.             $this->districts $districts;
  189.         }
  190.         if (!empty($address)) {
  191.             $this->address $address;
  192.         }
  193.         $this->mapCoordinate $mapCoordinate;
  194.         $this->normalizePrimaryStation();
  195.     }
  196.     private function normalizePrimaryStation(): void
  197.     {
  198.         if ($this->stations->isEmpty()) {
  199.             $this->primaryStation null;
  200.             return;
  201.         }
  202.         if ($this->primaryStation === null || !$this->stations->contains($this->primaryStation)) {
  203.             $this->primaryStation $this->stations->first();
  204.         }
  205.     }
  206.     public function setPhoneCallOptions(string $phoneNumber, ?PhoneCallRestrictions $restrictions, ?Messengers $messengers): void
  207.     {
  208.         $this->phoneNumber $phoneNumber;
  209.         $this->phoneCallRestrictions $restrictions;
  210.         $this->messengers $messengers;
  211.     }
  212.     public function setPricing(?ApartmentsPricing $apartmentsPricing, ?TakeOutPricing $takeOutPricing, ?int $extraCharge, ?ExpressPricing $expressPricing null): void
  213.     {
  214. //        $this->prices = $pricing;
  215.         $this->apartmentsPricing $apartmentsPricing;
  216.         $this->takeOutPricing $takeOutPricing;
  217.         $this->extraCharge $extraCharge;
  218.         $this->expressPricing $expressPricing;
  219.     }
  220.     public function isOwnedBy(Advertiser $account): bool
  221.     {
  222.         return $account->getId() === $this->owner->getId();
  223.     }
  224.     public function getId(): int
  225.     {
  226.         return $this->id;
  227.     }
  228.     public function getOwner(): ?Advertiser
  229.     {
  230.         return $this->owner;
  231.     }
  232.     public function setOwner(Advertiser $owner): void
  233.     {
  234.         $this->owner $owner;
  235.     }
  236.     public function getAdBoardPlacement(): ?AdBoardPlacement
  237.     {
  238.         return $this->adBoardPlacement;
  239.     }
  240.     /**
  241.      * Салон оплачен и выводится в общих списках на сайте
  242.      */
  243.     public function isActive(): bool
  244.     {
  245.         return null !== $this->adBoardPlacement;
  246.     }
  247.     public function getUriIdentity(): string
  248.     {
  249.         return $this->uriIdentity;
  250.     }
  251.     public function getName(): TranslatableValue
  252.     {
  253.         return $this->name;
  254.     }
  255.     public function getDescription(): TranslatableValue
  256.     {
  257.         return $this->description;
  258.     }
  259.     public function getPhoneNumber(): string
  260.     {
  261.         return $this->phoneNumber;
  262.     }
  263.     public function getWorkingHours(): ?WorkingHours
  264.     {
  265.         return $this->workingHours;
  266.     }
  267.     public function setWorkingHours(?WorkingHours $workingHours): void
  268.     {
  269. //        $this->phoneNumber = $phoneNumber;
  270.         $this->workingHours $workingHours;
  271.     }
  272.     //TODO return type
  273.     public function getEmail(): ?string
  274.     {
  275.         return $this->email;
  276.     }
  277.     public function setEmail(string $email): void
  278.     {
  279.         $this->email $email;
  280.     }
  281.     public function getPrices(): ?string
  282.     {
  283.         return $this->prices;
  284.     }
  285.     //TODO return type
  286.     public function getApartmentsPricing(): ?ApartmentsPricing
  287.     {
  288.         return $this->apartmentsPricing;
  289.     }
  290.     //TODO return type
  291.     public function getTakeOutPricing(): ?TakeOutPricing
  292.     {
  293.         return $this->takeOutPricing;
  294.     }
  295.     public function getExtraCharge(): ?int
  296.     {
  297.         return $this->extraCharge;
  298.     }
  299.     public function getThumbnail(): ?Thumbnail
  300.     {
  301.         return $this->thumbnail;
  302.     }
  303.     public function setThumbnail(string $path): void
  304.     {
  305.         $this->thumbnail = new Thumbnail($this$path);
  306.     }
  307.     public function getCity(): City
  308.     {
  309.         return $this->city;
  310.     }
  311.     /**
  312.      * @return Station[]
  313.      */
  314.     public function getStations(): Collection
  315.     {
  316.         return $this->stations;
  317.     }
  318.     public function getDistricts(): ?string
  319.     {
  320.         return $this->districts;
  321.     }
  322.     public function getAddress(): TranslatableValue
  323.     {
  324.         return $this->address;
  325.     }
  326.     public function getMapCoordinate(): ?MapCoordinate
  327.     {
  328.         return $this->mapCoordinate;
  329.     }
  330.     public function getUpdatedAt(): ?\DateTimeImmutable
  331.     {
  332.         return $this->updatedAt;
  333.     }
  334.     //TODO return type
  335.     public function setUpdatedAt(\DateTimeImmutable $updatedAt): void
  336.     {
  337.         $this->updatedAt $updatedAt;
  338.     }
  339.     public function addPhoto(string $path): Photo
  340.     {
  341.         $photos $this->getPhotos();
  342.         $found $photos->filter(function (Photo $photo) use ($path): bool {
  343.             return $path === $photo->getPath();
  344.         });
  345.         if (!$found->isEmpty()) {
  346.             return $found->first();
  347.         }
  348.         $photo = new Photo($this$path);
  349.         $this->photos->add($photo);
  350.         return $photo;
  351.     }
  352.     /**
  353.      * @return Photo[]
  354.      */
  355.     public function getPhotos(): Collection
  356.     {
  357.         return $this->photos;
  358.     }
  359.     public function removePhoto(string $path): void
  360.     {
  361.         foreach ($this->getPhotos() as $photo) {
  362.             if ($path === $photo->getPath()) {
  363.                 $this->photos->removeElement($photo);
  364.             }
  365.         }
  366.     }
  367.     public function addVideo(string $path): Video
  368.     {
  369.         $found $this->getVideos()->filter(function (Video $video) use ($path): bool {
  370.             return $path === $video->getPath();
  371.         });
  372.         if (!$found->isEmpty())
  373.             return $found->first();
  374.         $video = new Video($this$path);
  375.         //теперь разрешаем много видео
  376.         //$this->videos->clear();
  377.         $this->videos->add($video);
  378.         return $video;
  379.     }
  380.     /**
  381.      * @return Photo[]
  382.      */
  383.     public function getVideos(): Collection
  384.     {
  385.         return $this->videos;
  386.     }
  387.     public function removeVideo(string $path): void
  388.     {
  389.         foreach ($this->getVideos() as $video) {
  390.             if ($path === $video->getPath()) {
  391.                 $this->videos->removeElement($video);
  392.             }
  393.         }
  394.     }
  395.     public function isMediaProcessed(): bool
  396.     {
  397.         foreach ($this->videos as $video)
  398.             if (null === $video->getPreviewPath())
  399.                 return false;
  400.         return true;
  401.     }
  402.     public function delete(): void
  403.     {
  404.         $this->deletePlacementHiding();
  405.         $this->deleteFromAdBoard();
  406.         $this->setDeletedAt(Carbon::now());
  407.     }
  408.     public function deletePlacementHiding(): void
  409.     {
  410.         $this->placementHiding null;
  411.     }
  412.     public function deleteFromAdBoard(): void
  413.     {
  414.         $this->adBoardPlacement null;
  415.     }
  416.     //TODO return type
  417.     public function undoDelete(): void
  418.     {
  419.         $this->setDeletedAt(); // will pass null by default
  420.     }
  421.     //TODO return type
  422.     public function getPhoneCallRestrictions(): ?PhoneCallRestrictions
  423.     {
  424.         return $this->phoneCallRestrictions;
  425.     }
  426.     public function getExpressPricing(): ?ExpressPricing
  427.     {
  428.         return $this->expressPricing;
  429.     }
  430.     public function getClientTypes(): ?array
  431.     {
  432.         return $this->clientTypes;
  433.     }
  434.     //TODO return type
  435.     /**
  436.      * @param int[] $clientTypes
  437.      */
  438.     public function setClientTypes(?array $clientTypes): void
  439.     {
  440.         $this->clientTypes $clientTypes;
  441.     }
  442.     public function getMessengers(): ?Messengers
  443.     {
  444.         return $this->messengers;
  445.     }
  446.     public function getInactivatedAt(): ?\DateTimeImmutable
  447.     {
  448.         return $this->inactivatedAt;
  449.     }
  450.     public function setInactive(): void
  451.     {
  452.         $this->inactivatedAt CarbonImmutable::now();
  453.     }
  454.     public function undoInactive(): void
  455.     {
  456.         $this->inactivatedAt null;
  457.     }
  458.     public function isHidden(): bool
  459.     {
  460.         return null !== $this->getPlacementHiding();
  461.     }
  462.     public function getPlacementHiding(): ?PlacementHiding
  463.     {
  464.         return $this->placementHiding;
  465.     }
  466.     /**
  467.      * @return CommentByCustomer[]
  468.      */
  469.     public function getComments(): Collection
  470.     {
  471.         return $this->comments->filter(function (CommentByCustomer $comment): bool {
  472.             return null == $comment->getParent();
  473.         });
  474.     }
  475.     /**
  476.      * @return CommentByCustomer[]
  477.      */
  478.     public function getCommentsOrderedByNotReplied(): array
  479.     {
  480.         $comments $this->comments->filter(function (CommentByCustomer $comment): bool {
  481.             return null == $comment->getParent();
  482.         })->toArray();
  483.         usort($comments, function (CommentByCustomer $commentACommentByCustomer $commentB): int {
  484.             if ((null == $commentA->getLastCommentByAdvertiser() && null == $commentB->getLastCommentByAdvertiser())
  485.                 || (null != $commentA->getLastCommentByAdvertiser() && null != $commentB->getLastCommentByAdvertiser())) {
  486.                 if ($commentA->getCreatedAt() == $commentB->getCreatedAt())
  487.                     return $commentA->getId() > $commentB->getId() ? -1;
  488.                 else
  489.                     return $commentA->getCreatedAt() > $commentB->getCreatedAt() ? -1;
  490.             }
  491.             if (null == $commentA->getLastCommentByAdvertiser() && null != $commentB->getLastCommentByAdvertiser())
  492.                 return -1;
  493.             else
  494.                 return 1;
  495.         });
  496.         return $comments;
  497.     }
  498.     public function getCreatedAt(): ?\DateTimeImmutable
  499.     {
  500.         return $this->createdAt;
  501.     }
  502.     /**
  503.      * @return CommentByCustomer[]
  504.      */
  505.     public function getCommentsWithoutReply(): Collection
  506.     {
  507.         return $this->comments->filter(function (CommentByCustomer $comment): bool {
  508.             return null == $comment->getParent() && false == $comment->isCommentedByAdvertiser();
  509.         });
  510.     }
  511.     /**
  512.      * @return CommentByCustomer[]
  513.      */
  514.     public function getCommentsWithReply(): Collection
  515.     {
  516.         return $this->comments->filter(function (CommentByCustomer $comment): bool {
  517.             return null == $comment->getParent() && true == $comment->isCommentedByAdvertiser();
  518.         });
  519.     }
  520.     /**
  521.      * @return CommentByCustomer[]
  522.      */
  523.     public function getNewComments(): Collection
  524.     {
  525.         $weekAgo CarbonImmutable::now()->sub('7 days');
  526.         return $this->comments->filter(function (CommentByCustomer $comment) use ($weekAgo): bool {
  527.             return null == $comment->getParent()
  528.                 && (
  529.                     $comment->getCreatedAt() >= $weekAgo
  530.                     || null == $this->getCommentReply($comment)
  531.                 );
  532.         });
  533.     }
  534.     private function getCommentReply(CommentByCustomer $parent): ?CommentByCustomer
  535.     {
  536.         foreach ($this->comments as $comment)
  537.             if ($comment->getParent() == $parent)
  538.                 return $comment;
  539.         return null;
  540.     }
  541.     public function getOldComments(): Collection
  542.     {
  543.         $weekAgo CarbonImmutable::now()->sub('7 days');
  544.         return $this->comments->filter(function (CommentByCustomer $comment) use ($weekAgo): bool {
  545.             return null == $comment->getParent()
  546.                 && false == (
  547.                     $comment->getCreatedAt() >= $weekAgo
  548.                     || null == $this->getCommentReply($comment)
  549.                 );
  550.         });
  551.     }
  552.     public function getCommentFromUser(Customer $user): ?CommentByCustomer
  553.     {
  554.         foreach ($this->comments as $comment)
  555.             if (null == $comment->getParent() && null != $comment->getUser() && $user->getId() == $comment->getUser()->getId())
  556.                 return $comment;
  557.         return null;
  558.     }
  559.     public function seo(): ?array
  560.     {
  561.         return $this->seo;
  562.     }
  563.     public function seoPhoneNumber(): ?string
  564.     {
  565.         return $this->seo['phone'] ?? null;
  566.     }
  567.     public function setSeoPhoneNumber(string $phoneNumber): void
  568.     {
  569.         if (null === $this->seo) {
  570.             $this->seo = [];
  571.         }
  572.         $this->seo['phone'] = $phoneNumber;
  573.     }
  574.     public function getPrimaryStation(): ?Station
  575.     {
  576.         return $this->primaryStation;
  577.     }
  578.     public function setPrimaryStation(?Station $station): void
  579.     {
  580.         $this->primaryStation $station;
  581.         $this->normalizePrimaryStation();
  582.     }
  583. }