<?php declare(strict_types=1);
namespace Res\ResChannableConnector;
use Shopware\Core\Framework\Api\Util\AccessKeyHelper;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UpdateContext;
use Shopware\Core\Framework\Uuid\Uuid;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\System\User\UserEntity;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use function explode;
class ResChannableConnector extends Plugin
{
/**
* {@inheritdoc}
*/
public function build(ContainerBuilder $container): void
{
parent::build($container);
$loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/DependencyInjection/'));
$loader->load('setting.xml');
}
public function install(InstallContext $context): void
{
$this->createApiUser($context->getContext());
}
public function update(UpdateContext $context): void
{
$this->updateApiUser($context->getContext());
}
/**
* Update the api user
*/
private function updateApiUser(Context $context): void
{
$user = $this->getApiUser($context);
if ( !$user->count() ) {
return;
}
/** @var EntityRepositoryInterface $userRepository */
$userRepository = $this->container->get('user.repository');
# Create system context because of write protection
$systemContext = Context::createDefaultContext();
$user = $user->getEntities()->first();
$userRepository->update([
[
'id' => $user->getId(),
'admin' => false
]
],
$systemContext
);
}
/**
* Check api user exist
*/
private function checkApiUser(Context $context): bool
{
return ($this->getApiUser($context)->count() > 0);
}
/**
* Check api user exist
*/
private function getApiUser(Context $context): EntitySearchResult
{
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('username', 'ChannableApiUser'));
/** @var EntityRepositoryInterface $userRepository */
$userRepository = $this->container->get('user.repository');
return $userRepository->search($criteria,$context);
}
/**
* Creates the api user
*/
private function createApiUser(Context $context): void
{
if ( $this->checkApiUser($context) ) {
$this->updateApiUser($context);
return;
}
$userEntityId = Uuid::randomHex();
$criteria = new Criteria();
$criteria->addSorting(new FieldSorting('createdAt', FieldSorting::ASCENDING));
$criteria->setLimit(1);
/** @var EntityRepositoryInterface $userRepository */
$userRepository = $this->container->get('user.repository');
/** @var UserEntity|null $shopUser */
$shopUser = $userRepository->search($criteria, $context)->getEntities()->first();
$userMail = $shopUser->getEmail();
$userMail = explode('@',$userMail);
$userMail = $userEntityId.'@'.$userMail[1];
$accessKeyId = Uuid::randomHex();
$accessKey = AccessKeyHelper::generateAccessKey('user');
$secretAccessKey = AccessKeyHelper::generateSecretAccessKey();
# Create system context because of write protection
$systemContext = Context::createDefaultContext();
$access = [
'id' => $accessKeyId,
'writeAccess' => true,
'userId' => $userEntityId,
'label' => 'Channable integration',
'accessKey' => $accessKey,
'secretAccessKey' => $secretAccessKey,
];
$newUser = [
'id' => $userEntityId,
'firstName' => 'Channable',
'lastName' => 'API',
'password' => $accessKey,
'username' => 'ChannableApiUser',
'email' => $userMail,
'localeId' => $shopUser->getLocaleId(),
'active' => true,
'admin' => false,
'avatarId' => $shopUser->getAvatarId(),
'accessKeys' => [$access]
];
$userRepository->create([$newUser], $systemContext);
}
}