Skip to content

Commit

Permalink
Do not use deprecated field type storage features
Browse files Browse the repository at this point in the history
  • Loading branch information
emodric committed Oct 2, 2017
1 parent 218a3b7 commit 8d121de
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 110 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ matrix:
# mark as finished before allow_failures are run
fast_finish: true
include:
- php: 5.5
- php: 5.6
- php: 7.0
- php: 7.1
- php: 7.2

# test only master (+ pull requests)
branches:
Expand Down
17 changes: 5 additions & 12 deletions Core/FieldType/EnhancedSelection/EnhancedSelectionStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Netgen\Bundle\EnhancedSelectionBundle\Core\FieldType\EnhancedSelection;

use eZ\Publish\Core\FieldType\GatewayBasedStorage;
use eZ\Publish\SPI\FieldType\GatewayBasedStorage;
use eZ\Publish\SPI\Persistence\Content\Field;
use eZ\Publish\SPI\Persistence\Content\VersionInfo;

Expand All @@ -22,12 +22,9 @@ class EnhancedSelectionStorage extends GatewayBasedStorage
*/
public function storeFieldData(VersionInfo $versionInfo, Field $field, array $context)
{
/** @var \Netgen\Bundle\EnhancedSelectionBundle\Core\FieldType\EnhancedSelection\EnhancedSelectionStorage\Gateway $gateway */
$gateway = $this->getGateway($context);

$gateway->deleteFieldData($versionInfo, array($field->id));
$this->gateway->deleteFieldData($versionInfo, array($field->id));
if (!empty($field->value->externalData)) {
$gateway->storeFieldData($versionInfo, $field);
$this->gateway->storeFieldData($versionInfo, $field);
}
}

