Skip to content

Commit

Permalink
Add support for screen-bundle (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
tuj authored May 20, 2019
1 parent aa08fe9 commit e445ceb
Show file tree
Hide file tree
Showing 7 changed files with 308 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Os2Display/CoreBundle CHANGELOG

## 2.1.0

* Added support for screen-bundle.

## 2.0.0

* Symfony 3.4 upgrade.
Expand Down
5 changes: 5 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public function getConfigTreeBuilder()
// configure your bundle. See the documentation linked above for
// more information on that topic.

$rootNode
->children()
->integerNode('cache_ttl')->end()
->end();

return $treeBuilder;
}
}
8 changes: 8 additions & 0 deletions DependencyInjection/Os2DisplayCoreExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,13 @@ public function load(array $configs, ContainerBuilder $container) {
$this->dir = __DIR__;

parent::load($configs, $container);

$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$def = $container->getDefinition('os2display.middleware.service');

if (isset($config['cache_ttl'])) {
$def->replaceArgument(3, $config['cache_ttl']);
}
}
}
6 changes: 3 additions & 3 deletions Entity/Screen.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ class Screen extends ApiEntity implements GroupableEntity
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @Groups({"api", "api-bulk", "search", "screen", "timeline-screen", "campaign"})
* @Groups({"api", "api-bulk", "search", "screen", "timeline-screen", "campaign", "middleware"})
*/
private $id;

/**
* @ORM\Column(name="title", type="text", nullable=false)
* @Groups({"api", "api-bulk", "search", "screen", "timeline-screen", "campaign"})
* @Groups({"api", "api-bulk", "search", "screen", "timeline-screen", "campaign", "middleware"})
*/
private $title;

Expand Down Expand Up @@ -76,7 +76,7 @@ class Screen extends ApiEntity implements GroupableEntity

/**
* @ORM\ManyToOne(targetEntity="ScreenTemplate", inversedBy="screens")
* @Groups({"api", "api-bulk", "screen"})
* @Groups({"api", "api-bulk", "screen", "middleware"})
*/
private $template;

Expand Down
41 changes: 41 additions & 0 deletions Events/PrePushScreenSerializationEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/**
* @file
* This file is a part of the Os2Display CoreBundle.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Os2Display\CoreBundle\Events;

use Symfony\Component\EventDispatcher\Event;

/**
* Class PrePushChannelsEvent
*/
class PrePushScreenSerializationEvent extends Event
{
const NAME = 'os2display.core.pre_push_screen_serialization';

protected $screenObject;

/**
* PrePushScreenSerializationEvent constructor.
* @param \stdClass $screenObject
*/
public function __construct(\stdClass $screenObject)
{
$this->screenObject = $screenObject;
}

public function getScreenObject()
{
return $this->screenObject;
}

public function setScreenObject(\stdClass $screenObject)
{
$this->screenObject = $screenObject;
}
}
10 changes: 10 additions & 0 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ parameters:
router.options.generator_base_class: Os2Display\CoreBundle\Routing\HashtagDecodedUrlGenerator

services:
os2display.core.cache:
class: Doctrine\Common\Cache\FilesystemCache
arguments: ['%kernel.cache_dir%']
public: true

os2display.listener:
class: Os2Display\CoreBundle\EventListener\SearchIndexer
arguments: ['@jms_serializer', '@service_container', '@os2display.utility_service']
Expand Down Expand Up @@ -135,3 +140,8 @@ services:
arguments: ['sonata.media.provider.zencoder', '@sonata.media.filesystem.local', '@sonata.media.cdn.server', '@sonata.media.generator.default', '@sonata.media.thumbnail.format', '%absolute_path_to_server%', '%zencoder_api%', '@logger']
calls:
- [setTemplates, [{ helper_thumbnail: 'SonataMediaBundle:Provider:thumbnail.html.twig', helper_view: 'SonataMediaBundle:Provider:view_video.html.twig' }]]

os2display.middleware.service:
class: Os2Display\CoreBundle\Services\MiddlewareService
arguments: ['@doctrine.orm.entity_manager', '@jms_serializer', '@os2display.core.cache', 300, '@event_dispatcher']
public: true
237 changes: 237 additions & 0 deletions Services/MiddlewareService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
<?php

namespace Os2Display\CoreBundle\Services;

use Doctrine\ORM\EntityManagerInterface;
use JMS\Serializer\SerializerInterface;
use Os2Display\CoreBundle\Entity\Channel;
use Os2Display\CoreBundle\Entity\Screen;
use JMS\Serializer\SerializationContext;
use Doctrine\Common\Cache\CacheProvider;
use Os2Display\CoreBundle\Entity\ScreenTemplate;
use Os2Display\CoreBundle\Entity\SharedChannel;
use Os2Display\CoreBundle\Events\PrePushScreenSerializationEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

