src/Entity/Profile/Profile.php line 77

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