Expand All @@ -40,9 +37,7 @@ public function storeFieldData(VersionInfo $versionInfo, Field $field, array $co
*/
public function getFieldData(VersionInfo $versionInfo, Field $field, array $context)
{
/** @var \Netgen\Bundle\EnhancedSelectionBundle\Core\FieldType\EnhancedSelection\EnhancedSelectionStorage\Gateway $gateway */
$gateway = $this->getGateway($context);
$gateway->getFieldData($versionInfo, $field);
$this->gateway->getFieldData($versionInfo, $field);
}

/**
Expand All @@ -57,9 +52,7 @@ public function getFieldData(VersionInfo $versionInfo, Field $field, array $cont
*/
public function deleteFieldData(VersionInfo $versionInfo, array $fieldIds, array $context)
{
/** @var \Netgen\Bundle\EnhancedSelectionBundle\Core\FieldType\EnhancedSelection\EnhancedSelectionStorage\Gateway $gateway */
$gateway = $this->getGateway($context);
$gateway->deleteFieldData($versionInfo, $fieldIds);
$this->gateway->deleteFieldData($versionInfo, $fieldIds);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Netgen\Bundle\EnhancedSelectionBundle\Core\FieldType\EnhancedSelection\EnhancedSelectionStorage;

use eZ\Publish\Core\FieldType\StorageGateway;
use eZ\Publish\SPI\FieldType\StorageGateway;
use eZ\Publish\SPI\Persistence\Content\Field;
use eZ\Publish\SPI\Persistence\Content\VersionInfo;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,22 @@
use eZ\Publish\SPI\Persistence\Content\VersionInfo;
use Netgen\Bundle\EnhancedSelectionBundle\Core\FieldType\EnhancedSelection\EnhancedSelectionStorage\Gateway;
use PDO;
use RuntimeException;

class LegacyStorage extends Gateway
{
/**
* Connection.
*
* @var \eZ\Publish\Core\Persistence\Database\DatabaseHandler
*/
protected $connection;
protected $dbHandler;

/**
* Sets the data storage connection to use.
*
*
* @param \eZ\Publish\Core\Persistence\Database\DatabaseHandler $connection
* Constructor.
*
* @throws \RuntimeException if $connection is not an instance of
* {@link \eZ\Publish\Core\Persistence\Database\DatabaseHandler}
* @param \eZ\Publish\Core\Persistence\Database\DatabaseHandler $dbHandler
*/
public function setConnection($connection)
public function __construct(DatabaseHandler $dbHandler)
{
// This obviously violates the Liskov substitution Principle, but with
// the given class design there is no sane other option. Actually the
// dbHandler *should* be passed to the constructor, and there should
// not be the need to post-inject it.
if (!$connection instanceof DatabaseHandler) {
throw new RuntimeException('Invalid connection passed');
}

$this->connection = $connection;
$this->dbHandler = $dbHandler;
}

/**
Expand All @@ -48,20 +33,18 @@ public function setConnection($connection)
*/
public function storeFieldData(VersionInfo $versionInfo, Field $field)
{
$connection = $this->getConnection();

foreach ($field->value->externalData as $identifier) {
$insertQuery = $connection->createInsertQuery();
$insertQuery = $this->dbHandler->createInsertQuery();
$insertQuery
->insertInto($connection->quoteTable('sckenhancedselection'))
->insertInto($this->dbHandler->quoteTable('sckenhancedselection'))
->set(
$connection->quoteColumn('contentobject_attribute_id'),
$this->dbHandler->quoteColumn('contentobject_attribute_id'),
$insertQuery->bindValue($field->id, null, PDO::PARAM_INT)
)->set(
$connection->quoteColumn('contentobject_attribute_version'),
$this->dbHandler->quoteColumn('contentobject_attribute_version'),
$insertQuery->bindValue($versionInfo->versionNo, null, PDO::PARAM_INT)
)->set(
$connection->quoteColumn('identifier'),
$this->dbHandler->quoteColumn('identifier'),
$insertQuery->bindValue($identifier)
);

Expand Down Expand Up @@ -89,19 +72,17 @@ public function getFieldData(VersionInfo $versionInfo, Field $field)
*/
public function deleteFieldData(VersionInfo $versionInfo, array $fieldIds)
{
$connection = $this->getConnection();

$query = $connection->createDeleteQuery();
$query = $this->dbHandler->createDeleteQuery();
$query
->deleteFrom($connection->quoteTable('sckenhancedselection'))
->deleteFrom($this->dbHandler->quoteTable('sckenhancedselection'))
->where(
$query->expr->lAnd(
$query->expr->in(
$connection->quoteColumn('contentobject_attribute_id'),
$this->dbHandler->quoteColumn('contentobject_attribute_id'),
$fieldIds
),
$query->expr->eq(
$connection->quoteColumn('contentobject_attribute_version'),
$this->dbHandler->quoteColumn('contentobject_attribute_version'),
$query->bindValue($versionInfo->versionNo, null, PDO::PARAM_INT)
)
)
Expand All @@ -110,22 +91,6 @@ public function deleteFieldData(VersionInfo $versionInfo, array $fieldIds)
$query->prepare()->execute();
}

/**
* Returns the active connection.
*
* @throws \RuntimeException if no connection has been set, yet
*
* @return \eZ\Publish\Core\Persistence\Database\DatabaseHandler
*/
protected function getConnection()
{
if ($this->connection === null) {
throw new RuntimeException('Missing database connection.');
}

return $this->connection;
}

/**
* Returns the data for the given $fieldId and $versionNo.
*
Expand All @@ -136,20 +101,18 @@ protected function getConnection()
*/
protected function loadFieldData($fieldId, $versionNo)
{
$connection = $this->getConnection();

$query = $connection->createSelectQuery();
$query = $this->dbHandler->createSelectQuery();
$query
->selectDistinct('identifier')
->from($connection->quoteTable('sckenhancedselection'))
->from($this->dbHandler->quoteTable('sckenhancedselection'))
->where(
$query->expr->lAnd(
$query->expr->eq(
$connection->quoteColumn('contentobject_attribute_id', 'sckenhancedselection'),
$this->dbHandler->quoteColumn('contentobject_attribute_id', 'sckenhancedselection'),
$query->bindValue($fieldId, null, PDO::PARAM_INT)
),
$query->expr->eq(
$connection->quoteColumn('contentobject_attribute_version', 'sckenhancedselection'),
$this->dbHandler->quoteColumn('contentobject_attribute_version', 'sckenhancedselection'),
$query->bindValue($versionNo, null, PDO::PARAM_INT)
)
)
Expand Down
2 changes: 2 additions & 0 deletions Resources/config/fieldtypes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ services:

ezpublish.fieldType.sckenhancedselection.externalStorage:
class: "%ezpublish.fieldType.sckenhancedselection.externalStorage.class%"
arguments:
- "@ezpublish.fieldType.sckenhancedselection.storage_gateway"
tags:
- {name: ezpublish.fieldType.externalStorageHandler, alias: sckenhancedselection}

Expand Down
4 changes: 2 additions & 2 deletions Resources/config/storage_engines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ services:

ezpublish.fieldType.sckenhancedselection.storage_gateway:
class: "%ezpublish.fieldType.sckenhancedselection.storage_gateway.class%"
tags:
- {name: ezpublish.fieldType.externalStorageHandler.gateway, alias: sckenhancedselection, identifier: LegacyStorage}
arguments:
- "@ezpublish.api.storage_engine.legacy.dbhandler"
Original file line number Diff line number Diff line change
Expand Up @@ -34,32 +34,14 @@ public function setUp()
->setMethods(array('createDeleteQuery', 'quoteColumn', 'createInsertQuery', 'createSelectQuery'))
->getMock();

$this->storage = new LegacyStorage();
$this->storage = new LegacyStorage($this->connection);
}

public function testInstanceOfGateway()
{
$this->assertInstanceOf(Gateway::class, $this->storage);
}

public function testConnectionHandling()
{
$handler = $this->getMockForAbstractClass(DatabaseHandler::class);

$this->storage->setConnection($handler);
}

/**
* @expectedException \RuntimeException
* @expectedExceptionMessage Invalid connection passed
*/
public function testConnectionHandlingWithInvalidConnection()
{
$handler = new \stdClass();

$this->storage->setConnection($handler);
}

public function testDeleteFieldData()
{
$connection = $this->getMockBuilder(Connection::class)
Expand Down Expand Up @@ -90,7 +72,6 @@ public function testDeleteFieldData()
$versionInfo = new VersionInfo();
$fields = array(1, 2, 3);

$this->storage->setConnection($this->connection);
$this->storage->deleteFieldData($versionInfo, $fields);
}

Expand Down Expand Up @@ -134,7 +115,6 @@ public function testStoreFieldData()
)
);

$this->storage->setConnection($this->connection);
$this->storage->storeFieldData($versionInfo, $field);
}

Expand Down Expand Up @@ -187,18 +167,6 @@ public function testGetFieldData()
)
);

$this->storage->setConnection($this->connection);
$this->storage->getFieldData($versionInfo, $field);
}

/**
* @expectedException \RuntimeException
*/
public function testGetConnection()
{
$versionInfo = new VersionInfo();
$fields = array(1, 2, 3);

$this->storage->deleteFieldData($versionInfo, $fields);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@ public function setUp()
{
$this->gateway = $this->getMockBuilder(EnhancedSelectionStorage\Gateway\LegacyStorage::class)
->disableOriginalConstructor()
->setMethods(array('setConnection', 'deleteFieldData', 'storeFieldData', 'getFieldData'))
->setMethods(array('deleteFieldData', 'storeFieldData', 'getFieldData'))
->getMock();

$gateways = array('enhancedselection' => $this->gateway);

$this->storage = new EnhancedSelectionStorage($gateways);
$this->storage = new EnhancedSelectionStorage($this->gateway);
}

public function testHasFieldData()
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
}
],
"require": {
"ezsystems/ezpublish-kernel": ">=2014.11|^5.4|^6.5.2"
"ezsystems/ezpublish-kernel": "^6.11"
},
"require-dev": {
"phpunit/phpunit": "^4.8|^5.7",
Expand Down

0 comments on commit 8d121de

Please sign in to comment.