Skip to content

Commit

Permalink
Merge branch '1.0.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
fseeger committed Oct 13, 2020
2 parents 2ea4e14 + 42c74a9 commit 088d74a
Show file tree
Hide file tree
Showing 8 changed files with 363 additions and 55 deletions.
113 changes: 113 additions & 0 deletions Attribute/AbstractAttribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

namespace LxRmp\Attribute;

use LxRmp\Util\AbstractHandler;
use Doctrine\ORM\NoResultException;
use Shopware\Bundle\AttributeBundle\Service\CrudService;

abstract class AbstractAttribute extends AbstractHandler
{
/**
* CrudService
*
* @var CrudService
*/
private $crudService;

/**
* Name of the database table
*
* @return string
*/
abstract protected function getTableName();

/**
* List of fields
*
* @return array
*/
abstract protected function getFields();

/**
* Perform update for this plugin
*
* @param string $oldVersion Old Version
*/
abstract protected function doUpdate($oldVersion);

/**
* @param \Enlight_Event_EventArgs $args
* @return mixed|void
*/
public function onPluginUpdate(\Enlight_Event_EventArgs $args)
{
$oldVersion = $args->get('oldVersion');
$this->doUpdate($oldVersion);
}

/**
* @param \Enlight_Event_EventArgs $args
* @return mixed|void
*/
public function onPluginUninstall(\Enlight_Event_EventArgs $args){}

/**
* @param $name
* @throws \Exception
*/
protected function addField($name):void
{
$this->updateField($name);
}

/**
* @param $name
* @throws \Exception
*/
protected function updateField($name):void
{
$def = $this->getDefinition($name);
$service = $this->getCrudService();
$def[2]['displayInBackend'] = true;
$service->update($this->getTableName(), $def[0], $def[1], $def[2]);
}

/**
* @param $name
* @throws \Exception
*/
protected function deleteField($name):void
{
$service = $this->getCrudService();
if ($service->get($this->getTableName(), $name)) {
$service->delete($this->getTableName(), $name);
}
}

/**
* @param $name
* @return mixed
* @throws NoResultException
*/
protected function getDefinition($name)
{
foreach ($this->getFields() as $def) {
if ($def[0] == $name) {
return $def;
}
}
throw new NoResultException();
}

/**
* @return CrudService
*/
protected function getCrudService():CrudService
{
if (!$this->crudService instanceof CrudService) {
$this->crudService = Shopware()->Container()->get('shopware_attribute.crud_service');
}
return $this->crudService;
}
}
50 changes: 50 additions & 0 deletions Attribute/Payment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace LxRmp\Attribute;

use Shopware\Bundle\AttributeBundle\Service\TypeMapping;

class Payment extends AbstractAttribute
{

/**
* Name of the database table
*
* @return string
*/
protected function getTableName():string
{
return 's_core_paymentmeans_attributes';
}

/**
* @return array
*/
protected function getFields():array
{
$fields = array();

$fields[] = [
'harmless',
TypeMapping::TYPE_BOOLEAN,
[
'label' => 'Unbedenklich',
'position' => 10
]
];
return $fields;
}

/**
* @param string $oldVersion
*/
protected function doUpdate($oldVersion):void
{
if(version_compare('1.0.3', $oldVersion, '>')){
try{
$this->addField('harmless');
}catch (\Exception $exception){
}
}
}
}
39 changes: 39 additions & 0 deletions LxRmp.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
namespace LxRmp;

use Shopware\Components\Plugin;
use Shopware\Components\Plugin\Context\InstallContext;
use Shopware\Components\Plugin\Context\UninstallContext;
use Shopware\Components\Plugin\Context\UpdateContext;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
Expand All @@ -14,6 +17,8 @@ class LxRmp extends Plugin
{

public const PLUGIN_NAMESPACE = 'LxRmp';
public const PLUGIN_UPDATE = self::PLUGIN_NAMESPACE.'_Update';
public const PLUGIN_UNINSTALL = self::PLUGIN_NAMESPACE.'_Uninstall';

/**
* @return array
Expand Down Expand Up @@ -42,4 +47,38 @@ public function build(ContainerBuilder $container)
$container->setParameter('index.plugin_dir', $this->getPath());
parent::build($container);
}

/**
* @param InstallContext $context
*/
public function install(InstallContext $context)
{
$context->getPlugin()->setVersion('1.0.0');
}

/**
* @param UpdateContext $context
*/
public function update(UpdateContext $context)
{
$eventManager = $this->getEventManager();
$eventManager->notify(self::PLUGIN_UPDATE, ['oldVersion' => $context->getCurrentVersion()]);
}

/**
* @param UninstallContext $context
*/
public function uninstall(UninstallContext $context)
{
$eventManager = $this->getEventManager();
$eventManager->notify(self::PLUGIN_UNINSTALL);
}

/**
* @return object
*/
private function getEventManager()
{
return $this->container->get('events');
}
}
8 changes: 8 additions & 0 deletions Resources/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,13 @@
<service class="LxRmp\Components\Data\QuoteModel" id="lx_rmp.component.quotemodel">
<argument type="service" id="models"/>
</service>
<service class="LxRmp\Attribute\AbstractAttribute" id="lx_rmp.attribute.abstract" abstract="true" parent="lx_rmp.util.abstract.handler"/>
<service class="LxRmp\Util\AbstractHandler" id="lx_rmp.util.abstract.handler" abstract="true">
<argument type="service" id="models"/>
<argument type="service" id="dbal_connection" />
</service>
<service class="LxRmp\Attribute\Payment" id="lx_rmp.attribute.payment" parent="lx_rmp.attribute.abstract">
<tag name="shopware.event_subscriber"/>
</service>
</services>
</container>
14 changes: 14 additions & 0 deletions Services/PaymentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,18 @@ public function getPaymentByID(int $paymentId):?string
return null;
}

public function isHarmless(int $paymentId):bool
{
$queryBuilder = $this->entityManager->getDBALQueryBuilder();
$statement = $queryBuilder->select('harmless')
->from('s_core_paymentmeans_attributes')
->where('paymentmeanID = :id')
->setParameter('id', $paymentId)
->execute();
$payment = $statement->fetch();
if($payment && array_key_exists('harmless', $payment)){
return (bool)$payment['harmless'];
}
return false;
}
}
Loading

0 comments on commit 088d74a

Please sign in to comment.