Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
digitalascetic/google-api-client / DependencyInjection / DigitalAsceticGoogleApiClientExtension.php
Size: Mime:
<?php

namespace DigitalAscetic\GoogleApiClientBundle\DependencyInjection;

use DigitalAscetic\GoogleApiClientBundle\EventListener\GoogleApiNotificationsChannelMappingListener;
use DigitalAscetic\GoogleApiClientBundle\EventListener\GoogleApiNotificationsListener;
use DigitalAscetic\GoogleApiClientBundle\EventListener\GoogleApiUserEventListener;
use DigitalAscetic\GoogleApiClientBundle\Service\GoogleApiService;
use DigitalAscetic\GoogleApiClientBundle\Service\GoogleAuthService;
use DigitalAscetic\GoogleApiClientBundle\Service\GoogleCalendarService;
use DigitalAscetic\GoogleApiClientBundle\Service\GoogleMailService;
use DigitalAscetic\GoogleApiClientBundle\Service\GoogleNotificationsService;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class DigitalAsceticGoogleApiClientExtension extends Extension implements PrependExtensionInterface
{

    public function load(array $configs, ContainerBuilder $container)
    {
        $loader = new XmlFileLoader(
            $container,
            new FileLocator(__DIR__ . '/../../config')
        );
        $loader->load('services.xml');

        $config = $this->processConfiguration(new Configuration(), $configs);

        $configCleanDisconnectGoogleApiServices = array();

        $configWatchingGoogleApiServices = array();

        if ($config['enabled']) {

            $googleApi = new Definition(GoogleApiService::class);
            $googleApi->addArgument($config['auth']);
            $container->setDefinition(GoogleApiService::SERVICE_NAME, $googleApi);
            $container->setAlias(GoogleApiService::class, GoogleApiService::SERVICE_NAME);

            $googleAuth = new Definition(GoogleAuthService::class);
            $googleAuth->addArgument(new Reference('doctrine.orm.entity_manager'));
            $googleAuth->addArgument(new Reference('logger'));
            $googleAuth->addArgument($config['auth']);
            $container->setDefinition(GoogleAuthService::SERVICE_NAME, $googleAuth);
            $container->setAlias(GoogleAuthService::class, GoogleAuthService::SERVICE_NAME);

            $googleNotifications = new Definition(GoogleNotificationsService::class);
            $googleNotifications->addArgument(new Reference('service_container'));
            $googleNotifications->addArgument(new Reference('doctrine.orm.entity_manager'));
            $googleNotifications->addArgument(new Reference('router.default'));
            $googleNotifications->addArgument($config['auth']['protocol']);
            $container->setDefinition(GoogleNotificationsService::SERVICE_NAME, $googleNotifications);
            $container->setAlias(GoogleNotificationsService::class, GoogleNotificationsService::SERVICE_NAME);

            $calendarEnabled = false;
            if (isset($config['calendar']) && $config['calendar']['enabled']) {
                $calendarEnabled = true;

                $googleCalendar = new Definition(GoogleCalendarService::class);
                $googleCalendar->addArgument(new Reference('service_container'));
                $googleCalendar->addArgument(new Reference('doctrine.orm.entity_manager'));
                $googleCalendar->addArgument(new Reference(GoogleAuthService::SERVICE_NAME));
                $googleCalendar->addArgument(new Reference(GoogleNotificationsService::SERVICE_NAME));
                $googleCalendar->addArgument(new Reference('logger'));

                if ($config['calendar']['clean_disconnect'] || $config['calendar']['watch']) {
                    if (!$config['calendar']['event_service']) {
                        throw new InvalidConfigurationException('If clean_disconnect or watch is active, event_service must be required');
                    }

                    $googleCalendar->addArgument(new Reference($config['calendar']['event_service']));
                }

                $container->setDefinition(GoogleCalendarService::SERVICE_NAME, $googleCalendar);
                $container->setAlias(GoogleCalendarService::class, GoogleCalendarService::SERVICE_NAME);

                $calendarWatchEnabled = false;

                if ($config['calendar']['watch']) {
                    $calendarWatchEnabled = true;
                }

                array_push($configWatchingGoogleApiServices, new Reference(GoogleCalendarService::SERVICE_NAME));

                $container->setParameter(GoogleCalendarService::SERVICE_WATCH_ENABLED_PROPERTY, $calendarWatchEnabled);

                if ($config['calendar']['clean_disconnect']) {
                    array_push($configCleanDisconnectGoogleApiServices, new Reference(GoogleCalendarService::SERVICE_NAME));
                }
            }

            $container->setParameter(GoogleCalendarService::SERVICE_ENABLED_PROPERTY, $calendarEnabled);

            $googleApiUserListener = new Definition(GoogleApiUserEventListener::class);
            $googleApiUserListener->addArgument($configCleanDisconnectGoogleApiServices);
            $googleApiUserListener->addTag('kernel.event_subscriber');
            $container->setDefinition(GoogleApiUserEventListener::SERVICE_NAME, $googleApiUserListener);

            $googleNotificationsChannelMapping = new Definition(GoogleApiNotificationsChannelMappingListener::class);
            $googleNotificationsChannelMapping->addArgument($config['auth']['user']);
            $googleNotificationsChannelMapping->addTag('doctrine.event_subscriber');
            $container->setDefinition(GoogleApiNotificationsChannelMappingListener::SERVICE_NAME, $googleNotificationsChannelMapping);

            $googleNotificationsListener = new Definition(GoogleApiNotificationsListener::class);
            $googleNotificationsListener->addTag('kernel.event_subscriber');
            $googleNotificationsListener->addArgument(new Reference(GoogleNotificationsService::SERVICE_NAME));
            $googleNotificationsListener->addArgument($configWatchingGoogleApiServices);
            $googleNotificationsListener->addArgument(new Reference('doctrine.orm.entity_manager'));
            $container->setDefinition(GoogleApiNotificationsListener::SERVICE_NAME, $googleNotificationsListener);

            if (isset($config['gmail']) && $config['gmail']['enabled']) {
                $googleMail = new Definition(GoogleMailService::class);
                $googleMail->addArgument(new Reference('service_container'));
                $googleMail->addArgument(new Reference('doctrine.orm.entity_manager'));
                $googleMail->addArgument(new Reference(GoogleAuthService::SERVICE_NAME));
                $googleMail->addArgument(new Reference('logger'));

                $container->setDefinition(GoogleMailService::SERVICE_NAME, $googleMail);
                $container->setAlias(GoogleMailService::class, GoogleMailService::SERVICE_NAME);
            }
        }
    }

    /**
     * Allow an extension to prepend the extension configurations.
     *
     * @param ContainerBuilder $container
     */
    public function prepend(ContainerBuilder $container)
    {

    }
}