Skip to content

Commit

Permalink
Use an array collection when deleting a broadcast
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin de Keijzer committed Oct 26, 2020
1 parent a3db45a commit 4ca95e6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 55 deletions.
42 changes: 18 additions & 24 deletions Service/BroadcastManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
use Martin1982\LiveBroadcastBundle\Entity\Channel\AbstractChannel;
use Martin1982\LiveBroadcastBundle\Entity\Channel\PlannedChannelInterface;
use Martin1982\LiveBroadcastBundle\Entity\LiveBroadcast;
use Martin1982\LiveBroadcastBundle\Entity\LiveBroadcastRepository;
use Martin1982\LiveBroadcastBundle\Entity\Metadata\StreamEvent;
use Martin1982\LiveBroadcastBundle\Entity\Metadata\StreamEventRepository;
use Martin1982\LiveBroadcastBundle\Exception\LiveBroadcastException;
use Martin1982\LiveBroadcastBundle\Exception\LiveBroadcastOutputException;
use Martin1982\LiveBroadcastBundle\Service\ChannelApi\ChannelApiInterface;
Expand Down Expand Up @@ -48,22 +50,10 @@ public function __construct(EntityManager $entityManager, ChannelApiStack $apiSt
$this->apiStack = $apiStack;
}

/**
* Keep a remote connection alive
*/
public function keepConnectionAlive(): void
{
$connection = $this->entityManager->getConnection();
if (!$connection->ping()) {
$connection->close();
$connection->connect();
}
}

/**
* Get a broadcast by it's id
*
* @param string $broadcastId
* @param string|int $broadcastId
*
* @return LiveBroadcast|null|Object
*/
Expand Down Expand Up @@ -139,7 +129,11 @@ public function preUpdate(LiveBroadcast $broadcast): void
*/
public function preDelete(LiveBroadcast $broadcast): void
{
$this->removeLiveEvents($broadcast, $broadcast->getOutputChannels());
$outputChannels = $broadcast->getOutputChannels();

if ($outputChannels->count() > 0) {
$this->removeLiveEvents($broadcast, $outputChannels->toArray());
}
}

/**
Expand Down Expand Up @@ -192,17 +186,17 @@ public function getPlannedBroadcasts()
}

/**
* @return \Doctrine\Common\Persistence\ObjectRepository|\Doctrine\ORM\EntityRepository|\Martin1982\LiveBroadcastBundle\Entity\LiveBroadcastRepository
* @return LiveBroadcastRepository
*/
public function getBroadcastsRepository()
public function getBroadcastsRepository(): LiveBroadcastRepository
{
return $this->entityManager->getRepository(LiveBroadcast::class);
}