class MiddlewareService
{
protected $serializer;
protected $entityManager;
protected $channelRepository;
protected $sharedChannelRepository;
protected $screenRepository;
protected $cache;
protected $cacheTTL;
protected $dispatcher;

/**
* MiddlewareService constructor.
* @param \Doctrine\ORM\EntityManagerInterface $entityManager
* @param \JMS\Serializer\SerializerInterface $serializer
* @param \Doctrine\Common\Cache\CacheProvider $cache
* @param $cacheTTL
*/
public function __construct(
EntityManagerInterface $entityManager,
SerializerInterface $serializer,
CacheProvider $cache,
$cacheTTL,
EventDispatcherInterface $dispatcher
) {
$this->entityManager = $entityManager;
$this->serializer = $serializer;
$this->channelRepository = $entityManager->getRepository(
Channel::class
);
$this->sharedChannelRepository = $entityManager->getRepository(SharedChannel::class);
$this->screenRepository = $entityManager->getRepository(Screen::class);
$this->cache = $cache;
$this->cacheTTL = $cacheTTL;
$this->dispatcher = $dispatcher;
}

/**
* Get screen as an array.
*
* @param $screen
* @return mixed|string
*/
public function getScreenArray($screen)
{
$data = $this->serializer->serialize(
$screen,
'json',
SerializationContext::create()
->setGroups(array('middleware'))
);

$data = json_decode($data);

return $data;
}

/**
* Get channel as an array.
*
* @param $channel
* @return mixed|string
*/
public function getChannelArray($channel)
{
$data = $this->serializer->serialize(
$channel,
'json',
SerializationContext::create()
->setGroups(array('middleware'))
);

$data = json_decode($data);

return $data;
}

/**
* Get shared channel as an array.
*
* @param $channel
* @return mixed|string
*/
public function getSharedChannelArray($channel)
{
$data = $this->serializer->serialize(
$channel,
'json',
SerializationContext::create()
->setGroups(array('middleware'))
);

$data = json_decode($data);

return $data;
}

/**
* Get a fake screen array of 'full-screen' screen template, where
* channel is inserted in region 1.
*
* @param $channelId
* @return false|mixed|object
*/
public function getCurrentChannelArray($channelId) {
$cachedResult = $this->cache->fetch('os2display.core.channel.' . $channelId);

if ($cachedResult != false) {
return $cachedResult;
}

$channelRepository = $this->entityManager->getRepository(Channel::class);

$channel = $channelRepository->findOneById($channelId);
$channelArray = $this->getChannelArray($channel);

$channelArray->regions = [1];

// Use full-screen screen template.
$templateRepository = $this->entityManager->getRepository(ScreenTemplate::class);
$templateArray = json_decode($this->serializer->serialize(
$templateRepository->findOneById('full-screen'),
'json',
SerializationContext::create()
->setGroups(array('middleware'))
));

$result = (object)[
'channels' => [$channelArray],
'screen' => [
'id' => 1,
'title' => 'channel-public',
'options' => [],
'template' => $templateArray,
],
];

return $result;
}

/**
* @param $screenId
* @return false|mixed|object
*/
public function getCurrentScreenArray($screenId)
{
$cachedResult = $this->cache->fetch('os2display.core.screen.' . $screenId);

if ($cachedResult != false) {
return $cachedResult;
}

$screenRepository = $this->entityManager->getRepository(Screen::class);

$screen = $screenRepository->findOneById($screenId);
$screenArray = $this->getScreenArray($screen);

$result = (object)[
'channels' => [],
'screen' => $screenArray,
];

// Build result object.
foreach ($screen->getChannelScreenRegions() as $channelScreenRegion) {
$channel = $channelScreenRegion->getChannel();
$region = $channelScreenRegion->getRegion();
$channelId = null;
$data = null;
$isSharedChannel = false;

if (!is_null($channel)) {
$channelId = $channel->getId();
} else {
// Handle shared channels.
$channel = $channelScreenRegion->getSharedChannel();
$channelId = $channel->getUniqueId();
$isSharedChannel = true;
}

if (!isset($result->channels[$channelId])) {
$result->channels[$channelId] = (object)[
'regions' => [$region],
];
} else {
$result->channels[$channelId]->regions[] = $region;
}

$result->channels[$channelId]->isSharedChannel = $isSharedChannel;
}

// Send event to let other processes affect which channels are shown in
// the different regions of the screen. This is to allow e.g. campaigns
// to affect which channels are shown.
$event = new PrePushScreenSerializationEvent($result);
$this->dispatcher->dispatch(PrePushScreenSerializationEvent::NAME, $event);
$result = $event->getScreenObject();

// Serialize the channels in the region array.
foreach ($result->channels as $channelId => $channelObject) {
if (isset($channelObject->isSharedChannel) && $channelObject->isSharedChannel) {
$channel = $this->sharedChannelRepository->findOneByUniqueId($channelId);
$serializedChannel = $this->getSharedChannelArray($channel);
$serializedChannel->data->slides = json_decode($serializedChannel->data->slides);
}
else {
$channel = $this->channelRepository->findOneById($channelId);
$serializedChannel = $this->getChannelArray($channel);
}

$channelObject->data = $serializedChannel->data;

$channelObject->hash = sha1(
json_encode($result->channels[$channelId])
);
}

// Cache and return results.
$this->cache->save('os2display.core.screen.' . $screenId, $result, $this->cacheTTL);

return $result;
}
}

0 comments on commit e445ceb

Please sign in to comment.