<?php declare(strict_types=1);
namespace Tds\Merware;
use League\Flysystem\FilesystemInterface;
use Shopware\Core\Content\Category\CategoryDefinition;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
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\ActivateContext;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Shopware\Core\System\CustomField\CustomFieldTypes;
use Tds\Merware\Service\Core\FileService;
/**
* Class TdsMerware
*
* @package Tds\Merware
*/
class TdsMerware extends Plugin
{
/**
* @var FilesystemInterface
*/
private $fileSystem;
/**
* {@inheritDoc}
*/
public function install(InstallContext $installContext): void
{
/**
* @var EntityRepositoryInterface $customFieldSetRepository
*/
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$customFieldSetRepository->create($this->getCustomFieldSets(), $installContext->getContext());
}
/**
* {@inheritDoc}
*
* @param UninstallContext $uninstallContext
*
* @throws InconsistentCriteriaIdsException
*/
public function uninstall(UninstallContext $uninstallContext): void
{
$this->deleteCustomFieldSet($this->getCustomFieldSets(), $uninstallContext->getContext());
$this->deleteCustomFields($this->getCustomFieldSets(), $uninstallContext->getContext());
}
/**
* Helper-function to trigger the deletion of plugin custom field sets
*
* @param $customFieldSets
* @param $context
* @throws InconsistentCriteriaIdsException
*/
private function deleteCustomFieldSet($customFieldSets, $context): void
{
/**
* @var EntityRepositoryInterface $customFieldSetRepository
*/
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$ids = $this->getCustomFieldSetIds($customFieldSets, $context);
if (!empty($ids)) {
$customFieldSetRepository->delete($ids, $context);
}
}
/**
* Helper-function to get custom field set ids
*
* @param $customFieldSets
* @param $context
*
* @return array
*
* @throws InconsistentCriteriaIdsException
*/
private function getCustomFieldSetIds($customFieldSets, $context)
{
/**
* @var EntityRepositoryInterface $customFieldSetRepository
*/
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$ids = [];
foreach ($customFieldSets as $customFieldSet) {
$criteria = new Criteria();
$criteria->addFilter(
new EqualsFilter('name', $customFieldSet['name'])
);
$customFieldSetId = $customFieldSetRepository->searchIds($criteria, $context)->firstId();
if (!empty($customFieldSetId)) {
$ids[] = [
'id' => $customFieldSetId
];
}
}
return $ids;
}
/**
* Helper-function to delete custom fields
*
* @param $customFieldSets
* @param $context
*
* @throws InconsistentCriteriaIdsException
*/
private function deleteCustomFields($customFieldSets, $context): void
{
/**
* @var EntityRepositoryInterface $customFieldRepository
*/
$customFieldRepository = $this->container->get('custom_field.repository');
$ids = $this->getCustomFieldIds($customFieldSets, $context);
if (!empty($ids)) {
$customFieldRepository->delete($ids, $context);
}
}
/**
* Helper-function to get a list of custom field ids
*
* @param $customFieldSets
* @param $context
*
* @return array
*
* @throws InconsistentCriteriaIdsException
*/
private function getCustomFieldIds($customFieldSets, $context)
{
/**
* @var EntityRepositoryInterface $customFieldRepository
*/
$customFieldRepository = $this->container->get('custom_field.repository');
$ids = [];
foreach ($customFieldSets as $customFieldSet) {
if (!empty($customFieldSet['customFields'])) {
foreach ($customFieldSet['customFields'] as $customField) {
$criteria = new Criteria();
$criteria->addFilter(
new EqualsFilter('name', $customField['name'])
);
$customFieldId = $customFieldRepository->searchIds($criteria, $context)->firstId();
if (!empty($customFieldId)) {
$ids[] = [
'id' => $customFieldId
];
}
}
}
}
return $ids;
}
/**
* Helper-function to get a list of plugin-required custom field data
*
* @return array
*/
private function getCustomFieldSets()
{
return [
[
'name' => 'tds_merware_category',
'config' => [
'label' => [
'de-DE' => 'MerWare Kategorien',
'en-GB' => 'MerWare categories',
],
],
'customFields' => [
[
'name' => 'tds_merware_category_id',
'type' => CustomFieldTypes::TEXT,
'config' => [
'customFieldPosition' => 1,
'label' => [
'de-DE' => 'Interne ID',
'en-GB' => 'Internal ID',
],
],
]
],
'relations' => [
[
'entityName' => $this->container->get(CategoryDefinition::class)->getEntityName()
]
]
]
];
}
/**
* {@inheritDoc}
*
* @param ActivateContext $activateContext
*/
public function activate(ActivateContext $activateContext): void
{
parent::activate($activateContext);
$defaultDirectories = FileService::getDefaultDirectories();
$this->createPluginDirectories($defaultDirectories);
}
/**
* Helper-function to create plugin directories
*
* @param array $directories
*/
private function createPluginDirectories($directories = [])
{
foreach ($directories as $directory) {
if (!$this->fileSystem->has($directory)) {
$this->fileSystem->createDir($directory);
}
}
}
/**
* Custom setter to set the required file system
*
* @param FilesystemInterface $fileSystemInterface
*/
public function setFileSystem(FilesystemInterface $fileSystemInterface): void
{
$this->fileSystem = $fileSystemInterface;
}
}