It looks like getMetadataCookie()
does not always return an array, possibly because the cookie has not yet been set. You may want to check that it's actually an array before using it as such;
if (is_array($metadata) && array_key_exists('base_domain', $metadata) &&
For the added code, the same would apply to array_key_exists()
in the new code. If you're unsure if it's actually set to an array if the cookie is not set, check first.