<?php declare(strict_types=1);
namespace Compra\FoundationSW6;
use Compra\EsiSW6\Core\System\Service\ApiService;
use Shopware\Core\Framework\Api\Context\SystemSource;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UpdateContext;
use Shopware\Core\System\User\UserEntity;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class CompraFoundationSW6 extends Plugin
{
public const API_USER_LOCALE = 'de-DE';
public const API_USER = 'CompraApiAdmin';
public const API_USER_PASSWORD = 'elrond.1';
public const API_USER_SCOPE = 'write';
public const API_USER_POSSIBLE_EMAILS = [
'webteam@compra.de',
'webdev@compra.de',
'websupport@compra.de'
];
public const API_USER_FALLBACK_EMAIL = 'webteam%random_int%@compra.de';
public function build(ContainerBuilder $container): void
{
$container->setParameter('compra_foundation_s_w6.plugin_dir', $this->getPath());
parent::build($container);
}
public function install(InstallContext $installContext): void
{
parent::install($installContext);
$this->createAdministrationUser($installContext->getContext());
}
public function update(UpdateContext $updateContext): void
{
parent::update($updateContext);
$this->createAdministrationUser($updateContext->getContext());
}
/**
* Helper function to get the ID of the ESI user.
* Return null if no ESI user exists.
*
* @param Context $context
*/
public function createAdministrationUser(Context $context): void
{
/** @var EntityRepositoryInterface $userRepository */
$userRepository = $this->container->get('user.repository');
if (!$userRepository) {
throw new \RuntimeException('Unable to load service user.repository');
}
$usersResult = $userRepository->search(new Criteria(), $context);
$email = null;
// check which email address we can use, because there could already be a user with one of our possible addresses
foreach (self::API_USER_POSSIBLE_EMAILS as $possibleEmail) {
$result = $usersResult->filterByProperty('email', $possibleEmail)->first();
if (!$result) {
// no user with this email address, so we can use it
$email = $possibleEmail;
break;
}
}
if (!$email) {
$email = str_replace('%random_int%', (string) random_int(10000, 99999), self::API_USER_FALLBACK_EMAIL);
}
/** @var UserEntity[] $users */
$users = $usersResult->getElements();
$userId = null;
/*
* Iterate through all users and check if the CompraAiAdmin user already exists
*/
foreach ($users as $id => $user) {
if ($user->getUsername() === self::API_USER) {
$userId = $id;
break;
}
}
if (!$userId) {
// GET LOCALE ID FOR API ADMIN
/** @var EntityRepositoryInterface|null $localeRepo */
$localeRepo = $this->container->get('locale.repository');
if (!$localeRepo) {
throw new \RuntimeException('Unable to load service locale.repository');
}
$localeCriteria = new Criteria();
$localeCriteria->addFilter(new EqualsFilter('code', self::API_USER_LOCALE));
$localeId = $localeRepo->searchIds($localeCriteria, $context)->firstId();
// CREATE USER PAYLOAD
$apiUserPayload = [
[
'username' => self::API_USER,
'password' => self::API_USER_PASSWORD,
'firstName' => 'COMPRA API',
'lastName' => 'Admin',
'active' => true,
'admin' => true,
'email' => $email,
'localeId' => $localeId
]
];
// CREATE THE USER
$userRepository->upsert($apiUserPayload, new Context(new SystemSource()));
}
}
}