/**
  * @param string $url
  *   The URL to POST to
  * @param SiteDocument $site
  *   The site being posted to
  * @param array $params
  *   An array of keys and values to be posted
  *
  * @return mixed
  *   The content of the response
  *
  * @throws WardenRequestException
  *   If any error occurs
  */
 public function post($url, SiteDocument $site, array $params = array())
 {
     try {
         $this->setClientTimeout($this->connectionTimeout);
         // Don't verify SSL certificate.
         // @TODO make this optional
         $this->buzz->getClient()->setVerifyPeer(FALSE);
         if ($site->getAuthUser() && $site->getAuthPass()) {
             $headers = array(sprintf('Authorization: Basic %s', base64_encode($site->getAuthUser() . ':' . $site->getAuthPass())));
             $this->setConnectionHeaders($headers);
         }
         $params['token'] = $this->sslEncryptionService->generateRequestToken();
         $content = http_build_query($params);
         /** @var \Buzz\Message\Response $response */
         $response = $this->buzz->post($url, $this->connectionHeaders, $content);
         if (!$response->isSuccessful()) {
             $this->logger->addError("Unable to request data from {$url}\nStatus code: " . $response->getStatusCode() . "\nHeaders: " . print_r($response->getHeaders(), TRUE));
             throw new WardenRequestException("Unable to request data from {$url}. Check log for details.");
         }
         $site->setLastSuccessfulRequest();
         $this->siteManager->updateDocument();
         return $response->getContent();
     } catch (ClientException $clientException) {
         throw new WardenRequestException($clientException->getMessage());
     }
 }
 /**
  * Update the Drupal Core and Modules with the latest versions.
  *
  * @param bool $updateNewSitesOnly
  *   Only update modules on sites marked as new.
  */
 public function updateAllDrupalModules($updateNewSitesOnly = FALSE)
 {
     $this->logger->addInfo('*** Starting Drupal Update Request Service ***');
     $drupalAllModuleVersions = array();
     $moduleLatestVersion = array();
     $majorVersions = $this->siteManager->getAllMajorVersionReleases();
     foreach ($majorVersions as $version) {
         $modules = $this->drupalModuleManager->getAllByVersion($version);
         /** @var ModuleDocument $module */
         foreach ($modules as $module) {
             $this->logger->addInfo('Updating - ' . $module->getProjectName() . ' for version: ' . $version);
             try {
                 $this->processDrupalUpdateData($module->getProjectName(), $version);
             } catch (\Exception $e) {
                 $this->logger->addWarning(' - Unable to update module version [' . $version . ']: ' . $e->getMessage());
                 continue;
             }
             $drupalAllModuleVersions[$version][$module->getProjectName()] = $drupalModuleVersions = $this->moduleVersions;
             $moduleVersions = array();
             // Get the recommended module version.
             if (isset($drupalModuleVersions[ModuleDocument::MODULE_VERSION_TYPE_RECOMMENDED])) {
                 $moduleRecommendedLatestVersion = $drupalModuleVersions[ModuleDocument::MODULE_VERSION_TYPE_RECOMMENDED][0];
                 $moduleVersions[ModuleDocument::MODULE_VERSION_TYPE_RECOMMENDED] = $moduleRecommendedLatestVersion;
                 $moduleLatestVersion[$version][$module->getProjectName()][ModuleDocument::MODULE_VERSION_TYPE_RECOMMENDED] = $moduleRecommendedLatestVersion;
             }
             // Get the other module version.
             if (isset($drupalModuleVersions[ModuleDocument::MODULE_VERSION_TYPE_OTHER])) {
                 $moduleOtherLatestVersion = $drupalModuleVersions[ModuleDocument::MODULE_VERSION_TYPE_OTHER][0];
                 $moduleVersions[ModuleDocument::MODULE_VERSION_TYPE_OTHER] = $moduleOtherLatestVersion;
                 $moduleLatestVersion[$version][$module->getProjectName()][ModuleDocument::MODULE_VERSION_TYPE_OTHER] = $moduleOtherLatestVersion;
             }
             $module->setName($this->getModuleName());
             $module->setIsNew(FALSE);
             $module->setLatestVersion($version, $moduleVersions);
             $module->setProjectStatus($this->projectStatus);
             $this->drupalModuleManager->updateDocument();
         }
     }
     foreach ($majorVersions as $version) {
         // Update the core after the modules to update the versions of the modules
         // for a site.
         $this->logger->addInfo('Updating - Drupal version: ' . $version);
         try {
             $this->processDrupalUpdateData('drupal', $version);
         } catch (\Exception $e) {
             $this->logger->addWarning(' - Unable to update drupal version [' . $version . ']: ' . $e->getMessage());
             continue;
         }
         $newOnly = $updateNewSitesOnly ? array('isNew' => TRUE) : array();
         $sites = $this->siteManager->getDocumentsBy(array_merge(array('coreVersion.release' => $version), $newOnly));
         // Update the sites for the major version with the latest core & module version information.
         $coreVersions = $this->moduleVersions[ModuleDocument::MODULE_VERSION_TYPE_RECOMMENDED];
         /** @var SiteDocument $site */
         foreach ($sites as $site) {
             $this->logger->addInfo('Updating site: ' . $site->getId() . ' - ' . $site->getUrl());
             if ($site->getCoreReleaseVersion() != $version) {
                 continue;
             }
             if (isset($moduleLatestVersion[$version])) {
                 $site->setModulesLatestVersion($moduleLatestVersion[$version]);
             }
             // Check for if the core version is out of date and requires a security update.
             $siteCurrentVersion = $site->getCoreVersion();
             $hasCriticalIssue = FALSE;
             $needsSecurityUpdate = FALSE;
             foreach ($coreVersions as $coreVersion) {
                 if ($coreVersion['version'] == $siteCurrentVersion) {
                     break;
                 }
                 if ($coreVersion['isSecurity']) {
                     $needsSecurityUpdate = TRUE;
                     $hasCriticalIssue = TRUE;
                 }
             }
             // Check all the site modules to see if any of them are out of date and need a security update.
             foreach ($site->getModules() as $siteModule) {
                 if (!isset($siteModule['latestVersion'])) {
                     continue;
                 }
                 if ($siteModule['version'] == $siteModule['latestVersion']) {
                     continue;
                 }
                 if (is_null($siteModule['version'])) {
                     continue;
                 }
                 // Check to see if this site's modules require a security update.
                 $siteModuleVersionInfo = ModuleDocument::getVersionInfo($siteModule['version']);
                 $versionType = NULL;
                 if (isset($drupalAllModuleVersions[$version][$siteModule['name']][ModuleDocument::MODULE_VERSION_TYPE_RECOMMENDED])) {
                     $drupalModuleRecommendedVersionInfo = ModuleDocument::getVersionInfo($drupalAllModuleVersions[$version][$siteModule['name']][ModuleDocument::MODULE_VERSION_TYPE_RECOMMENDED][0]['version']);
                     $versionType = $drupalModuleRecommendedVersionInfo['minor'] == $siteModuleVersionInfo['minor'] ? ModuleDocument::MODULE_VERSION_TYPE_RECOMMENDED : NULL;
                 }
                 if (isset($drupalAllModuleVersions[$version][$siteModule['name']][ModuleDocument::MODULE_VERSION_TYPE_OTHER]) && is_null($versionType)) {
                     $drupalModuleOtherVersionInfo = ModuleDocument::getVersionInfo($drupalAllModuleVersions[$version][$siteModule['name']][ModuleDocument::MODULE_VERSION_TYPE_OTHER][0]['version']);
                     $versionType = $drupalModuleOtherVersionInfo['minor'] == $siteModuleVersionInfo['minor'] ? ModuleDocument::MODULE_VERSION_TYPE_OTHER : NULL;
                 }
                 if (!is_null($versionType)) {
                     foreach ($drupalAllModuleVersions[$version][$siteModule['name']][$versionType] as $drupalModule) {
                         if ($drupalModule['version'] == $siteModule['version']) {
                             break;
                         }
                         // Check for site module being a dev version - then skip.
                         if (isset($siteModuleVersionInfo['extra']) && strstr($siteModuleVersionInfo['extra'], 'dev') !== FALSE) {
                             break;
                         }
                         if ($drupalModule['isSecurity']) {
                             unset($drupalModule['version']);
                             $site->updateModule($siteModule['name'], $drupalModule);
                             $siteModule['isSecurity'] = TRUE;
                         }
                     }
                 }
                 if ($siteModule['isSecurity']) {
                     $hasCriticalIssue = TRUE;
                 }
             }
             $site->setLatestCoreVersion($coreVersions[0]['version'], $needsSecurityUpdate);
             $site->setIsNew(FALSE);
             $site->setHasCriticalIssue($hasCriticalIssue);
             $this->siteManager->updateDocument();
         }
     }
     $this->logger->addInfo('*** FINISHED Drupal Update Request Service ***');
 }