/**
* @return \Doctrine\Common\Persistence\ObjectRepository|\Doctrine\ORM\EntityRepository|\Martin1982\LiveBroadcastBundle\Entity\Metadata\StreamEventRepository
* @return StreamEventRepository
*/
public function getEventsRepository()
public function getEventsRepository(): StreamEventRepository
{
return $this->entityManager->getRepository(StreamEvent::class);
}
Expand Down Expand Up @@ -271,7 +265,7 @@ private function getDeletedChannels(Collection $previousState, Collection $newSt
private function createLiveEvents(LiveBroadcast $broadcast, array $channels): void
{
foreach ($channels as $channel) {
if ($channel instanceof PlannedChannelInterface) {
if ($channel instanceof PlannedChannelInterface && $channel instanceof AbstractChannel) {
$api = $this->apiStack->getApiForChannel($channel);

if ($api) {
Expand All @@ -288,7 +282,7 @@ private function createLiveEvents(LiveBroadcast $broadcast, array $channels): vo
private function updateLiveEvents(LiveBroadcast $broadcast, array $channels): void
{
foreach ($channels as $channel) {
if ($channel instanceof PlannedChannelInterface) {
if ($channel instanceof PlannedChannelInterface && $channel instanceof AbstractChannel) {
$api = $this->apiStack->getApiForChannel($channel);

if ($api) {
Expand All @@ -305,17 +299,17 @@ private function updateLiveEvents(LiveBroadcast $broadcast, array $channels): vo
private function removeLiveEvents(LiveBroadcast $broadcast, array $channels): void
{
foreach ($channels as $channel) {
if ($channel instanceof PlannedChannelInterface) {
if ($channel instanceof PlannedChannelInterface && $channel instanceof AbstractChannel) {
$api = $this->apiStack->getApiForChannel($channel);
$this->attemptDeleteOnApi($broadcast, $channel, $api);
}
}
}

/**
* @param LiveBroadcast $broadcast
* @param AbstractChannel $channel
* @param ChannelApiInterface $api
* @param LiveBroadcast $broadcast
* @param AbstractChannel $channel
* @param ChannelApiInterface|null $api
*/
private function attemptDeleteOnApi(LiveBroadcast $broadcast, AbstractChannel $channel, ChannelApiInterface $api = null): void
{
Expand Down
36 changes: 5 additions & 31 deletions Tests/Service/BroadcastManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
namespace Martin1982\LiveBroadcastBundle\Tests\Service;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\DBAL\Connection;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\OptimisticLockException;
Expand All @@ -19,6 +18,7 @@
use Martin1982\LiveBroadcastBundle\Entity\LiveBroadcast;
use Martin1982\LiveBroadcastBundle\Entity\LiveBroadcastRepository;
use Martin1982\LiveBroadcastBundle\Entity\Metadata\StreamEvent;
use Martin1982\LiveBroadcastBundle\Entity\Metadata\StreamEventRepository;
use Martin1982\LiveBroadcastBundle\Exception\LiveBroadcastException;
use Martin1982\LiveBroadcastBundle\Service\BroadcastManager;
use Martin1982\LiveBroadcastBundle\Service\ChannelApi\ChannelApiInterface;
Expand All @@ -42,32 +42,6 @@ class BroadcastManagerTest extends TestCase
*/
protected $stack;

/**
* Test connection keepalive method
*/
public function testKeepConnectionAlive(): void
{
$connection = $this->createMock(Connection::class);
$connection->expects(self::atLeastOnce())
->method('ping')
->willReturn(false);
$connection->expects(self::atLeastOnce())
->method('close')
->willReturn(true);
$connection->expects(self::atLeastOnce())
->method('connect')
->willReturn(true);

/** @var MockObject|EntityManager $entityManager */
$entityManager = $this->createMock(EntityManager::class);
$entityManager->expects(self::atLeastOnce())
->method('getConnection')
->willReturn($connection);

$manager = new BroadcastManager($entityManager, $this->stack);
$manager->keepConnectionAlive();
}

/**
* Test getting a broadcast entity by id
*/
Expand Down Expand Up @@ -135,7 +109,7 @@ public function testPreUpdate(): void

$broadcastNewState = $this->createMock(LiveBroadcast::class);
$broadcastOldState = $this->createMock(LiveBroadcast::class);
$broadcastRepository = $this->createMock(EntityRepository::class);
$broadcastRepository = $this->createMock(LiveBroadcastRepository::class);
$api = $this->createMock(FacebookApiService::class);

$oldChannelList = new ArrayCollection();
Expand Down Expand Up @@ -188,7 +162,7 @@ public function testUpdateWithNoPreviousState():void
{
$broadcast = $this->createMock(LiveBroadcast::class);

$broadcastRepository = $this->createMock(EntityRepository::class);
$broadcastRepository = $this->createMock(LiveBroadcastRepository::class);
$broadcastRepository->expects(self::atLeastOnce())
->method('findOneBy')
->willReturn(null);
Expand All @@ -210,7 +184,7 @@ public function testUpdateWithNoPreviousState():void
*/
public function testPreDelete(): void
{
$channels = [ $this->createMock(ChannelFacebook::class) ];
$channels = new ArrayCollection([ $this->createMock(ChannelFacebook::class) ]);

$api = $this->createMock(ChannelApiInterface::class);
$api->expects(self::once())
Expand Down Expand Up @@ -272,7 +246,7 @@ public function testGetEventsRepository(): void
$this->entityManager->expects(self::atLeastOnce())
->method('getRepository')
->with(StreamEvent::class)
->willReturn($this->createMock(EntityRepository::class));
->willReturn($this->createMock(StreamEventRepository::class));

$broadcastManager = new BroadcastManager($this->entityManager, $this->stack);
self::assertInstanceOf(EntityRepository::class, $broadcastManager->getEventsRepository());
Expand Down

0 comments on commit 4ca95e6

Please sign in to comment.