4
\$\begingroup\$

My app interacts with Google Calendar. This Service wraps Google_Service_Calendar:

<?php
declare(strict_types=1);

namespace App\Service;

use Symfony\Component\HttpFoundation\Session\SessionInterface;

class GoogleCalendarService extends \Google_Service_Calendar
{
    public const CALENDAR_ID = '[email protected]';

    public function __construct(SessionInterface $session) {
        $client = new \Google_Client();
        $client->setAccessToken($session->get('google_access_token'));
        parent::__construct($client);
    }
}

I have the feeling that having the session as a dependency is not good practice. For a user of that service, it is absolutely not clear that the access token needs to be set in the session.

I could probably add some Exceptions for the case that it is not set, but I wonder whether there is a better way?

\$\endgroup\$
1
  • \$\begingroup\$ should $session be an optional parameter? if not, then under what conditions would an exception be warranted? \$\endgroup\$ Jan 21, 2021 at 0:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.