예제 #1
0
 /**
  * @param Asset $asset
  */
 protected function checkAsset(Asset $asset)
 {
     if (!$this->shouldBeChecked($asset) || !$this->observer->pageToCheck($asset)) {
         return;
     }
     $cookies = $this->config->getCookies();
     foreach ($cookies as $key => $cookie) {
         $cookie['Domain'] = $this->basePage->host;
         $cookies[$key] = new SetCookie($cookie);
     }
     $jar = new CookieJar(false, $cookies);
     try {
         $response = $this->client->request('GET', $asset->getURL(), ['cookies' => $jar]);
     } catch (RequestException $exception) {
         $response = $exception->getResponse();
         $asset->setResponseCode(Asset::CODE_ERROR);
     }
     if ($response) {
         $asset->setResponseCode($response->getStatusCode());
     }
     $this->observer->pageChecked($asset, $response);
     $this->checkedAssets[] = $asset;
     if (!$response) {
         return;
     }
     if (!$this->isExternal($asset) && $this->isHtmlPage($response)) {
         $this->checkAllAssets($response->getBody()->getContents(), $asset);
     }
 }
 /**
  * Show results.
  *
  * @param Asset[] $assets
  */
 protected function sendEmailResults($assets)
 {
     if (empty($this->config->reportEmail)) {
         return;
     }
     $countFailed = 0;
     $messages = [];
     $assets = array_filter($assets, function (Asset $asset) {
         return $asset->isError();
     });
     /** @var Asset $asset */
     foreach ($assets as $asset) {
         $countFailed++;
         $message = ' * ' . $asset->getURL();
         if ($asset->getParentPage() instanceof Asset) {
             $message .= ' on ' . $asset->getParentPage()->getURL();
         }
         $messages[] = $message;
     }
     if (empty($messages)) {
         return;
     }
     $mail = new \PHPMailer();
     $mailFrom = $this->config->getMailFrom();
     $mail->setFrom($mailFrom, 'Site Checker');
     $addresses = $this->config->getReportEmailAddresses();
     foreach ($addresses as $emailAddress) {
         $mail->addAddress($emailAddress);
     }
     $mail->Subject = 'SiteChecker report';
     $mail->Body = "Hi, here are some broken links on your website:\n\n";
     $mail->Body .= implode(PHP_EOL, $messages);
     if (!$mail->send()) {
         $this->logger->error('Message could not be sent.');
         $this->logger->error('Mailer Error: ' . $mail->ErrorInfo);
     } else {
         $this->logger->info('Message has been sent');
     }
 }