<commit_before><?php
/**
 * This file is part of the Juvem package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace AppBundle\Manager\Calendar;

use AppBundle\Entity\Event;
use AppBundle\Entity\EventRepository;
use AppBundle\Manager\Calendar\CalDav\CalDavConnector;
use AppBundle\Manager\Calendar\CalDav\CalDavVCalendarObjectFactory;
use Psr\Log\LoggerInterface;

class CalendarManager
{

    /**
     * @var CalDavConnector
     */
    private ?CalDavConnector $connector;

    /**
     * @var CalDavVCalendarObjectFactory
     */
    private CalDavVCalendarObjectFactory $calDavObjectFactory;

    /**
     * @var EventRepository
     */
    private EventRepository $eventRepository;

    /**
     * @var LoggerInterface
     */
    private LoggerInterface $logger;

    /**
     * @param CalDavConnector|null         $connector
     * @param CalDavVCalendarObjectFactory $calDavCreator
     * @param EventRepository              $eventRepository
     * @param LoggerInterface              $logger
     */
    public function __construct(
        ?CalDavConnector             $connector,
        CalDavVCalendarObjectFactory $calDavCreator,
        EventRepository              $eventRepository,
        LoggerInterface              $logger
    ) {
        $this->connector           = $connector;
        $this->eventRepository     = $eventRepository;
        $this->calDavObjectFactory = $calDavCreator;
        $this->logger              = $logger;
    }

    /**
     * Synchronize calendar
     *
     * @return void
     */
    public function sync(): void
    {
        if (!$this->connector) {
            return;
        }
        $start = microtime(true);

        $calendarEvents = $this->connector->fetchCalendarObjects();
        $juvemEvents    = $this->eventRepository->findAll();
        foreach ($juvemEvents as $juvemEvent) {
            if (!$juvemEvent->isDeleted() && $juvemEvent->isCalendarEntryEnabled()) {
                $this->updateEventCalendarEntry($juvemEvent);
            } else {
                $name = self::createJuvemEventName($juvemEvent);
                foreach ($calendarEvents as $calendarEvent) {
                    if (strpos($calendarEvent->getHref(), $name . '.ics') !== false) {
                        $this->connector->removeCalendarObject($name);
                    }
                }
            }
        }

        $duration = round((microtime(true) - $start) * 1000);
        $this->logger->notice(
            'Synchronized calendar within {duration} ms', ['duration' => $duration]
        );
    }

    /**
     * Delete calendar object
     *
     * @param string $name
     * @return void
     */
    public function removeCalendarObject(Event $juvemEvent)
    {
        $name = self::createJuvemEventName($juvemEvent);
        $this->connector->removeCalendarObject($name);
    }

    /**
     * Create or update a calendar entry for transmitted event
     *
     * @param Event $juvemEvent
     * @return void
     */
    public function updateEventCalendarEntry(Event $juvemEvent)
    {
        if ($juvemEvent->getId() === null) {
            return;
        }
        $calendar = $this->calDavObjectFactory->createForJuvemEvent($juvemEvent);<commit_msg>No longer trying to update calendar objects if no connector configured<commit_after><?php
/**
 * This file is part of the Juvem package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace AppBundle\Manager\Calendar;

use AppBundle\Entity\Event;
use AppBundle\Entity\EventRepository;
use AppBundle\Manager\Calendar\CalDav\CalDavConnector;
use AppBundle\Manager\Calendar\CalDav\CalDavVCalendarObjectFactory;
use Psr\Log\LoggerInterface;

class CalendarManager
{

    /**
     * @var CalDavConnector
     */
    private ?CalDavConnector $connector;

    /**
     * @var CalDavVCalendarObjectFactory
     */
    private CalDavVCalendarObjectFactory $calDavObjectFactory;

    /**
     * @var EventRepository
     */
    private EventRepository $eventRepository;

    /**
     * @var LoggerInterface
     */
    private LoggerInterface $logger;

    /**
     * @param CalDavConnector|null         $connector
     * @param CalDavVCalendarObjectFactory $calDavCreator
     * @param EventRepository              $eventRepository
     * @param LoggerInterface              $logger
     */
    public function __construct(
        ?CalDavConnector             $connector,
        CalDavVCalendarObjectFactory $calDavCreator,
        EventRepository              $eventRepository,
        LoggerInterface              $logger
    ) {
        $this->connector           = $connector;
        $this->eventRepository     = $eventRepository;
        $this->calDavObjectFactory = $calDavCreator;
        $this->logger              = $logger;
    }

    /**
     * Synchronize calendar
     *
     * @return void
     */
    public function sync(): void
    {
        if (!$this->connector) {
            return;
        }
        $start = microtime(true);

        $calendarEvents = $this->connector->fetchCalendarObjects();
        $juvemEvents    = $this->eventRepository->findAll();
        foreach ($juvemEvents as $juvemEvent) {
            if (!$juvemEvent->isDeleted() && $juvemEvent->isCalendarEntryEnabled()) {
                $this->updateEventCalendarEntry($juvemEvent);
            } else {
                $name = self::createJuvemEventName($juvemEvent);
                foreach ($calendarEvents as $calendarEvent) {
                    if (strpos($calendarEvent->getHref(), $name . '.ics') !== false) {
                        $this->connector->removeCalendarObject($name);
                    }
                }
            }
        }

        $duration = round((microtime(true) - $start) * 1000);
        $this->logger->notice(
            'Synchronized calendar within {duration} ms', ['duration' => $duration]
        );
    }

    /**
     * Delete calendar object
     *
     * @param string $name
     * @return void
     */
    public function removeCalendarObject(Event $juvemEvent)
    {
        if (!$this->connector) {
            return;
        }
        $name = self::createJuvemEventName($juvemEvent);
        $this->connector->removeCalendarObject($name);
    }

    /**
     * Create or update a calendar entry for transmitted event
     *
     * @param Event $juvemEvent
     * @return void
     */
    public function updateEventCalendarEntry(Event $juvemEvent)
    {
        if ($juvemEvent->getId() === null || !$this->connector) {
            return;
        }
        $calendar = $this->calDavObjectFactory->createForJuvemEvent($juvemEvent);
{'fraction_non_alphanumeric': 0.110525545308154, 'fraction_numerical': 0.0011711316059142146, 'mean_word_length': 2.142594296228151, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6739227', 'n_tokens_mistral': 1880, 'n_tokens_neox': 1815, 'n_words': 383}