src/Entity/Profile/Profile.php line 74

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.     #[ORM\ManyToOne(targetEntityStation::class)]
  196.     #[ORM\JoinColumn(name'primary_station_id'referencedColumnName'id'nullabletrueonDelete'SET NULL')]
  197.     #[Groups('profile')]
  198.     private ?Station $primaryStation null;
  199.     protected function __construct(?\DateTimeImmutable $createdAt)
  200.     {
  201.         $this->draft true;
  202.         $this->createdAt $createdAt;
  203.         $this->photos = new ArrayCollection();
  204.         $this->selfies = new ArrayCollection();
  205.         $this->videos = new ArrayCollection();
  206.         $this->processingFiles = new ArrayCollection();
  207.         $this->comments = new ArrayCollection();
  208.         $this->topPlacements = new ArrayCollection();
  209.         $this->providedServices = new ArrayCollection();
  210.         $this->stations = new ArrayCollection();
  211.         $this->inactivatedAt CarbonImmutable::now();
  212.     }
  213.     public static function draft(?\DateTimeImmutable $createdAt null, ?bool $dummy null): self
  214.     {
  215.         $profile = new static($createdAt);
  216.         if (null !== $dummy)
  217.             $profile->dummy $dummy;
  218.         return $profile;
  219.     }
  220.     public static function create(string $uriIdentity, ?\DateTimeImmutable $createdAt null): self
  221.     {
  222.         $profile = new static($createdAt);
  223.         $profile->defineUriIdentity($uriIdentity);
  224.         $profile->toggleMasseur(false);
  225.         return $profile;
  226.     }
  227.     public function defineUriIdentity(string $uriIdentity): void
  228.     {
  229.         if (!$this->isDraft()) {
  230.             throw new \DomainException('Profile is already created and can\'t change its URI.');
  231.         }
  232.         $this->uriIdentity $uriIdentity;
  233.         $this->draft false;
  234.     }
  235.     public function isDraft(): bool
  236.     {
  237.         return $this->draft;
  238.     }
  239.     public function toggleMasseur(bool $isMasseur): void
  240.     {
  241.         if ($this->masseur !== $isMasseur && (null !== $this->adBoardPlacement && false == $this->adBoardPlacement->getType()->isFree())) {
  242.             throw new \DomainException('Impossible to toggle profile type while it is displaying on adboard.');
  243.         }
  244.         $this->masseur $isMasseur;
  245.     }
  246.     public static function createMasseur(string $uriIdentity, ?\DateTimeImmutable $createdAt null): self
  247.     {
  248.         $profile = new static($createdAt);
  249.         $profile->defineUriIdentity($uriIdentity);
  250.         $profile->toggleMasseur(true);
  251.         return $profile;
  252.     }
  253.     public function isOwnedBy(Advertiser $account): bool
  254.     {
  255.         return $account->getId() === $this->owner->getId();
  256.     }
  257.     public function getId(): int
  258.     {
  259.         return $this->id;
  260.     }
  261.     public function setBio(TranslatableValue $nameTranslatableValue $description): void
  262.     {
  263.         $this->name $name;
  264.         $this->description $description;
  265.     }
  266.     public function setLocation(City $city$stations, ?MapCoordinate $mapCoordinate): void
  267.     {
  268.         if (!$this->isDraft() && !$this->city->equals($city)) {
  269.             throw new \DomainException('City change for a saved profile is forbidden.');
  270.         }
  271.         $this->city $city;
  272.         $this->changeStations($stations);
  273.         $this->normalizePrimaryStation();
  274.         $this->mapCoordinate $mapCoordinate;
  275.     }
  276.     protected function changeStations($stations)
  277.     {
  278.         if (null === $stations)
  279.             return;
  280.         if (false === is_array($stations) && false === is_iterable($stations))
  281.             throw new \InvalidArgumentException('Stations list should be either an array or an ArrayCollection');
  282.         $stationsArray is_iterable($stations) && !is_array($stations) ? iterator_to_array($stations) : $stations;
  283.         $stations = [];
  284.         foreach ($stationsArray as $station) {
  285.             $stations[$station->getId()] = $station;
  286.         }
  287.         $stationIds array_map(function (Station $station): int {
  288.             return $station->getId();
  289.         }, $stations);
  290.         $existingStationIds $this->stations->map(function (Station $station): int {
  291.             return $station->getId();
  292.         })->getValues();
  293.         $stationIdsToAdd array_diff($stationIds$existingStationIds);
  294.         $stationIdsToRemove array_diff($existingStationIds$stationIds);
  295.         foreach ($stationIdsToAdd as $stationId) {
  296.             $this->stations->add($stations[$stationId]);
  297.         }
  298.         foreach ($stationIdsToRemove as $stationId) {
  299.             $this->stations->remove($stationId);
  300.         }
  301.     }
  302.     public function normalizePrimaryStation(): void
  303.     {
  304.         if ($this->stations->isEmpty()) {
  305.             $this->primaryStation null;
  306.             return;
  307.         }
  308.         if ($this->primaryStation === null || !$this->stations->contains($this->primaryStation)) {
  309.             $this->primaryStation $this->stations->first();
  310.         }
  311.     }
  312.     public function setEnabledProvidedServices($services): void
  313.     {
  314.         if (null !== $services) {
  315.             if (is_array($services)) {
  316.                 $services = new ArrayCollection($services);
  317.             } elseif (!$services instanceof ArrayCollection) {
  318.                 if (is_iterable($services)) {
  319.                     $services = new ArrayCollection(iterator_to_array($services));
  320.                 } else {
  321.                     throw new \InvalidArgumentException('Services list should be either an array or an ArrayCollection');
  322.                 }
  323.             }
  324.             $this->providedServices $services;
  325.         }
  326.     }
  327.     public function setPhoneCallOptions(string $phoneNumber, ?PhoneCallRestrictions $restrictions, ?Messengers $messengers): void
  328.     {
  329.         $this->phoneNumber $phoneNumber;
  330.         $this->phoneCallRestrictions $restrictions;
  331.         $this->messengers $messengers;
  332.     }
  333.     public function setPricing(?ApartmentsPricing $apartmentsPricing, ?TakeOutPricing $takeOutPricing, ?int $extraCharge, ?ExpressPricing $expressPricing null, ?CarPricing $carPricing null): void
  334.     {
  335.         $this->apartmentsPricing $apartmentsPricing;
  336.         $this->takeOutPricing $takeOutPricing;
  337.         $this->extraCharge $extraCharge;
  338.         $this->expressPricing $expressPricing;
  339.         $this->carPricing $carPricing;
  340.     }
  341.     public function isApproved(): bool
  342.     {
  343.         return $this->approved;
  344.     }
  345.     public function approve(): void
  346.     {
  347.         $this->approved true;
  348.     }
  349.     public function unApprove(): void
  350.     {
  351.         $this->approved false;
  352.     }
  353.     public function getOwner(): ?Advertiser
  354.     {
  355.         return $this->owner;
  356.     }
  357.     public function setOwner(Advertiser $owner): void
  358.     {
  359.         $this->owner $owner;
  360.     }
  361.     public function hasOwner(): bool
  362.     {
  363.         return null !== $this->owner;
  364.     }
  365.     public function getTopPlacements(): Collection
  366.     {
  367.         return $this->topPlacements;
  368.     }
  369.     public function addTopPlacement(TopPlacement $topPlacement): void
  370.     {
  371.         $this->topPlacements->add($topPlacement);
  372.     }
  373.     public function getAdBoardPlacement(): ?AdBoardPlacement
  374.     {
  375.         return $this->adBoardPlacement;
  376.     }
  377.     public function setAdBoardPlacement(AdBoardPlacement $adBoardPlacement): void
  378.     {
  379.         $this->adBoardPlacement $adBoardPlacement;
  380.     }
  381.     /**
  382.      * Анкета оплачена и выводится в общих списках на сайте
  383.      * или в ТОПе, то есть "АКТИВНА"
  384.      */
  385.     public function isActive(): bool
  386.     {
  387.         return null !== $this->adBoardPlacement || $this->hasRunningTopPlacement();
  388.     }
  389.     public function hasRunningTopPlacement(): bool
  390.     {
  391.         $now = new \DateTimeImmutable('now');
  392.         foreach ($this->topPlacements as /** @var TopPlacement $topPlacement */ $topPlacement) {
  393.             if ($topPlacement->getPlacedAt() <= $now && $now <= $topPlacement->getExpiresAt())
  394.                 return true;
  395.         }
  396.         return false;
  397.     }
  398.     public function getUriIdentity(): string
  399.     {
  400.         return $this->uriIdentity;
  401.     }
  402.     public function getName(): TranslatableValue
  403.     {
  404.         return $this->name;
  405.     }
  406.     public function getDescription(): ?TranslatableValue
  407.     {
  408.         return $this->description;
  409.     }
  410.     public function getPersonParameters(): PersonParameters
  411.     {
  412.         return $this->personParameters;
  413.     }
  414.     public function setPersonParameters(PersonParameters $personParameters): void
  415.     {
  416.         $this->personParameters $personParameters;
  417.     }
  418.     public function getPhoneNumber(): string
  419.     {
  420.         return $this->phoneNumber;
  421.     }
  422.     //TODO return type
  423.     public function getPhoneCallRestrictions(): ?PhoneCallRestrictions
  424.     {
  425.         return $this->phoneCallRestrictions;
  426.     }
  427.     public function isMasseur(): bool
  428.     {
  429.         return $this->masseur;
  430.     }
  431.     //TODO return type
  432.     public function getClientRestrictions(): ?ClientRestrictions
  433.     {
  434.         return $this->clientRestrictions;
  435.     }
  436.     //TODO return type
  437.     public function setClientRestrictions(?ClientRestrictions $restrictions): void
  438.     {
  439.         $this->clientRestrictions $restrictions;
  440.     }
  441.     //TODO return type
  442.     public function getApartmentsPricing(): ?ApartmentsPricing
  443.     {
  444.         return $this->apartmentsPricing;
  445.     }
  446.     public function getTakeOutPricing(): ?TakeOutPricing
  447.     {
  448.         return $this->takeOutPricing;
  449.     }
  450.     public function getExtraCharge(): ?int
  451.     {
  452.         return $this->extraCharge;
  453.     }
  454.     public function addPhoto(string $pathbool $isMain): Photo
  455.     {
  456.         $photos $this->getPhotos();
  457.         $found $photos->filter(function (Photo $photo) use ($path): bool {
  458.             return $path === $photo->getPath();
  459.         });
  460.         if (!$found->isEmpty())
  461.             return $found->first();
  462.         if (true === $isMain) {
  463.             $photos->forAll(function ($indexPhoto $photo): true {
  464.                 $photo->unsetMain();
  465.                 return true;
  466.             });
  467.         }
  468.         $photo = new Photo($this$path$isMain);
  469.         $this->photos->add($photo);
  470.         return $photo;
  471.     }
  472.     /**
  473.      * @return Photo[]
  474.      */
  475.     public function getPhotos(): Collection
  476.     {
  477.         return $this->photos->filter(function ($mediaFile): bool {
  478.             return get_class($mediaFile) == Photo::class;
  479.         });
  480.     }
  481.     public function removePhoto(string $path): bool
  482.     {
  483.         foreach ($this->getPhotos() as $photo) {
  484.             if ($path === $photo->getPath()) {
  485.                 $this->photos->removeElement($photo);
  486.                 return true;
  487.             }
  488.         }
  489.         return false;
  490.     }
  491.     public function getMainPhotoOrFirstPhoto(): ?Photo
  492.     {
  493.         $photos $this->getPhotos();
  494.         if ($photos->isEmpty()) {
  495.             return null;
  496.         }
  497.         $mainPhoto $this->getMainPhoto();
  498.         if (null === $mainPhoto) {
  499.             $mainPhoto $photos->first();
  500.         }
  501.         return $mainPhoto;
  502.     }
  503.     public function getMainPhoto(): ?Photo
  504.     {
  505.         $photos $this->getPhotos();
  506.         if ($photos->isEmpty()) {
  507.             return null;
  508.         }
  509.         $mainPhoto null;
  510.         $photos->forAll(function ($indexPhoto $photo) use (&$mainPhoto): bool {
  511.             if ($photo->isMain()) {
  512.                 $mainPhoto $photo;
  513.                 return false// Stop the cycle
  514.             }
  515.             return true;
  516.         });
  517.         return $mainPhoto;
  518.     }
  519.     public function changeMainPhoto(string $path): void
  520.     {
  521.         $photos $this->getPhotos();
  522.         $found $photos->filter(function (Photo $photo) use ($path): bool {
  523.             return $path === $photo->getPath();
  524.         });
  525.         if ($found->isEmpty()) {
  526.             return;
  527.         }
  528.         $mainPhoto $found->first();
  529.         $photos->forAll(function ($indexPhoto $photo): true {
  530.             $photo->unsetMain();
  531.             return true;
  532.         });
  533.         $mainPhoto->setMain();
  534.     }
  535.     public function addSelfie(string $path): Selfie
  536.     {
  537.         $found $this->getSelfies()->filter(function (Selfie $selfie) use ($path): bool {
  538.             return $path === $selfie->getPath();
  539.         });
  540.         if (!$found->isEmpty())
  541.             return $found->first();
  542.         $selfie = new Selfie($this$path);
  543.         $this->selfies->add($selfie);
  544.         return $selfie;
  545.     }
  546.     /**
  547.      * @return Selfie[]
  548.      */
  549.     public function getSelfies(): Collection
  550.     {
  551.         return $this->selfies;
  552.     }
  553.     public function removeSelfie(string $path): bool
  554.     {
  555.         foreach ($this->getSelfies() as $selfie) {
  556.             if ($path === $selfie->getPath()) {
  557.                 $this->selfies->removeElement($selfie);
  558.                 return true;
  559.             }
  560.         }
  561.         return false;
  562.     }
  563.     public function getConfirmedVideos(): Collection
  564.     {
  565.         return $this->videos->filter(function ($mediaFile): bool {
  566.             if (!$mediaFile instanceof Video) {
  567.                 return false;
  568.             }
  569.             return $mediaFile->isConfirmed();
  570.         });
  571.     }
  572.     /**
  573.      * Храним только 1 видео для анкеты
  574.      */
  575.     public function addVideo(string $videoPath, ?string $posterPath null): Video
  576.     {
  577.         $found $this->getVideos()->filter(function (Video $video) use ($videoPath): bool {
  578.             return $videoPath === $video->getPath();
  579.         });
  580.         if (!$found->isEmpty())
  581.             return $found->first();
  582.         $video = new Video($this$videoPath);
  583.         if (null !== $posterPath) {
  584.             $video->setPreviewPath($posterPath);
  585.         }
  586.         //теперь разрешаем много видео
  587.         //$this->videos->clear();
  588.         $this->videos->add($video);
  589.         return $video;
  590.     }
  591.     /**
  592.      * @return Video[]
  593.      */
  594.     public function getVideos(): Collection
  595.     {
  596.         return $this->videos->filter(function ($mediaFile): bool {
  597.             return ($mediaFile instanceof Video);
  598.         });
  599.     }
  600.     public function removeVideo(string $path): bool
  601.     {
  602.         foreach ($this->getVideos() as $video) {
  603.             if ($path === $video->getPath()) {
  604.                 $this->videos->removeElement($video);
  605.                 $this->photos->removeElement($video);
  606.                 return true;
  607.             }
  608.         }
  609.         return false;
  610.     }
  611.     /**
  612.      * Добавляет таск на обработку оригинала видео в подходящий формат
  613.      *
  614.      * @param string $path Путь к файлу оригинала относительно фс очередей
  615.      */
  616.     public function addRawVideo(string $path): FileProcessingTask
  617.     {
  618.         $file = new FileProcessingTask($this$path);
  619.         $this->processingFiles->add($file);
  620.         return $file;
  621.     }
  622.     public function hasFilesInProcess(): bool
  623.     {
  624.         return $this->videosInProcess() > 0;
  625.     }
  626.     public function videosInProcess(): int
  627.     {
  628.         $inProcess $this->processingFiles->filter(function (FileProcessingTask $task): bool {
  629.             return !$task->isCompleted();
  630.         });
  631.         return $inProcess->count();
  632.     }
  633.     public function isMediaProcessed(): bool
  634.     {
  635.         foreach ($this->videos as $video)
  636.             if (null === $video->getPreviewPath())
  637.                 return false;
  638.         return true;
  639.     }
  640.     public function getAvatar(): ?Avatar
  641.     {
  642.         return $this->avatar;
  643.     }
  644.     public function setAvatar(string $path): void
  645.     {
  646.         $this->avatar = new Avatar($this$path);
  647.     }
  648.     public function removeAvatar(): bool
  649.     {
  650.         if (null == $this->avatar)
  651.             return false;
  652.         foreach ($this->photos as $photo) {
  653.             if ($this->avatar->getPath() === $photo->getPath()) {
  654.                 $this->photos->removeElement($photo);
  655.                 break;
  656.             }
  657.         }
  658.         $this->avatar null;
  659.         return true;
  660.     }
  661.     /**
  662.      * @return CommentByCustomer[]
  663.      */
  664.     public function getComments(): Collection
  665.     {
  666.         return $this->comments->filter(function (CommentByCustomer $comment): bool {
  667.             return null == $comment->getParent();
  668.         });
  669.     }
  670.     /**
  671.      * @return CommentByCustomer[]
  672.      */
  673.     public function getCommentsOrderedByNotReplied(): array
  674.     {
  675.         $comments $this->comments->filter(function (CommentByCustomer $comment): bool {
  676.             return null == $comment->getParent();
  677.         })->toArray();
  678.         usort($comments, function (CommentByCustomer $commentACommentByCustomer $commentB): int {
  679.             if ((null == $commentA->getLastCommentByAdvertiser() && null == $commentB->getLastCommentByAdvertiser())
  680.                 || (null != $commentA->getLastCommentByAdvertiser() && null != $commentB->getLastCommentByAdvertiser())) {
  681.                 if ($commentA->getCreatedAt() == $commentB->getCreatedAt())
  682.                     return $commentA->getId() > $commentB->getId() ? -1;
  683.                 else
  684.                     return $commentA->getCreatedAt() > $commentB->getCreatedAt() ? -1;
  685.             }
  686.             if (null == $commentA->getLastCommentByAdvertiser() && null != $commentB->getLastCommentByAdvertiser())
  687.                 return -1;
  688.             else
  689.                 return 1;
  690.         });
  691.         return $comments;
  692.     }
  693.     public function getCreatedAt(): ?\DateTimeImmutable
  694.     {
  695.         return $this->createdAt;
  696.     }
  697.     /**
  698.      * @return CommentByCustomer[]
  699.      */
  700.     public function getCommentsWithoutReply(): Collection
  701.     {
  702.         return $this->comments->filter(function (CommentByCustomer $comment): bool {
  703.             return null == $comment->getParent() && false == $comment->isCommentedByAdvertiser();
  704.         });
  705.     }
  706.     /**
  707.      * @return CommentByCustomer[]
  708.      */
  709.     public function getCommentsWithReply(): Collection
  710.     {
  711.         return $this->comments->filter(function (CommentByCustomer $comment): bool {
  712.             return null == $comment->getParent() && true == $comment->isCommentedByAdvertiser();
  713.         });
  714.     }
  715.     /**
  716.      * @return CommentByCustomer[]
  717.      */
  718.     public function getNewComments(): Collection
  719.     {
  720.         $weekAgo CarbonImmutable::now()->sub('7 days');
  721.         return $this->comments->filter(function (CommentByCustomer $comment) use ($weekAgo): bool {
  722.             return null == $comment->getParent()
  723.                 && (
  724.                     $comment->getCreatedAt() >= $weekAgo
  725.                     || null == $this->getCommentReply($comment)
  726.                 );
  727.         });
  728.     }
  729.     private function getCommentReply(CommentByCustomer $parent): ?CommentByCustomer
  730.     {
  731.         foreach ($this->comments as $comment)
  732.             if ($comment->getParent() == $parent)
  733.                 return $comment;
  734.         return null;
  735.     }
  736.     public function getOldComments(): Collection
  737.     {
  738.         $weekAgo CarbonImmutable::now()->sub('7 days');
  739.         return $this->comments->filter(function (CommentByCustomer $comment) use ($weekAgo): bool {
  740.             return null == $comment->getParent()
  741.                 && false == (
  742.                     $comment->getCreatedAt() >= $weekAgo
  743.                     || null == $this->getCommentReply($comment)
  744.                 );
  745.         });
  746.     }
  747.     public function getCommentFromUser(Customer $user): ?CommentByCustomer
  748.     {
  749.         foreach ($this->comments as $comment)
  750.             if (null == $comment->getParent() && null != $comment->getUser() && $user->getId() == $comment->getUser()->getId())
  751.                 return $comment;
  752.         return null;
  753.     }
  754.     //TODO return type
  755.     public function getCity(): City
  756.     {
  757.         return $this->city;
  758.     }
  759.     /**
  760.      * @return Station[]
  761.      */
  762.     public function getStations(): Collection
  763.     {
  764.         return $this->stations;
  765.     }
  766.     public function getMapCoordinate(): ?MapCoordinate
  767.     {
  768.         return $this->mapCoordinate;
  769.     }
  770.     public function getUpdatedAt(): ?\DateTimeImmutable
  771.     {
  772.         return $this->updatedAt;
  773.     }
  774.     public function setUpdatedAt(\DateTimeImmutable $updatedAt): void
  775.     {
  776.         $this->updatedAt $updatedAt;
  777.     }
  778.     public function getModerationStatus(): int
  779.     {
  780.         return $this->moderationStatus;
  781.     }
  782.     public function setModerationStatus(int $status): void
  783.     {
  784.         if (self::MODERATION_STATUS_APPROVED === $status) {
  785.             throw new \RuntimeException(sprintf('Use %s::passModeration() method instead', static::class));
  786.         }
  787.         $validStatuses = [self::MODERATION_STATUS_NOT_PASSEDself::MODERATION_STATUS_APPROVEDself::MODERATION_STATUS_WAITINGself::MODERATION_STATUS_REJECTED];
  788.         if (false === array_search($status$validStatuses))
  789.             throw new \LogicException('Trying to set an invalid moderation status');
  790.         $this->moderationStatus $status;
  791.     }
  792.     public function isModerationPassed(): bool
  793.     {
  794.         return $this->moderationStatus == self::MODERATION_STATUS_APPROVED;
  795.     }
  796.     public function isModerationWaiting(): bool
  797.     {
  798.         return $this->moderationStatus == self::MODERATION_STATUS_WAITING;
  799.     }
  800.     public function isModerationRejected(): bool
  801.     {
  802.         return $this->moderationStatus == self::MODERATION_STATUS_REJECTED;
  803.     }
  804.     public function passModeration(?ModerationRequest $moderationRequest null): void
  805.     {
  806.         $this->moderationStatus self::MODERATION_STATUS_APPROVED;
  807.         $func = static function ($kPhoto|Video $file) use ($moderationRequest): bool {
  808.             if (!$file->isConfirmed()) {
  809.                 $file->passModeration($moderationRequest);
  810.             }
  811.             return true;
  812.         };
  813.         $this->videos->forAll($func);
  814.     }
  815.     public function delete(): void
  816.     {
  817.         $this->deletePlacementHiding();
  818.         $this->deleteFromAdBoard();
  819.         $now = new \DateTimeImmutable('now');
  820.         $toDelete = [];
  821.         foreach ($this->topPlacements as $topPlacement) {
  822.             if ($topPlacement->getExpiresAt() > $now)
  823.                 $toDelete[] = $topPlacement;
  824.         }
  825.         foreach ($toDelete as $topPlacement)
  826.             $this->topPlacements->removeElement($topPlacement);
  827.         $this->setDeletedAt(Carbon::now());
  828.     }
  829.     public function deletePlacementHiding(): void
  830.     {
  831.         $this->placementHiding null;
  832.     }
  833.     public function deleteFromAdBoard(): void
  834.     {
  835.         $this->adBoardPlacement null;
  836.     }
  837.     //TODO return type
  838.     public function undoDelete(): void
  839.     {
  840.         $this->setDeletedAt(); // will pass null by default
  841.     }
  842.     //TODO return type
  843.     public function deleteFromTopPlacement(): void
  844.     {
  845.         //здесь нужна логика отмены конретного размещения
  846. //        $this->topPlacement = null;
  847.     }
  848.     public function getExpressPricing(): ?ExpressPricing
  849.     {
  850.         return $this->expressPricing;
  851.     }
  852.     public function getCarPricing(): ?CarPricing
  853.     {
  854.         return $this->carPricing;
  855.     }
  856.     //TODO return type
  857.     /**
  858.      * @return int[]
  859.      */
  860.     public function getClientTypes(): array
  861.     {
  862.         return $this->clientTypes ?? [];
  863.     }
  864.     /**
  865.      * @param int[] $clientTypes
  866.      */
  867.     public function setClientTypes(array $clientTypes): void
  868.     {
  869.         $this->clientTypes $clientTypes;
  870.     }
  871.     public function getMessengers(): ?Messengers
  872.     {
  873.         return $this->messengers;
  874.     }
  875.     public function getInactivatedAt(): ?\DateTimeImmutable
  876.     {
  877.         return $this->inactivatedAt;
  878.     }
  879.     public function setInactive(): void
  880.     {
  881.         $this->inactivatedAt CarbonImmutable::now();
  882.     }
  883.     public function undoInactive(): void
  884.     {
  885.         $this->inactivatedAt null;
  886.     }
  887.     public function isHidden(): bool
  888.     {
  889.         return null !== $this->getPlacementHiding();
  890.     }
  891.     public function getPlacementHiding(): ?PlacementHiding
  892.     {
  893.         return $this->placementHiding;
  894.     }
  895.     public function setPlacementHiding(PlacementHiding $placementHiding): void
  896.     {
  897.         $this->placementHiding $placementHiding;
  898.     }
  899.     public function hasSelfie(): bool
  900.     {
  901.         return $this->selfies->count() > 0;
  902.     }
  903.     public function hasVideo(): bool
  904.     {
  905.         return $this->videos->count() > 0;
  906.     }
  907.     public function isCommented(): bool
  908.     {
  909.         return $this->comments->count() > 0;
  910.     }
  911.     public function adminApprovalPhoto(): ?AdminApprovalPhoto
  912.     {
  913.         return $this->adminApprovalPhoto;
  914.     }
  915.     public function setAdminApprovalPhoto(?string $path): void
  916.     {
  917.         $this->adminApprovalPhoto $path ? new AdminApprovalPhoto($this$path) : null;
  918.     }
  919.     public function seo(): ?array
  920.     {
  921.         return $this->seo;
  922.     }
  923.     public function seoPhoneNumber(): ?string
  924.     {
  925.         return $this->seo['phone'] ?? null;
  926.     }
  927.     public function setSeoPhoneNumber(string $phoneNumber): void
  928.     {
  929.         if (null === $this->seo) {
  930.             $this->seo = [];
  931.         }
  932.         $this->seo['phone'] = $phoneNumber;
  933.     }
  934.     public function getPrimaryStation(): ?Station
  935.     {
  936.         return $this->primaryStation;
  937.     }
  938.     public function setPrimaryStation(?Station $station): void
  939.     {
  940.         $this->primaryStation $station;
  941.         $this->normalizePrimaryStation();
  942.     }
  943. }