Skip to content

Conversation

@infostreams
Copy link
Contributor

This pull request add support to change which requests are issued. It does this by allowing you to inject a different implementation of the (new) ICalDAVRequestFactory into the CalDAVRequestFactory, thereby adding support to change what goes into a specific request.

It also adds support to retrieve the full list of found properties of a request.

My use case is that I added a custom property to my calendars, and I want to request that property. However, I either do not understand the Facades or I might have missed how I can change the outgoing requests. Every class is final (which is a good thing), but there are no ways do to dependency injection either.

With this implementation I can now do the following:

    CalDAVRequestFactory::setInstance(new Calendar\CalDAVRequestFactory());

    $this->client = new CalDavClient(....)

and then in Calendar\CalDAVRequestFactory:

namespace App\Helpers\Calendar;

use CalDAVClient\Facade\Requests\IAbstractWebDAVRequest,
    CalDAVClient\Facade\Requests\ICalDAVRequestFactory,
    CalDAVClient\Facade\Requests\UserPrincipalRequest,
    CalDAVClient\Facade\Requests\CalendarHomeRequest,
    CalDAVClient\Facade\Requests\GetCalendarRequest,
    CalDAVClient\Facade\Requests\CalendarSyncRequest,
    CalDAVClient\Facade\Requests\CalendarMultiGetRequest,
    CalDAVClient\Facade\Requests\CalendarQueryRequest,
    CalDAVClient\Facade\Requests\CalendarCreateRequest,
    CalDAVClient\Facade\Requests\EventCreateRequest,
    CalDAVClient\Facade\Requests\EventUpdateRequest,

    // redefine the GetCalendarsRequest
    App\Helpers\Calendar\Requests\GetCalendarsRequest;


class CalDAVRequestFactory implements ICalDAVRequestFactory {

    /**
     * Builds a request of a certain type.
     *
     * @param string $type
     * @param array $params
     * @return IAbstractWebDAVRequest|null
     * @throws \InvalidArgumentException
     */
    public function build($type, $params = [])
    {
          // omitted for brevity, basically literally copy-pasted from the default CalDAVRequestFactory
    }
}

This would then allow me to define my custom request in App\Helpers\Calendar\Requests\GetCalendarsRequest, and I can access my custom property as follows:

/**
 * @var $calendars \CalDAVClient\Facade\Responses\GetCalendarsResponse
 */
$calendars = $this->client->getCalendars($calendar_home_url);

/**
 * @var $responses[] \CalDAVClient\Facade\Responses\GetCalendarMultiResponse
 */
$responses = $calendars->getResponses();

/**
 * @var $multiresponse \CalDAVClient\Facade\Responses\GetCalendarMultiResponse
 */
foreach ($responses as $multiresponse) {

    /**
     * @var $multi[] \CalDAVClient\Facade\Responses\GetCalendarResponse
     */
    $multi = $multiresponse->getResponses();

    /**
     * @var $r \CalDAVClient\Facade\Responses\GetCalendarResponse
     */
    foreach ($multi as $r) {
        $props = $r->getFoundProps();

        if (!empty($r->getDisplayName())) {

            $list[] = [
                'title' => $r->getDisplayName(),
                'link' => $r->getHRef(),
                'color' => $r->getColor(),
                'ctag' => $r->getCTag(),
                'MY_CUSTOM_PROPERTY => $props['MY_CUSTOM_PROPERTY'] 
            ];
        }
    }
}

I've toyed with the idea of having a DefaultCalDAVRequestFactory or an AbstractCalDAVRequestFactory so that I did not have to copy-paste the 'build' method and could just do parent::build()or $this->buildDefault() or something, but they were all messy and I didn't find a clean / satisfactory solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant