<?php
namespace App\Console\TestData;
use AngelGamez\TranslatableBundle\Entity\TranslatableValue;
use App\Entity\Account\Advertiser;
use App\Entity\Location\City;
use App\Entity\Profile\Genders;
use App\Entity\Profile\PersonParameters;
use App\Entity\Profile\Profile;
use App\Entity\User;
use App\Event\Profile\ProfileWasCreated;
use App\Repository\CityRepository;
use App\Repository\UserRepository;
use App\Service\CountryCurrencyResolver;
use App\Service\DefaultCityProvider;
use App\Service\UploadedFileNamer;
use Doctrine\ORM\EntityManagerInterface;
use League\Flysystem\FilesystemOperator;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
class CreateDummyAccounts extends Command
{
private SymfonyStyle $io;
public function __construct(
private EntityManagerInterface $entityManager,
private UploadedFileNamer $uploadedFileNamer,
private FilesystemOperator $profileMediaFilesystem,
private DefaultCityProvider $defaultCityProvider,
private EncoderFactoryInterface $passwordEncoderFactory,
private CountryCurrencyResolver $countryCurrencyResolver,
private CityRepository $cityRepository,
private UserRepository $userRepository,
private ParameterBagInterface $parameterBag,
private KernelInterface $kernel,
private EventDispatcherInterface $eventDispatcher,
)
{
parent::__construct();
}
/**
* @inheritDoc
*/
protected function configure()
{
$this->setName('test-data:dummy-profiles:create');
$this->addArgument('default_user', InputArgument::OPTIONAL, 'To default user');
}
/**
* @inheritDoc
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$this->io = $io;
$io->progressStart();
$io->title(sprintf("Adding dummy profiles"));
$toDefaultUser = $input->getArgument('default_user');
if(null !== $toDefaultUser && ($this->kernel->getEnvironment() == 'dev' || $this->kernel->getEnvironment() == 'review')) {
$advertiser = $this->userRepository->findOneBy(['email' => 'account1@example.com']);
if(!$advertiser)
throw new \Exception('Default account not found');
} else {
$advertiser = $this->createAdvertiser();
}
$citiesWithCount = $this->parameterBag->get('test_data.dummy_profiles.cities_and_count');
$citiesWithCount = explode(',', $citiesWithCount);
foreach ($citiesWithCount as $cityCount) {
$cityCount = explode(':', $cityCount);
$uriIdentity = $cityCount[0];
$count = (int)$cityCount[1];
$io->section('Adding to ' . $uriIdentity);
$city = $this->cityRepository->ofUriIdentity($uriIdentity);
if (null === $city && in_array($uriIdentity, ['moscow', 'moskva'], true)) {
$city = $this->defaultCityProvider->getDefaultCity();
$io->warning(sprintf('City "%s" not found. Using default city "%s".', $uriIdentity, $city->getUriIdentity()));
}
if (null === $city) {
$io->warning(sprintf('City "%s" not found. Skipped.', $uriIdentity));
continue;
}
$this->generateDummyProfiles($advertiser, $city, $count);
}
return Command::SUCCESS;
}
protected function createAdvertiser(): Advertiser
{
$advertiser = $this->userRepository->findOneBy(['email' => 'kirilldavydenko@protonmail.com']);
if($advertiser)
return $advertiser;
$defaultCity = $this->defaultCityProvider->getDefaultCity();
$passwordEncoder = $this->passwordEncoderFactory->getEncoder(User::class);
$encryptedPassword = $passwordEncoder->encodePassword('YXeK5bkuy2WAsUqVEr48', null);
$advertiser = new Advertiser();
$advertiser->setEmail('kirilldavydenko@protonmail.com');
$advertiser->setNickName('kirilldavydenko');
$advertiser->setPassword($encryptedPassword);
$advertiser->setCity($defaultCity);
$advertiser->resolveCurrency($this->countryCurrencyResolver);
$advertiser->setEnabled(true);
$this->entityManager->persist($advertiser);
return $advertiser;
}
protected function generateDummyProfiles(Advertiser $owner, City $city, int $count): void
{
$publicFolder = $this->parameterBag->get('app.path.web');
$this->io->progressStart();
for ($i = 1; $i <= $count; $i++) {
$uriIdentity = sprintf('test-anketa-%s-%s', $city->getUriIdentity(), $i);
$profile = Profile::draft(null, true);
$profile->toggleMasseur(false);
$profile->setLocation($city, [], null);
$profile->defineUriIdentity($uriIdentity);
$profile->setOwner($owner);
$profile->setPhoneCallOptions('+79123456789', null, null);
$profile->setPersonParameters(new PersonParameters(Genders::FEMALE, 55, null, null, null, null, null, null, null, null, null, null, null, null, null));
$profile->setBio(
new TranslatableValue(['ru' => sprintf('Тест анкета %s № %s', $city->getName()->getTranslation('ru'), $i)]),
new TranslatableValue(['ru' => 'Тестовая анкета. Не звонить. Тестовая анкета. Не звонить. Тестовая анкета. Не звонить. Тестовая анкета. Не звонить. Тестовая анкета. Не звонить.'])
);
$profile->passModeration();
$avatarPath = $this->uploadedFileNamer->generateName();
$avatarPath = $this->uploadedFileNamer->calcNestedPath($avatarPath) . '/' . $avatarPath . ".png";
$this->profileMediaFilesystem->write($avatarPath, file_get_contents($publicFolder . 'static/img/avatar-test.png'));
$profile->setAvatar($avatarPath);
//$adBoardPlacement = AdBoardPlacement::free($profile, 1, null, CarbonImmutable::now(), new \DateTimeImmutable('2100-01-01 00:00:00'));
//$profile->setAdBoardPlacement($adBoardPlacement);
$this->entityManager->persist($profile);
$this->entityManager->flush();
$this->eventDispatcher->dispatch(new ProfileWasCreated($profile), ProfileWasCreated::NAME);
$this->io->progressAdvance();
}
$this->io->progressFinish();
}
}