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