protected function getPreferredSite(array $sites, Request $request)
    {
        if (0 === \count($sites)) {
            return;
        }

        $sitesLocales = array_map(static function (SiteInterface $site) {
            return $site->getLocale();
        }, $sites);

        $language = $request->getPreferredLanguage($sitesLocales);
        $host = $request->getHost();

        foreach ($sites as $site) {
            if (\in_array($site->getHost(), ['localhost', $host], true) && $language === $site->getLocale()) {
                return $site;
            }
        }

        return reset($sites);
    }