public function invoke(EventInterface $event) {
        $request = $event->getRequest();
        $resource = (string) $request->getRoute();
        $method = $request->getMethod();
        $allowed = $this->params['allowedMethods'];

        if (!isset($allowed[$resource]) || !in_array($method, $allowed[$resource])) {
            // The listener is not configured for the current method/resource combination
            return;
        }

        $origin = $request->headers->get('Origin');

        // Vary on Origin to prevent caching allowed/disallowed requests
        $event->getResponse()->setVary('Origin', false);

        // Fall back if the passed origin is not allowed
        if (!$origin || !$this->originIsAllowed($origin)) {
            return;
        }

        // Flag this as an allowed request
        $this->requestAllowed = true;

        $event->getResponse()->headers->add([
            'Access-Control-Allow-Origin' => $origin,
        ]);
    }