src/Entity/Profile/Profile.php line 75

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