src/Entity/Profile/Profile.php line 72

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by simpson <simpsonwork@gmail.com>
  4.  * Date: 2019-03-19
  5.  * Time: 15:36
  6.  */
  7. namespace App\Entity\Profile;
  8. use AngelGamez\TranslatableBundle\Entity\TranslatableValue;
  9. //use ApiPlatform\Core\Annotation\ApiProperty;
  10. use App\Entity\Account\Advertiser;
  11. use App\Entity\ApartmentsPricing;
  12. use App\Entity\ContainsDomainEvents;
  13. use App\Entity\Account\Customer;
  14. use App\Entity\DomainEventsRecorderTrait;
  15. use App\Entity\ExpressPricing;
  16. use App\Entity\IProvidesServices;
  17. use App\Entity\Location\City;
  18. use App\Entity\Location\MapCoordinate;
  19. use App\Entity\Location\Station;
  20. use App\Entity\Messengers;
  21. use App\Entity\PhoneCallRestrictions;
  22. use App\Entity\Profile\Comment\CommentByCustomer;
  23. use App\Entity\Profile\Confirmation\ModerationRequest;
  24. use App\Entity\Sales\Profile\AdBoardPlacement;
  25. use App\Entity\Sales\Profile\PlacementHiding;
  26. use App\Entity\Sales\Profile\TopPlacement;
  27. use App\Entity\ProvidedServiceTrait;
  28. use App\Entity\TakeOutPricing;
  29. use App\Repository\ProfileRepository;
  30. use Carbon\Carbon;
  31. use Carbon\CarbonImmutable;
  32. use Doctrine\Common\Collections\ArrayCollection;
  33. use Doctrine\Common\Collections\Collection;
  34. use Doctrine\ORM\Mapping as ORM;
  35. use Doctrine\ORM\Mapping\Index;
  36. //use ApiPlatform\Core\Annotation\ApiResource;
  37. //use ApiPlatform\Core\Annotation\ApiFilter;
  38. //use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
  39. //use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\RangeFilter;
  40. use Gedmo\SoftDeleteable\Traits\SoftDeleteableEntity;
  41. use Symfony\Component\Serializer\Annotation\Groups;
  42. use Gedmo\Mapping\Annotation as Gedmo;
  43. use App\Validator\Constraints\ValidPhoneForCountry as ValidPhoneForCountryAssert;
  44. use App\Validator\Constraints\PhoneNotBlack as PhoneNotBlackAssert;
  45. /**
  46.  * ApiResource(collectionOperations={"get"}, itemOperations={"get"}, normalizationContext={"groups"={"profile"}}, attributes={"pagination_client_enabled"=true, "pagination_client_items_per_page"=true})
  47.  * ApiFilter(SearchFilter::class, properties={"city": "exact", "providedServices": "exact"})
  48.  * ApiFilter(RangeFilter::class, properties={"personParameters.age", "personParameters.height", "personParameters.weight", "personParameters.breastSize", "apartmentsPricing.oneHourPrice"})
  49.  * @ValidPhoneForCountryAssert/ProtocolClass
  50.  * @PhoneNotBlackAssert/ProtocolClass
  51.  */
  52. #[Gedmo\SoftDeleteable(fieldName"deletedAt"timeAwaretrue)]
  53. #[ORM\Table(name'profiles')]
  54. #[Index(name'idx_deleted_at'columns: ['deleted_at'])]
  55. #[Index(name'idx_created_at'columns: ['created_at'])]
  56. #[Index(name'idx_gender'columns: ['person_gender'])]
  57. #[Index(name'idx_apartments_one_hour_price'columns: ['apartments_one_hour_price'])]
  58. #[Index(name'idx_is_dummy'columns: ['is_dummy'])]
  59. #[Index(name'idx_city_deleted'columns: ['city_id''deleted_at'])]
  60. #[Index(name'idx_city_deleted_moderation'columns: ['city_id''deleted_at''moderation_status'])]
  61. #[Index(name'idx_city_masseur_deleted'columns: ['city_id''is_masseur''deleted_at'])]
  62. #[Index(name'idx_city_masseur_deleted_moderation'columns: ['city_id''is_masseur''deleted_at''moderation_status'])]
  63. #[Index(name'idx_city_deleted_gender'columns: ['city_id''deleted_at''person_gender'])]
  64. #[Index(name'idx_city_deleted_moderation_gender'columns: ['city_id''deleted_at''moderation_status''person_gender'])]
  65. #[Index(name'idx_city_masseur_deleted_gender'columns: ['city_id''is_masseur''deleted_at''person_gender'])]
  66. #[Index(name'idx_city_masseur_deleted_moderation_gender'columns: ['city_id''is_masseur''deleted_at''moderation_status''person_gender'])]
  67. #[ORM\Entity(repositoryClassProfileRepository::class)]
  68. #[ORM\HasLifecycleCallbacks]
  69. class Profile implements ContainsDomainEventsIProvidesServices
  70. {
  71.     use SoftDeleteableEntity;
  72.     use DomainEventsRecorderTrait;
  73.     use ProvidedServiceTrait;
  74.     const MODERATION_STATUS_NOT_PASSED 0;
  75.     const MODERATION_STATUS_APPROVED 1;
  76.     const MODERATION_STATUS_WAITING 2;
  77.     const MODERATION_STATUS_REJECTED 3;
  78.     #[ORM\Id]
  79.     #[ORM\Column(name'id'type'integer')]
  80.     #[ORM\GeneratedValue(strategy'AUTO')]
  81.     #[Groups('profile')]
  82.     protected int $id;
  83.     #[ORM\JoinColumn(name'user_id'referencedColumnName'id'nullabletrue)]
  84.     #[ORM\ManyToOne(targetEntityAdvertiser::class, inversedBy'profiles')]
  85.     protected ?Advertiser $owner;
  86.     #[ORM\Column(name'is_dummy'type'boolean'options: ['default' => 0])]
  87.     protected bool $dummy false;
  88.     /** @var TopPlacement[] */
  89.     #[ORM\OneToMany(targetEntityTopPlacement::class, mappedBy'profile'cascade: ['all'], orphanRemovaltrue)]
  90.     protected Collection $topPlacements;
  91.     #[ORM\OneToOne(targetEntityAdBoardPlacement::class, mappedBy'profile'cascade: ['all'], orphanRemovaltrue)]
  92.     protected ?AdBoardPlacement $adBoardPlacement null;
  93.     #[ORM\OneToOne(targetEntityPlacementHiding::class, mappedBy'profile'cascade: ['all'])]
  94.     protected ?PlacementHiding $placementHiding null;
  95.     #[ORM\Column(name'uri_identity'type'string'length64)]
  96.     #[Groups('profile')]
  97.     protected string $uriIdentity;
  98.     #[ORM\Column(name'name'type'translatable')]
  99.     #[Groups('profile')]
  100.     protected TranslatableValue $name;
  101.     #[ORM\Column(name'description'type'translatable')]
  102.     #[Groups('profile')]
  103.     protected ?TranslatableValue $description null;
  104.     #[ORM\Embedded(class: PersonParameters::class, columnPrefix'person_')]
  105.     #[Groups('profile')]
  106.     protected PersonParameters $personParameters;
  107.     /** var ProfileService[] */
  108.     #[ORM\OneToMany(targetEntityProfileService::class, mappedBy'profile'cascade: ['all'], orphanRemovaltrue)]
  109.     #[ORM\Cache(usage'NONSTRICT_READ_WRITE'region'profiles')]
  110.     protected Collection $providedServices;
  111.     /** @var int[] */
  112.     #[ORM\Column(name'client_types'type'simple_array'nullabletrue)]
  113.     protected ?array $clientTypes;
  114.     #[ORM\Column(name'phone_number'type'string'length24)]
  115.     #[Groups('profile')]
  116.     protected string $phoneNumber;
  117.     #[ORM\Embedded(class: Messengers::class, columnPrefixfalse)]
  118.     #[Groups('profile')]
  119.     protected ?Messengers $messengers null;
  120.     #[ORM\Embedded(class: PhoneCallRestrictions::class, columnPrefixfalse)]
  121.     protected ?PhoneCallRestrictions $phoneCallRestrictions null;
  122.     #[ORM\Column(name'is_masseur'type'boolean')]
  123.     protected bool $masseur false;
  124.     #[ORM\Embedded(class: ClientRestrictions::class, columnPrefixfalse)]
  125.     protected ?ClientRestrictions $clientRestrictions null;
  126.     #[ORM\Embedded(class: ApartmentsPricing::class, columnPrefixfalse)]
  127.     #[Groups('profile')]
  128.     protected ?ApartmentsPricing $apartmentsPricing null;
  129.     #[ORM\Embedded(class: TakeOutPricing::class, columnPrefixfalse)]
  130.     #[Groups('profile')]
  131.     protected ?TakeOutPricing $takeOutPricing null;
  132.     #[ORM\Embedded(class: ExpressPricing::class, columnPrefixfalse)]
  133.     #[Groups('profile')]
  134.     protected ?ExpressPricing $expressPricing null;
  135.     #[ORM\Embedded(class: CarPricing::class, columnPrefixfalse)]
  136.     #[Groups('profile')]
  137.     protected ?CarPricing $carPricing null;
  138.     #[ORM\Column(name'extra_charge'type'integer'nullabletrue)]
  139.     #[Groups('profile')]
  140.     protected ?int $extraCharge;
  141.     /** @var Photo[] */
  142.     #[ORM\OneToMany(targetEntityPhoto::class, mappedBy'profile'cascade: ['all'], orphanRemovaltrue)]
  143.     #[Groups('profile')]
  144.     protected Collection $photos;
  145.     /** @var Selfie[] */
  146.     #[ORM\OneToMany(targetEntitySelfie::class, mappedBy'profile'cascade: ['all'], orphanRemovaltrue)]
  147.     #[Groups('profile')]
  148.     protected Collection $selfies;
  149.     /** @var Video[] */
  150.     #[ORM\OneToMany(targetEntityVideo::class, mappedBy'profile'cascade: ['all'], orphanRemovaltrue)]
  151.     protected Collection $videos;
  152.     #[ORM\OneToMany(targetEntityFileProcessingTask::class, mappedBy'profile'cascade: ['all'], orphanRemovaltrue)]
  153.     protected Collection $processingFiles;
  154.     #[ORM\OneToOne(targetEntityAdminApprovalPhoto::class, mappedBy'profile'cascade: ['all'], orphanRemovaltrue)]
  155.     protected ?AdminApprovalPhoto $adminApprovalPhoto null;
  156.     #[ORM\OneToOne(targetEntityAvatar::class, mappedBy'profile'cascade: ['all'], orphanRemovaltrue)]
  157.     protected ?Avatar $avatar null;
  158.     /** @var CommentByCustomer[] */
  159.     #[ORM\OneToMany(targetEntityCommentByCustomer::class, mappedBy'profile')]
  160.     protected Collection $comments;
  161.     #[ORM\Column(name'is_approved'type'boolean')]
  162.     #[Groups('profile')]
  163.     protected bool $approved false;
  164.     #[ORM\Column(name'moderation_status'type'integer')]
  165.     #[Groups('profile')]
  166.     protected int $moderationStatus 0;
  167.     #[ORM\JoinColumn(name'city_id'referencedColumnName'id')]
  168.     #[ORM\ManyToOne(targetEntityCity::class)]
  169.     #[ORM\Cache(usage'NONSTRICT_READ_WRITE'region'profiles')]
  170.     protected City $city;
  171.     /** @var Station[] */
  172.     //, indexBy="id"
  173.     #[ORM\JoinTable(name'profile_stations')]
  174.     #[ORM\JoinColumn(name'profile_id'referencedColumnName'id')]
  175.     #[ORM\InverseJoinColumn(name'station_id'referencedColumnName'id')]
  176.     #[ORM\ManyToMany(targetEntityStation::class)]
  177.     #[Groups('profile')]
  178.     #[ORM\Cache(usage'NONSTRICT_READ_WRITE'region'profiles')]
  179.     protected Collection $stations;
  180.     #[ORM\Embedded(class: MapCoordinate::class, columnPrefixfalse)] // ApiProperty()
  181.     #[Groups('profile')]
  182.     protected ?MapCoordinate $mapCoordinate;
  183.     #[ORM\Column(name'created_at'type'datetimetz_immutable'nullabletrue)]
  184.     protected ?\DateTimeImmutable $createdAt;
  185.     #[Gedmo\Timestampable(on:"change"field:["name""description""personParameters""providedServices""clientTypes""phoneNumber""messengers""phoneCallrestrictions""masseur""clientRestrictions""apartmentsPricing""takeOutPricing""expressPricing""carPricing""extraCharge""photos""selfies""videos""avatar""stations""mapCoordinate"])]
  186.     #[ORM\Column(name'updated_at'type'datetimetz_immutable'nullabletrue)]
  187.     #[Groups('profile')]
  188.     protected ?\DateTimeImmutable $updatedAt;
  189.     #[ORM\Column(name'inactivated_at'type'datetimetz_immutable'nullabletrue)]
  190.     protected ?\DateTimeImmutable $inactivatedAt;
  191.     private bool $draft false;
  192.     #[ORM\Column(name'seo'type'json'nullabletrue)]
  193.     #[Groups('profile')]
  194.     private ?array $seo null;
  195.     public static function draft(?\DateTimeImmutable $createdAt null, ?bool $dummy null): self
  196.     {
  197.         $profile = new static($createdAt);
  198.         if(null !== $dummy)
  199.             $profile->dummy $dummy;
  200.         return $profile;
  201.     }
  202.     public static function create(string $uriIdentity, ?\DateTimeImmutable $createdAt null): self
  203.     {
  204.         $profile = new static($createdAt);
  205.         $profile->defineUriIdentity($uriIdentity);
  206.         $profile->toggleMasseur(false);
  207.         return $profile;
  208.     }
  209.     public static function createMasseur(string $uriIdentity, ?\DateTimeImmutable $createdAt null): self
  210.     {
  211.         $profile = new static($createdAt);
  212.         $profile->defineUriIdentity($uriIdentity);
  213.         $profile->toggleMasseur(true);
  214.         return $profile;
  215.     }
  216.     protected function __construct(?\DateTimeImmutable $createdAt)
  217.     {
  218.         $this->draft true;
  219.         $this->createdAt $createdAt;
  220.         $this->photos = new ArrayCollection();
  221.         $this->selfies = new ArrayCollection();
  222.         $this->videos = new ArrayCollection();
  223.         $this->processingFiles = new ArrayCollection();
  224.         $this->comments = new ArrayCollection();
  225.         $this->topPlacements = new ArrayCollection();
  226.         $this->providedServices = new ArrayCollection();
  227.         $this->stations = new ArrayCollection();
  228.         $this->inactivatedAt CarbonImmutable::now();
  229.     }
  230.     public function isDraft(): bool
  231.     {
  232.         return $this->draft;
  233.     }
  234.     public function isOwnedBy(Advertiser $account): bool
  235.     {
  236.         return $account->getId() === $this->owner->getId();
  237.     }
  238.     public function defineUriIdentity(string $uriIdentity): void
  239.     {
  240.         if (!$this->isDraft()) {
  241.             throw new \DomainException('Profile is already created and can\'t change its URI.');
  242.         }
  243.         $this->uriIdentity $uriIdentity;
  244.         $this->draft false;
  245.     }
  246.     public function toggleMasseur(bool $isMasseur): void
  247.     {
  248.         if ($this->masseur !== $isMasseur && (null !== $this->adBoardPlacement && false == $this->adBoardPlacement->getType()->isFree())) {
  249.             throw new \DomainException('Impossible to toggle profile type while it is displaying on adboard.');
  250.         }
  251.         $this->masseur $isMasseur;
  252.     }
  253.     public function setBio(TranslatableValue $nameTranslatableValue $description): void
  254.     {
  255.         $this->name $name;
  256.         $this->description $description;
  257.     }
  258.     public function setLocation(City $city$stations, ?MapCoordinate $mapCoordinate): void
  259.     {
  260.         if (!$this->isDraft() && !$this->city->equals($city)) {
  261.             throw new \DomainException('City change for a saved profile is forbidden.');
  262.         }
  263.         $this->city $city;
  264.         $this->changeStations($stations);
  265.         $this->mapCoordinate $mapCoordinate;
  266.     }
  267.     protected function changeStations($stations)
  268.     {
  269.         if(null === $stations)
  270.             return;
  271.         if(false === is_array($stations) && false === is_iterable($stations))
  272.             throw new \InvalidArgumentException('Stations list should be either an array or an ArrayCollection');
  273.         $stationsArray is_iterable($stations) && !is_array($stations) ? iterator_to_array($stations) : $stations;
  274.         $stations = [];
  275.         foreach ($stationsArray as $station) {
  276.             $stations[$station->getId()] = $station;
  277.         }
  278.         $stationIds array_map(function (Station $station): int {
  279.             return $station->getId();
  280.         }, $stations);
  281.         $existingStationIds $this->stations->map(function (Station $station): int {
  282.             return $station->getId();
  283.         })->getValues();
  284.         $stationIdsToAdd array_diff($stationIds$existingStationIds);
  285.         $stationIdsToRemove array_diff($existingStationIds$stationIds);
  286.         foreach ($stationIdsToAdd as $stationId) {
  287.             $this->stations->add($stations[$stationId]);
  288.         }
  289.         foreach ($stationIdsToRemove as $stationId) {
  290.             $this->stations->remove($stationId);
  291.         }
  292.     }
  293.     public function setPersonParameters(PersonParameters $personParameters): void
  294.     {
  295.         $this->personParameters $personParameters;
  296.     }
  297.     public function setEnabledProvidedServices($services): void
  298.     {
  299.         if (null !== $services) {
  300.             if (is_array($services)) {
  301.                 $services = new ArrayCollection($services);
  302.             } elseif (!$services instanceof ArrayCollection) {
  303.                 if (is_iterable($services)) {
  304.                     $services = new ArrayCollection(iterator_to_array($services));
  305.                 } else {
  306.                     throw new \InvalidArgumentException('Services list should be either an array or an ArrayCollection');
  307.                 }
  308.             }
  309.             $this->providedServices $services;
  310.         }
  311.     }
  312.     public function setPhoneCallOptions(string $phoneNumber, ?PhoneCallRestrictions $restrictions, ?Messengers $messengers): void
  313.     {
  314.         $this->phoneNumber $phoneNumber;
  315.         $this->phoneCallRestrictions $restrictions;
  316.         $this->messengers $messengers;
  317.     }
  318.     public function setClientRestrictions(?ClientRestrictions $restrictions): void
  319.     {
  320.         $this->clientRestrictions $restrictions;
  321.     }
  322.     public function setPricing(?ApartmentsPricing $apartmentsPricing, ?TakeOutPricing $takeOutPricing, ?int $extraCharge, ?ExpressPricing $expressPricing null, ?CarPricing $carPricing null): void
  323.     {
  324.         $this->apartmentsPricing $apartmentsPricing;
  325.         $this->takeOutPricing $takeOutPricing;
  326.         $this->extraCharge $extraCharge;
  327.         $this->expressPricing $expressPricing;
  328.         $this->carPricing $carPricing;
  329.     }
  330.     public function setUpdatedAt(\DateTimeImmutable $updatedAt): void
  331.     {
  332.         $this->updatedAt $updatedAt;
  333.     }
  334.     public function isApproved(): bool
  335.     {
  336.         return $this->approved;
  337.     }
  338.     public function approve(): void
  339.     {
  340.         $this->approved true;
  341.     }
  342.     public function unApprove(): void
  343.     {
  344.         $this->approved false;
  345.     }
  346.     public function getId(): int
  347.     {
  348.         return $this->id;
  349.     }
  350.     public function getOwner(): ?Advertiser
  351.     {
  352.         return $this->owner;
  353.     }
  354.     public function setOwner(Advertiser $owner): void
  355.     {
  356.         $this->owner $owner;
  357.     }
  358.     public function hasOwner(): bool
  359.     {
  360.         return null !== $this->owner;
  361.     }
  362.     public function getTopPlacements(): Collection
  363.     {
  364.         return $this->topPlacements;
  365.     }
  366.     public function addTopPlacement(TopPlacement $topPlacement): void
  367.     {
  368.         $this->topPlacements->add($topPlacement);
  369.     }
  370.     public function getAdBoardPlacement(): ?AdBoardPlacement
  371.     {
  372.         return $this->adBoardPlacement;
  373.     }
  374.     public function setAdBoardPlacement(AdBoardPlacement $adBoardPlacement): void
  375.     {
  376.         $this->adBoardPlacement $adBoardPlacement;
  377.     }
  378.     /**
  379.      * Анкета оплачена и выводится в общих списках на сайте
  380.      * или в ТОПе, то есть "АКТИВНА"
  381.      */
  382.     public function isActive(): bool
  383.     {
  384.         return null !== $this->adBoardPlacement || $this->hasRunningTopPlacement();
  385.     }
  386.     public function getUriIdentity(): string
  387.     {
  388.         return $this->uriIdentity;
  389.     }
  390.     public function getName(): TranslatableValue
  391.     {
  392.         return $this->name;
  393.     }
  394.     public function getDescription(): ?TranslatableValue
  395.     {
  396.         return $this->description;
  397.     }
  398.     public function getPersonParameters(): PersonParameters
  399.     {
  400.         return $this->personParameters;
  401.     }
  402.     public function getPhoneNumber(): string
  403.     {
  404.         return $this->phoneNumber;
  405.     }
  406.     //TODO return type
  407.     public function getPhoneCallRestrictions(): ?PhoneCallRestrictions
  408.     {
  409.         return $this->phoneCallRestrictions;
  410.     }
  411.     public function isMasseur(): bool
  412.     {
  413.         return $this->masseur;
  414.     }
  415.     //TODO return type
  416.     public function getClientRestrictions(): ?ClientRestrictions
  417.     {
  418.         return $this->clientRestrictions;
  419.     }
  420.     //TODO return type
  421.     public function getApartmentsPricing(): ?ApartmentsPricing
  422.     {
  423.         return $this->apartmentsPricing;
  424.     }
  425.     //TODO return type
  426.     public function getTakeOutPricing(): ?TakeOutPricing
  427.     {
  428.         return $this->takeOutPricing;
  429.     }
  430.     public function getExtraCharge(): ?int
  431.     {
  432.         return $this->extraCharge;
  433.     }
  434.     /**
  435.      * @return Photo[]
  436.      */
  437.     public function getPhotos(): Collection
  438.     {
  439.         return $this->photos->filter(function ($mediaFile): bool {
  440.             return get_class($mediaFile) == Photo::class;
  441.         });
  442.     }
  443.     public function addPhoto(string $pathbool $isMain): Photo
  444.     {
  445.         $photos $this->getPhotos();
  446.         $found $photos->filter(function (Photo $photo) use ($path): bool {
  447.             return $path === $photo->getPath();
  448.         });
  449.         if (!$found->isEmpty())
  450.             return $found->first();
  451.         if (true === $isMain) {
  452.             $photos->forAll(function ($indexPhoto $photo): true {
  453.                 $photo->unsetMain();
  454.                 return true;
  455.             });
  456.         }
  457.         $photo = new Photo($this$path$isMain);
  458.         $this->photos->add($photo);
  459.         return $photo;
  460.     }
  461.     public function removePhoto(string $path): bool
  462.     {
  463.         foreach ($this->getPhotos() as $photo) {
  464.             if ($path === $photo->getPath()) {
  465.                 $this->photos->removeElement($photo);
  466.                 return true;
  467.             }
  468.         }
  469.         return false;
  470.     }
  471.     public function getMainPhotoOrFirstPhoto(): ?Photo
  472.     {
  473.         $photos $this->getPhotos();
  474.         if ($photos->isEmpty()) {
  475.             return null;
  476.         }
  477.         $mainPhoto $this->getMainPhoto();
  478.         if (null === $mainPhoto) {
  479.             $mainPhoto $photos->first();
  480.         }
  481.         return $mainPhoto;
  482.     }
  483.     public function getMainPhoto(): ?Photo
  484.     {
  485.         $photos $this->getPhotos();
  486.         if ($photos->isEmpty()) {
  487.             return null;
  488.         }
  489.         $mainPhoto null;
  490.         $photos->forAll(function ($indexPhoto $photo) use (&$mainPhoto): bool {
  491.             if ($photo->isMain()) {
  492.                 $mainPhoto $photo;
  493.                 return false// Stop the cycle
  494.             }
  495.             return true;
  496.         });
  497.         return $mainPhoto;
  498.     }
  499.     public function changeMainPhoto(string $path): void
  500.     {
  501.         $photos $this->getPhotos();
  502.         $found $photos->filter(function (Photo $photo) use ($path): bool {
  503.             return $path === $photo->getPath();
  504.         });
  505.         if ($found->isEmpty()) {
  506.             return;
  507.         }
  508.         $mainPhoto $found->first();
  509.         $photos->forAll(function ($indexPhoto $photo): true {
  510.             $photo->unsetMain();
  511.             return true;
  512.         });
  513.         $mainPhoto->setMain();
  514.     }
  515.     /**
  516.      * @return Selfie[]
  517.      */
  518.     public function getSelfies(): Collection
  519.     {
  520.         return $this->selfies;
  521.     }
  522.     public function addSelfie(string $path): Selfie
  523.     {
  524.         $found $this->getSelfies()->filter(function (Selfie $selfie) use ($path): bool {
  525.             return $path === $selfie->getPath();
  526.         });
  527.         if (!$found->isEmpty())
  528.             return $found->first();
  529.         $selfie = new Selfie($this$path);
  530.         $this->selfies->add($selfie);
  531.         return $selfie;
  532.     }
  533.     public function removeSelfie(string $path): bool
  534.     {
  535.         foreach ($this->getSelfies() as $selfie) {
  536.             if ($path === $selfie->getPath()) {
  537.                 $this->selfies->removeElement($selfie);
  538.                 return true;
  539.             }
  540.         }
  541.         return false;
  542.     }
  543.     /**
  544.      * @return Video[]
  545.      */
  546.     public function getVideos(): Collection
  547.     {
  548.         return $this->videos->filter(function ($mediaFile): bool {
  549.             return ($mediaFile instanceof Video);
  550.         });
  551.     }
  552.     public function getConfirmedVideos(): Collection
  553.     {
  554.         return $this->videos->filter(function ($mediaFile): bool {
  555.             if (!$mediaFile instanceof Video) {
  556.                 return false;
  557.             }
  558.             return $mediaFile->isConfirmed();
  559.         });
  560.     }
  561.     /**
  562.      * Храним только 1 видео для анкеты
  563.      */
  564.     public function addVideo(string $videoPath, ?string $posterPath null): Video
  565.     {
  566.         $found $this->getVideos()->filter(function (Video $video) use ($videoPath): bool {
  567.             return $videoPath === $video->getPath();
  568.         });
  569.         if (!$found->isEmpty())
  570.             return $found->first();
  571.         $video = new Video($this$videoPath);
  572.         if (null !== $posterPath) {
  573.             $video->setPreviewPath($posterPath);
  574.         }
  575.         //теперь разрешаем много видео
  576.         //$this->videos->clear();
  577.         $this->videos->add($video);
  578.         return $video;
  579.     }
  580.     public function removeVideo(string $path): bool
  581.     {
  582.         foreach ($this->getVideos() as $video) {
  583.             if ($path === $video->getPath()) {
  584.                 $this->videos->removeElement($video);
  585.                 $this->photos->removeElement($video);
  586.                 return true;
  587.             }
  588.         }
  589.         return false;
  590.     }
  591.     /**
  592.      * Добавляет таск на обработку оригинала видео в подходящий формат
  593.      *
  594.      * @param string $path Путь к файлу оригинала относительно фс очередей
  595.      */
  596.     public function addRawVideo(string $path): FileProcessingTask
  597.     {
  598.         $file = new FileProcessingTask($this$path);
  599.         $this->processingFiles->add($file);
  600.         return $file;
  601.     }
  602.     public function videosInProcess(): int
  603.     {
  604.         $inProcess $this->processingFiles->filter(function (FileProcessingTask $task): bool {
  605.             return !$task->isCompleted();
  606.         });
  607.         return $inProcess->count();
  608.     }
  609.     public function hasFilesInProcess(): bool
  610.     {
  611.         return $this->videosInProcess() > 0;
  612.     }
  613.     public function isMediaProcessed(): bool
  614.     {
  615.         foreach ($this->videos as $video)
  616.             if(null === $video->getPreviewPath())
  617.                 return false;
  618.         return true;
  619.     }
  620.     public function getAvatar(): ?Avatar
  621.     {
  622.         return $this->avatar;
  623.     }
  624.     public function setAvatar(string $path): void
  625.     {
  626.         $this->avatar = new Avatar($this$path);
  627.     }
  628.     public function removeAvatar(): bool
  629.     {
  630.         if(null == $this->avatar)
  631.             return false;
  632.         foreach ($this->photos as $photo) {
  633.             if ($this->avatar->getPath() === $photo->getPath()) {
  634.                 $this->photos->removeElement($photo);
  635.                 break;
  636.             }
  637.         }
  638.         $this->avatar null;
  639.         return true;
  640.     }
  641.     /**
  642.      * @return CommentByCustomer[]
  643.      */
  644.     public function getComments(): Collection
  645.     {
  646.         return $this->comments->filter(function(CommentByCustomer $comment): bool {
  647.             return null == $comment->getParent();
  648.         });
  649.     }
  650.     /**
  651.      * @return CommentByCustomer[]
  652.      */
  653.     public function getCommentsOrderedByNotReplied(): array
  654.     {
  655.         $comments $this->comments->filter(function(CommentByCustomer $comment): bool {
  656.             return null == $comment->getParent();
  657.         })->toArray();
  658.         usort($comments, function (CommentByCustomer $commentACommentByCustomer $commentB): int {
  659.             if((null == $commentA->getLastCommentByAdvertiser() && null == $commentB->getLastCommentByAdvertiser())
  660.                 || (null != $commentA->getLastCommentByAdvertiser() && null != $commentB->getLastCommentByAdvertiser())) {
  661.                 if($commentA->getCreatedAt() == $commentB->getCreatedAt())
  662.                     return $commentA->getId() > $commentB->getId() ? -1;
  663.                 else
  664.                     return $commentA->getCreatedAt() > $commentB->getCreatedAt() ? -1;
  665.             }
  666.             if(null == $commentA->getLastCommentByAdvertiser() && null != $commentB->getLastCommentByAdvertiser())
  667.                 return -1;
  668.             else
  669.                 return 1;
  670.         });
  671.         return $comments;
  672.     }
  673.     /**
  674.      * @return CommentByCustomer[]
  675.      */
  676.     public function getCommentsWithoutReply(): Collection
  677.     {
  678.         return $this->comments->filter(function(CommentByCustomer $comment): bool {
  679.             return null == $comment->getParent() && false == $comment->isCommentedByAdvertiser();
  680.         });
  681.     }
  682.     /**
  683.      * @return CommentByCustomer[]
  684.      */
  685.     public function getCommentsWithReply(): Collection
  686.     {
  687.         return $this->comments->filter(function(CommentByCustomer $comment): bool {
  688.             return null == $comment->getParent() && true == $comment->isCommentedByAdvertiser();
  689.         });
  690.     }
  691.     /**
  692.      * @return CommentByCustomer[]
  693.      */
  694.     public function getNewComments(): Collection
  695.     {
  696.         $weekAgo CarbonImmutable::now()->sub('7 days');
  697.         return $this->comments->filter(function(CommentByCustomer $comment) use ($weekAgo): bool {
  698.             return null == $comment->getParent()
  699.                 && (
  700.                     $comment->getCreatedAt() >= $weekAgo
  701.                     || null == $this->getCommentReply($comment)
  702.                 );
  703.         });
  704.     }
  705.     public function getOldComments(): Collection
  706.     {
  707.         $weekAgo CarbonImmutable::now()->sub('7 days');
  708.         return $this->comments->filter(function(CommentByCustomer $comment) use ($weekAgo): bool {
  709.             return null == $comment->getParent()
  710.                 && false == (
  711.                     $comment->getCreatedAt() >= $weekAgo
  712.                     || null == $this->getCommentReply($comment)
  713.                 );
  714.         });
  715.     }
  716.     private function getCommentReply(CommentByCustomer $parent): ?CommentByCustomer
  717.     {
  718.         foreach ($this->comments as $comment)
  719.             if($comment->getParent() == $parent)
  720.                 return $comment;
  721.         return null;
  722.     }
  723.     public function getCommentFromUser(Customer $user): ?CommentByCustomer
  724.     {
  725.         foreach ($this->comments as $comment)
  726.             if(null == $comment->getParent() && null != $comment->getUser() && $user->getId() == $comment->getUser()->getId())
  727.                 return $comment;
  728.         return null;
  729.     }
  730.     public function getCity(): City
  731.     {
  732.         return $this->city;
  733.     }
  734.     /**
  735.      * @return Station[]
  736.      */
  737.     public function getStations(): Collection
  738.     {
  739.         return $this->stations;
  740.     }
  741.     //TODO return type
  742.     public function getMapCoordinate(): ?MapCoordinate
  743.     {
  744.         return $this->mapCoordinate;
  745.     }
  746.     public function getCreatedAt(): ?\DateTimeImmutable
  747.     {
  748.         return $this->createdAt;
  749.     }
  750.     public function getUpdatedAt(): ?\DateTimeImmutable
  751.     {
  752.         return $this->updatedAt;
  753.     }
  754.     public function getModerationStatus(): int
  755.     {
  756.         return $this->moderationStatus;
  757.     }
  758.     public function isModerationPassed(): bool
  759.     {
  760.         return $this->moderationStatus == self::MODERATION_STATUS_APPROVED;
  761.     }
  762.     public function isModerationWaiting(): bool
  763.     {
  764.         return $this->moderationStatus == self::MODERATION_STATUS_WAITING;
  765.     }
  766.     public function isModerationRejected(): bool
  767.     {
  768.         return $this->moderationStatus == self::MODERATION_STATUS_REJECTED;
  769.     }
  770.     public function setModerationStatus(int $status): void
  771.     {
  772.         if (self::MODERATION_STATUS_APPROVED === $status) {
  773.             throw new \RuntimeException(sprintf('Use %s::passModeration() method instead', static::class));
  774.         }
  775.         $validStatuses = [self::MODERATION_STATUS_NOT_PASSEDself::MODERATION_STATUS_APPROVEDself::MODERATION_STATUS_WAITINGself::MODERATION_STATUS_REJECTED];
  776.         if(false === array_search($status$validStatuses))
  777.             throw new \LogicException('Trying to set an invalid moderation status');
  778.         $this->moderationStatus $status;
  779.     }
  780.     public function passModeration(?ModerationRequest $moderationRequest null): void
  781.     {
  782.         $this->moderationStatus self::MODERATION_STATUS_APPROVED;
  783.         $func = static function($kPhoto|Video $file) use ($moderationRequest): bool {
  784.             if (!$file->isConfirmed()) {
  785.                 $file->passModeration($moderationRequest);
  786.             }
  787.             return true;
  788.         };
  789.         $this->videos->forAll($func);
  790.     }
  791.     public function delete(): void
  792.     {
  793.         $this->deletePlacementHiding();
  794.         $this->deleteFromAdBoard();
  795.         $now = new \DateTimeImmutable('now');
  796.         $toDelete = [];
  797.         foreach ($this->topPlacements as $topPlacement) {
  798.             if ($topPlacement->getExpiresAt() > $now)
  799.                 $toDelete[] = $topPlacement;
  800.         }
  801.         foreach ($toDelete as $topPlacement)
  802.             $this->topPlacements->removeElement($topPlacement);
  803.         $this->setDeletedAt(Carbon::now());
  804.     }
  805.     public function undoDelete(): void
  806.     {
  807.         $this->setDeletedAt(); // will pass null by default
  808.     }
  809.     public function deleteFromAdBoard(): void
  810.     {
  811.         $this->adBoardPlacement null;
  812.     }
  813.     public function deleteFromTopPlacement(): void
  814.     {
  815.         //здесь нужна логика отмены конретного размещения
  816. //        $this->topPlacement = null;
  817.     }
  818.     public function hasRunningTopPlacement(): bool
  819.     {
  820.         $now = new \DateTimeImmutable('now');
  821.         foreach ($this->topPlacements as /** @var TopPlacement $topPlacement */ $topPlacement) {
  822.             if($topPlacement->getPlacedAt() <= $now && $now <= $topPlacement->getExpiresAt())
  823.                 return true;
  824.         }
  825.         return false;
  826.     }
  827.     //TODO return type
  828.     public function getExpressPricing(): ?ExpressPricing
  829.     {
  830.         return $this->expressPricing;
  831.     }
  832.     //TODO return type
  833.     public function getCarPricing(): ?CarPricing
  834.     {
  835.         return $this->carPricing;
  836.     }
  837.     /**
  838.      * @return int[]
  839.      */
  840.     public function getClientTypes(): array
  841.     {
  842.         return $this->clientTypes ?? [];
  843.     }
  844.     /**
  845.      * @param int[] $clientTypes
  846.      */
  847.     public function setClientTypes(array $clientTypes): void
  848.     {
  849.         $this->clientTypes $clientTypes;
  850.     }
  851.     //TODO return type
  852.     public function getMessengers(): ?Messengers
  853.     {
  854.         return $this->messengers;
  855.     }
  856.     public function getInactivatedAt(): ?\DateTimeImmutable
  857.     {
  858.         return $this->inactivatedAt;
  859.     }
  860.     public function setInactive(): void
  861.     {
  862.         $this->inactivatedAt CarbonImmutable::now();
  863.     }
  864.     public function undoInactive(): void
  865.     {
  866.         $this->inactivatedAt null;
  867.     }
  868.     public function getPlacementHiding(): ?PlacementHiding
  869.     {
  870.         return $this->placementHiding;
  871.     }
  872.     public function setPlacementHiding(PlacementHiding $placementHiding): void
  873.     {
  874.         $this->placementHiding $placementHiding;
  875.     }
  876.     public function isHidden(): bool
  877.     {
  878.         return null !== $this->getPlacementHiding();
  879.     }
  880.     public function deletePlacementHiding(): void
  881.     {
  882.         $this->placementHiding null;
  883.     }
  884.     public function hasSelfie(): bool
  885.     {
  886.         return $this->selfies->count() > 0;
  887.     }
  888.     public function hasVideo(): bool
  889.     {
  890.         return $this->videos->count() > 0;
  891.     }
  892.     public function isCommented(): bool
  893.     {
  894.         return $this->comments->count() > 0;
  895.     }
  896.     public function adminApprovalPhoto(): ?AdminApprovalPhoto
  897.     {
  898.         return $this->adminApprovalPhoto;
  899.     }
  900.     public function setAdminApprovalPhoto(?string $path): void
  901.     {
  902.         $this->adminApprovalPhoto $path ? new AdminApprovalPhoto($this$path) : null;
  903.     }
  904.     public function seo(): ?array
  905.     {
  906.         return $this->seo;
  907.     }
  908.     public function seoPhoneNumber(): ?string
  909.     {
  910.         return $this->seo['phone'] ?? null;
  911.     }
  912.     public function setSeoPhoneNumber(string $phoneNumber): void
  913.     {
  914.         if(null === $this->seo) {
  915.             $this->seo = [];
  916.         }
  917.         $this->seo['phone'] = $phoneNumber;
  918.     }
  919. }