<?php declare(strict_types=1);
namespace Samson;
if (file_exists(dirname(__DIR__) . '/vendor/autoload.php')) {
require_once dirname(__DIR__) . '/vendor/autoload.php';
}
/***
*
* This file is part of the "SAMSON Shop" project.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* (c) 2020
*
***/
use Samson\MessageQueue\Middleware\SentryMiddlewareCompilerPass;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\ActivateContext;
use Shopware\Core\Framework\Plugin\Context\UpdateContext;
use Shopware\Storefront\Framework\ThemeInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Symfony\Component\DependencyInjection\ContainerBuilder;
/**
* Bootstrap file for SAMSON plugin.
*
* Class SamsonCustomer
* @package Samson
* @author Artur Seitz <artur.seitz@dkd.de>
*/
class SamsonCustomer extends Plugin implements ThemeInterface
{
public function build(ContainerBuilder $container): void
{
parent::build($container);
$container->addCompilerPass(new SentryMiddlewareCompilerPass());
}
/** {@inheritDoc} */
public function update(UpdateContext $updateContext): void
{
parent::update($updateContext);
$this->updateStructure();
$currentVersion = $updateContext->getCurrentPluginVersion();
if (version_compare($currentVersion, '1.1.3') < 0) {
$this->createSalesCategory($updateContext);
}
}
private function createSalesCategory(UpdateContext $updateContext):void {
$context = $updateContext->getContext();
$criteria = (new Criteria())
->setLimit(1)
->addFilter(new EqualsFilter('parentId', null));
$categoryRepository = $this->container->get('category.repository');
$productStreamRepository = $this->container->get('product_stream.repository');
$productStreamFilterRepository = $this->container->get('product_stream_filter.repository');
$main = $categoryRepository->search($criteria, $context);
if ($main->count() == 0) {
return;
}
$main = $main->first()->getId();
$productStreamId = $productStreamRepository->create([[
'name' => 'Products for Sale',
]], $context)->getEvents()->first()->getIds()[0];
$orFilterId = $productStreamFilterRepository->create([[
'type' => 'multi',
'operator' => 'OR',
'productStreamId' => $productStreamId
]], $context)->getEvents()->first()->getIds()[0];
$andFilterId = $productStreamFilterRepository->create([[
'type' => 'multi',
'operator' => 'AND',
'productStreamId' => $productStreamId,
'parentId' => $orFilterId
]], $context)->getEvents()->first()->getIds()[0];
$productStreamFilterRepository->create([[
'type' => 'equals',
'field' => 'customFields.samson_product_is_on_sale',
'value' => '1',
'productStreamId' => $productStreamId,
'parentId' => $andFilterId
]], $context);
$categoryRepository->create([[
'name' => 'Sales',
'type' => 'page',
'productAssignmentType' => 'product_stream',
'parentId' => $main,
'productStreamId' => $productStreamId
]], $context);
}
/** {@inheritDoc} */
public function activate(ActivateContext $activateContext): void
{
parent::activate($activateContext);
$this->updateStructure();
}
/**
* Creates new custom fields and property groups.
*/
protected function updateStructure(): void
{
/* @todo */
}
}