<?php declare(strict_types=1);
/**
* @copyright 2020 Crehler Sp. z o. o. <https://crehler.com>
*
* @author Jakub MedyĆski <jme@crehler.com>
* @support Crehler <support@crehler.com>
* @created 27 lip 2020
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Crehler\Arrangements;
use Crehler\Arrangements\Lifecycle\Install;
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\DataAbstractionLayer\Search\IdSearchResult;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\ActivateContext;
use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UpdateContext;
use Shopware\Core\System\CustomField\CustomFieldTypes;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
class CrehlerArrangements extends Plugin
{
public const CATEGORY_CUSTOM_FIELDS = [
'name' => 'crehler_category_is_arrangements',
'type' => CustomFieldTypes::BOOL,
];
public function build(ContainerBuilder $container): void
{
parent::build($container);
$loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/DependencyInjection/'));
$loader->load('logger.xml');
$loader->load('entity.xml');
$loader->load('subscriber.xml');
$loader->load('service.xml');
$loader->load('snippets.xml');
$loader->load('message.xml');
}
public function install(InstallContext $installContext): void
{
$install = new Install($this->container);
$install->run($installContext->getContext());
parent::install($installContext);
}
public function update(UpdateContext $updateContext): void
{
$install = new Install($this->container);
$install->run($updateContext->getContext());
parent::update($updateContext);
}
public function activate(ActivateContext $activateContext): void
{
$this->activateCustomField(self::CATEGORY_CUSTOM_FIELDS, $activateContext->getContext(), $this->container->get('custom_field.repository'));
}
public function deactivate(DeactivateContext $deactivateContext): void
{
}
private function activateCustomField(array $customField, Context $context, EntityRepositoryInterface $customFieldRepository): void
{
$customFieldIds = $this->getCustomFieldIds($customField, $context, $customFieldRepository);
if ($customFieldIds->getTotal() !== 0) {
return;
}
$customFieldRepository->upsert([$customField], $context);
}
private function getCustomFieldIds(array $customField, Context $context, EntityRepositoryInterface $customFieldRepository): IdSearchResult
{
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('name', $customField['name']));
return $customFieldRepository->searchIds($criteria, $context);
}
}