Skip to content

Instantly share code, notes, and snippets.

@screeny05
Created March 12, 2018 12:34
Show Gist options
  • Save screeny05/7f9fb46a05124eab0f94a64cfdbbb100 to your computer and use it in GitHub Desktop.
Save screeny05/7f9fb46a05124eab0f94a64cfdbbb100 to your computer and use it in GitHub Desktop.
LegacyMediaById.php
<?php
use Shopware\Bundle\StoreFrontBundle\Service\Core\ContextService;
use Shopware\Bundle\StoreFrontBundle\Gateway\DBAL\MediaGateway;
use Shopware\Components\Compatibility\LegacyStructConverter;
use Enlight_Template_Default;
use InvalidArgumentException;
/**
* @author Sebastian Langer <[email protected]>
* @license MIT
*/
class LegacyMediaById implements SmartyPluginInterface
{
/** @var ContextService $contextService */
private $contextService;
/** @var MediaGateway $mediaGateway */
private $mediaGateway;
/** @var LegacyStructConverter $legacyStructConverter */
private $legacyStructConverter;
/**
* @param ContextService $contextService
* @param MediaGateway $mediaGateway
* @param LegacyStructConverter $legacyStructConverter
*/
public function __construct(
ContextService $contextService,
MediaGateway $mediaGateway,
LegacyStructConverter $legacyStructConverter
) {
$this->contextService = $contextService;
$this->mediaGateway = $mediaGateway;
$this->legacyStructConverter = $legacyStructConverter;
}
/**
* Get the legacy media struct of the media-object by its id.
* Returns null if not found.
*
* @param array $params
* @param $template
* @throws InvalidArgumentException
*/
public function smartyFunctionLegacyMediaById(array $params, Enlight_Template_Default $template)
{
if (!array_key_exists('id', $params) || !array_key_exists('assign', $params)) {
throw new InvalidArgumentException('Arguments id and assign must be given.');
}
$id = $params['id'];
$assign = $params['assign'];
$context = $this->contextService->getContext();
$mediaStruct = $this->mediaGateway->get($id, $context);
$legacyMedia = $this->legacyStructConverter->convertMediaStruct($mediaStruct);
if (!is_array($legacyMedia)) {
$template->assign($assign, null);
} else {
$template->assign($assign, $legacyMedia);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment