From 9ad6a2142ba19e7ca09076039731e4f2e0277f0c Mon Sep 17 00:00:00 2001 From: Martin de Keijzer Date: Thu, 3 Mar 2022 14:03:06 +0100 Subject: [PATCH] Update code inspections --- Admin/Block/FacebookBlockService.php | 4 +- Admin/Block/YouTubeBlockService.php | 14 +----- Admin/ChannelAdmin.php | 44 +++++++++++-------- Admin/InputAdmin.php | 16 +++---- Admin/LiveBroadcastAdmin.php | 22 ++++++---- Controller/CRUDController.php | 44 +++++++++++-------- .../LiveBroadcastSonataAdminExtension.php | 2 + README.md | 6 +-- Resources/views/Block/facebook_auth.html.twig | 4 +- Tests/LiveBroadcastSonataAdminBundleTest.php | 3 +- 10 files changed, 84 insertions(+), 75 deletions(-) diff --git a/Admin/Block/FacebookBlockService.php b/Admin/Block/FacebookBlockService.php index 813c62d..1de0e05 100644 --- a/Admin/Block/FacebookBlockService.php +++ b/Admin/Block/FacebookBlockService.php @@ -21,12 +21,12 @@ class FacebookBlockService extends AbstractBlockService /** * @var FacebookApiService */ - protected $apiService; + protected FacebookApiService $apiService; /** * @var ChannelAdmin */ - protected $admin; + protected ChannelAdmin $admin; /** * FacebookBlockService constructor diff --git a/Admin/Block/YouTubeBlockService.php b/Admin/Block/YouTubeBlockService.php index 50d7bce..a502866 100644 --- a/Admin/Block/YouTubeBlockService.php +++ b/Admin/Block/YouTubeBlockService.php @@ -6,7 +6,6 @@ */ namespace Martin1982\LiveBroadcastSonataAdminBundle\Admin\Block; -use Google_Client; use Martin1982\LiveBroadcastBundle\Exception\LiveBroadcastException; use Martin1982\LiveBroadcastBundle\Service\ChannelApi\Client\GoogleClient; use Sonata\BlockBundle\Block\BlockContextInterface; @@ -14,7 +13,6 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\HttpFoundation\Response; -use Symfony\Component\HttpFoundation\Session\Session; use Twig\Environment; /** @@ -25,12 +23,12 @@ class YouTubeBlockService extends AbstractBlockService /** * @var GoogleClient */ - protected $googleClient; + protected GoogleClient $googleClient; /** * @var RequestStack */ - protected $requestStack; + protected RequestStack $requestStack; /** * YouTubeBlockService constructor @@ -58,20 +56,12 @@ public function __construct(Environment $twig, GoogleClient $googleClient, Reque public function execute(BlockContextInterface $blockContext, Response $response = null): Response { $client = $this->googleClient->getClient(); - if (!$client instanceof Google_Client) { - throw new LiveBroadcastException('Could not load the google client'); - } - $request = $this->requestStack->getCurrentRequest(); if (!$request) { $request = new Request(); } $session = $request->getSession(); - if (!$session) { - $request->setSession(new Session()); - } - $refreshToken = $session->get('youTubeRefreshToken'); if ($refreshToken) { $client->fetchAccessTokenWithRefreshToken($refreshToken); diff --git a/Admin/ChannelAdmin.php b/Admin/ChannelAdmin.php index e80d0eb..76e76c9 100644 --- a/Admin/ChannelAdmin.php +++ b/Admin/ChannelAdmin.php @@ -27,7 +27,7 @@ class ChannelAdmin extends AbstractAdmin /** * @var array */ - protected $subclassConfigs = []; + protected array $subclassConfigs = []; /** * ChannelAdmin constructor @@ -43,9 +43,13 @@ public function __construct(string $code, string $class, string $baseControllerN } /** - * {@inheritdoc} + * Get the correct channel template. + * + * @param string $name + * + * @return string */ - public function getTemplate($name): string + public function getTemplate(string $name): string { $subject = $this->getSubject(); @@ -65,7 +69,7 @@ public function getTemplate($name): string * * @param array $configs */ - public function setSubclassConfigs($configs): void + public function setSubclassConfigs(array $configs): void { $this->subclassConfigs = $configs; } @@ -73,7 +77,7 @@ public function setSubclassConfigs($configs): void /** * @param AbstractChannel[] $subclasses */ - public function setConfiguredSubclasses($subclasses): void + public function setConfiguredSubclasses(array $subclasses): void { $configuredSubclasses = []; $config = $this->subclassConfigs; @@ -88,7 +92,9 @@ public function setConfiguredSubclasses($subclasses): void } /** - * {@inheritdoc} + * Configure extra admin routes. + * + * @param RouteCollectionInterface $collection */ protected function configureRoutes(RouteCollectionInterface $collection): void { @@ -101,7 +107,7 @@ protected function configureRoutes(RouteCollectionInterface $collection): void * * @throws \RuntimeException */ - protected function configureFormFields(FormMapper $formMapper): void + protected function configureFormFields(FormMapper $form): void { $subject = $this->getSubject(); @@ -110,7 +116,7 @@ protected function configureFormFields(FormMapper $formMapper): void $nameClasses = 'generic-channel-name input-yt-channelname'; } - $formMapper + $form ->with('Channel') ->add('channelName', TextType::class, [ 'label' => 'Channel name', @@ -118,30 +124,30 @@ protected function configureFormFields(FormMapper $formMapper): void ]); if (!$subject instanceof PlannedChannelInterface) { - $formMapper->add('streamKey', TextType::class, ['label' => 'Stream key']); - $formMapper->add('streamServer', TextType::class, ['label' => 'Stream server']); + $form->add('streamKey', TextType::class, ['label' => 'Stream key']); + $form->add('streamServer', TextType::class, ['label' => 'Stream server']); } if ($subject instanceof ChannelFacebook) { - $formMapper->add('accessToken', HiddenType::class, [ + $form->add('accessToken', HiddenType::class, [ 'attr' => ['class' => 'fb-access-token'], ]); - $formMapper->add('fbEntityId', HiddenType::class, [ + $form->add('fbEntityId', HiddenType::class, [ 'attr' => ['class' => 'fb-entity-id'], ]); } if ($subject instanceof ChannelYouTube) { - $formMapper->add('youTubeChannelName', TextType::class, [ + $form->add('youTubeChannelName', TextType::class, [ 'attr' => ['class' => 'input-yt-channelname', 'readonly' => 'readonly'], ]); - $formMapper->add('refreshToken', TextType::class, [ + $form->add('refreshToken', TextType::class, [ 'attr' => ['class' => 'input-yt-refreshtoken', 'readonly' => 'readonly'], ]); } - $formMapper->end(); + $form->end(); } /** @@ -149,9 +155,9 @@ protected function configureFormFields(FormMapper $formMapper): void * * @throws \RuntimeException */ - protected function configureDatagridFilters(DatagridMapper $datagridMapper): void + protected function configureDatagridFilters(DatagridMapper $filter): void { - $datagridMapper + $filter ->add('channelName') ->add('isHealthy'); } @@ -161,9 +167,9 @@ protected function configureDatagridFilters(DatagridMapper $datagridMapper): voi * * @throws \RuntimeException */ - protected function configureListFields(ListMapper $listMapper): void + protected function configureListFields(ListMapper $list): void { - $listMapper + $list ->add('channelName') ->add('isHealthy') ->add('_action', 'actions', [ diff --git a/Admin/InputAdmin.php b/Admin/InputAdmin.php index c0a6af8..01096fe 100644 --- a/Admin/InputAdmin.php +++ b/Admin/InputAdmin.php @@ -37,27 +37,27 @@ public function __construct(string $code, string $class, string $baseControllerN * {@inheritdoc} * @throws \RuntimeException */ - protected function configureFormFields(FormMapper $formMapper): void + protected function configureFormFields(FormMapper $form): void { $subject = $this->getSubject(); - $formMapper + $form ->tab('General') ->with('General'); if ($subject instanceof MediaFile) { - $formMapper->add('fileLocation', TextType::class, ['label' => 'File location on server']); + $form->add('fileLocation', TextType::class, ['label' => 'File location on server']); } if ($subject instanceof MediaRtmp) { - $formMapper->add('rtmpAddress', TextType::class, ['label' => 'Address of the RTMP stream to repeat']); + $form->add('rtmpAddress', TextType::class, ['label' => 'Address of the RTMP stream to repeat']); } if ($subject instanceof MediaUrl) { - $formMapper->add('url', TextType::class, ['label' => 'URL to video file']); + $form->add('url', TextType::class, ['label' => 'URL to video file']); } - $formMapper->end() + $form->end() ->end(); } @@ -65,8 +65,8 @@ protected function configureFormFields(FormMapper $formMapper): void * {@inheritdoc} * @throws \RuntimeException */ - protected function configureListFields(ListMapper $listMapper): void + protected function configureListFields(ListMapper $list): void { - $listMapper->add('__toString', 'string', ['label' => 'Input']); + $list->add('__toString', 'string', ['label' => 'Input']); } } diff --git a/Admin/LiveBroadcastAdmin.php b/Admin/LiveBroadcastAdmin.php index 1e4e16a..1598642 100644 --- a/Admin/LiveBroadcastAdmin.php +++ b/Admin/LiveBroadcastAdmin.php @@ -19,6 +19,7 @@ use Sonata\AdminBundle\Form\Type\ModelType; use Sonata\DoctrineORMAdminBundle\Model\ModelManager; use Sonata\Form\Type\DateTimePickerType; +use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; use Symfony\Component\Form\Extension\Core\Type\CheckboxType; use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\Extension\Core\Type\FileType; @@ -33,7 +34,12 @@ class LiveBroadcastAdmin extends AbstractAdmin /** * @var string */ - protected $thumbnailPath = ''; + protected string $thumbnailPath = ''; + + /** + * @var array + */ + protected array $datagridValues = []; /** * {@inheritdoc} @@ -101,12 +107,12 @@ protected function getThumbnailHtml(): ?string /** * {@inheritdoc} * - * @throws \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException + * @throws InvalidArgumentException * @throws \RuntimeException */ - protected function configureFormFields(FormMapper $formMapper): void + protected function configureFormFields(FormMapper $form): void { - $formMapper + $form ->with('General', [ 'class' => 'col-md-8', ]) @@ -165,9 +171,9 @@ protected function configureFormFields(FormMapper $formMapper): void * * @throws \RuntimeException */ - protected function configureDatagridFilters(DatagridMapper $datagridMapper): void + protected function configureDatagridFilters(DatagridMapper $filter): void { - $datagridMapper + $filter ->add('name') ->add('startTimestamp') ->add('endTimestamp'); @@ -178,9 +184,9 @@ protected function configureDatagridFilters(DatagridMapper $datagridMapper): voi * * @throws \RuntimeException */ - protected function configureListFields(ListMapper $listMapper): void + protected function configureListFields(ListMapper $list): void { - $listMapper + $list ->addIdentifier('name') ->add('outputChannels', 'sonata_type_model', ['label' => 'Channel(s)']) ->add('startTimestamp', 'datetime', ['label' => 'Start time']) diff --git a/Controller/CRUDController.php b/Controller/CRUDController.php index 05e1301..e90e211 100644 --- a/Controller/CRUDController.php +++ b/Controller/CRUDController.php @@ -7,12 +7,20 @@ namespace Martin1982\LiveBroadcastSonataAdminBundle\Controller; use Facebook\Authentication\AccessToken; +use Martin1982\LiveBroadcastBundle\Exception\LiveBroadcastOutputException; use Martin1982\LiveBroadcastBundle\Service\ChannelApi\Client\GoogleClient; +use Psr\Container\ContainerExceptionInterface; +use Psr\Container\NotFoundExceptionInterface; +use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException; +use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; use Sonata\AdminBundle\Controller\CRUDController as Controller; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Session\SessionInterface; +use Symfony\Component\Routing\Exception\InvalidParameterException; +use Symfony\Component\Routing\Exception\MissingMandatoryParametersException; +use Symfony\Component\Routing\Exception\RouteNotFoundException; /** * Class CRUDController @@ -26,11 +34,12 @@ class CRUDController extends Controller * * @return JsonResponse * - * @throws \Martin1982\LiveBroadcastBundle\Exception\LiveBroadcastOutputException + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface */ public function longLivedAccessTokenAction(Request $request): JsonResponse { - $facebookService = $this->get('live.broadcast.facebook_api.service'); + $facebookService = $this->container->get('live.broadcast.facebook_api.service'); $accessToken = $facebookService->getLongLivedAccessToken($request->get('userAccessToken')); $response = new JsonResponse(null, 500); @@ -47,23 +56,20 @@ public function longLivedAccessTokenAction(Request $request): JsonResponse * * @return RedirectResponse * - * @throws \Symfony\Component\Routing\Exception\RouteNotFoundException - * @throws \Symfony\Component\Routing\Exception\MissingMandatoryParametersException - * @throws \Symfony\Component\Routing\Exception\InvalidParameterException - * @throws \Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException - * @throws \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException - * @throws \Martin1982\LiveBroadcastBundle\Exception\LiveBroadcastOutputException + * @throws ContainerExceptionInterface + * @throws LiveBroadcastOutputException + * @throws NotFoundExceptionInterface */ public function youTubeOAuthAction(Request $request): RedirectResponse { $session = $request->getSession(); - if ($session && $request->get('cleartoken')) { + if ($request->get('cleartoken')) { $this->clearToken($session); } $requestCode = $request->get('code'); - if ($requestCode && $session) { + if ($requestCode) { $this->checkRequestCode($request, $session); } @@ -73,9 +79,9 @@ public function youTubeOAuthAction(Request $request): RedirectResponse /** * @param SessionInterface $session * - * @throws \Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException - * @throws \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException - * @throws \Martin1982\LiveBroadcastBundle\Exception\LiveBroadcastOutputException + * @throws ContainerExceptionInterface + * @throws LiveBroadcastOutputException + * @throws NotFoundExceptionInterface */ protected function clearToken(SessionInterface $session): void { @@ -89,9 +95,9 @@ protected function clearToken(SessionInterface $session): void * @param Request $request * @param SessionInterface $session * - * @throws \Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException - * @throws \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException - * @throws \Martin1982\LiveBroadcastBundle\Exception\LiveBroadcastOutputException + * @throws ContainerExceptionInterface + * @throws LiveBroadcastOutputException + * @throws NotFoundExceptionInterface */ protected function checkRequestCode(Request $request, SessionInterface $session): void { @@ -130,9 +136,9 @@ protected function checkRequestCode(Request $request, SessionInterface $session) /** * @return \Google_Client * - * @throws \Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException - * @throws \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException - * @throws \Martin1982\LiveBroadcastBundle\Exception\LiveBroadcastOutputException + * @throws ContainerExceptionInterface + * @throws LiveBroadcastOutputException + * @throws NotFoundExceptionInterface */ private function getGoogleClient(): \Google_Client { diff --git a/DependencyInjection/LiveBroadcastSonataAdminExtension.php b/DependencyInjection/LiveBroadcastSonataAdminExtension.php index 09e16e7..816fd43 100644 --- a/DependencyInjection/LiveBroadcastSonataAdminExtension.php +++ b/DependencyInjection/LiveBroadcastSonataAdminExtension.php @@ -18,6 +18,8 @@ class LiveBroadcastSonataAdminExtension extends Extension { /** * @inheritDoc + * + * @throws \Exception */ public function load(array $configs, ContainerBuilder $container): void { diff --git a/README.md b/README.md index cb9e3b0..24dd7c3 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ The Live Broadcast Bundle will make it possible to plan live video streams to various channels like Twitch, YouTube Live, Facebook Live (referred to as Output or Channels). -As "Input" we support files, URL's or existing RTMP streams. +As "Input" we support files, URLs or existing RTMP streams. For more info you can view the latest recorded presentation below, check the demo project at https://github.com/Martin1982/live-broadcast-demo or read on; @@ -120,7 +120,7 @@ Edit your `app/config/config.yml` with the following configuration: application_id: YourFacebookAppId application_secret: YourFacebookAppSecret -When using Sonata Admin; add the Sonata block to your blocks config: +When using Sonata Admin; add the Sonata block to your `blocks` config: sonata_block: blocks: @@ -143,7 +143,7 @@ Add the YouTube API info to your config.yml: redirect_route: admin_martin1982_livebroadcast_channel_abstractchannel_youtubeoauth -When using Sonata Admin; add the Sonata block to your blocks config: +When using Sonata Admin; add the Sonata block to your `blocks` config: sonata_block: blocks: diff --git a/Resources/views/Block/facebook_auth.html.twig b/Resources/views/Block/facebook_auth.html.twig index 486c3da..91db94e 100644 --- a/Resources/views/Block/facebook_auth.html.twig +++ b/Resources/views/Block/facebook_auth.html.twig @@ -97,7 +97,7 @@ option.text('Personal Facebook account: ' + meResponse.name); option.attr('value', meResponse.id); - /* Get a long lived access token for the user access token */ + /* Get a long-lived access token for the user access token */ $.ajax({ url: '{{ admin.generateUrl('longLivedAccessToken') }}', data: { @@ -109,7 +109,7 @@ FB.broadcast = { accessToken: data.accessToken }; - /* Retrieve page access tokens with the long lived user access token */ + /* Retrieve page access tokens with the long-lived user access token */ FB.api('/me/accounts', {access_token: data.accessToken }, makeAccountSelectable); FB.api('/me/groups', makeGroupsSelectable); FB.api('/me/events', makeEventsSelectable); diff --git a/Tests/LiveBroadcastSonataAdminBundleTest.php b/Tests/LiveBroadcastSonataAdminBundleTest.php index 67a8d7c..fbc3433 100644 --- a/Tests/LiveBroadcastSonataAdminBundleTest.php +++ b/Tests/LiveBroadcastSonataAdminBundleTest.php @@ -23,8 +23,7 @@ public function testBuild(): void { $container = $this->createMock(ContainerBuilder::class); $container->expects(self::atLeastOnce()) - ->method('addCompilerPass') - ->willReturn(true); + ->method('addCompilerPass'); $bundle = new LiveBroadcastSonataAdminBundle();