public function update(Response $response)
    {
        $flatCookies = $response->headers->getCookies();
        $cookies = [];

        $getCookieId = function (Cookie $cookie) {
            return $cookie->getDomain() . '|' . $cookie->getPath() . '|' . $cookie->getName();
        };

        /** @var Cookie $cookie */
        foreach ($flatCookies as $cookie) {
            $cookies[$getCookieId($cookie)] = $cookie;
        }

        foreach ($this->cookieModels as $cookieModel) {
            $cookie = $cookieModel->toCookie();

            if ($cookieModel->getClear()) {
                $response->headers->clearCookie($cookie->getName(), $cookie->getPath(), $cookie->getDomain());
            } elseif (!isset($cookies[$getCookieId($cookie)]) && $cookieModel->isDirty()) {
                $response->headers->setCookie($cookieModel->toCookie());
            }
        }
    }