1 | <?php |
||
13 | class ConsoleObserver implements SiteCheckObserver |
||
14 | { |
||
15 | |||
16 | /** |
||
17 | * @var LoggerInterface |
||
18 | */ |
||
19 | protected $logger; |
||
20 | |||
21 | /** |
||
22 | * @var Config |
||
23 | */ |
||
24 | protected $config; |
||
25 | |||
26 | /** |
||
27 | * @param Config $config |
||
28 | */ |
||
29 | public function setConfig($config) |
||
33 | |||
34 | /** |
||
35 | * SiteChecker constructor. |
||
36 | * @param \Psr\Log\LoggerInterface|null $logger |
||
37 | */ |
||
38 | public function __construct(LoggerInterface $logger = null) |
||
42 | |||
43 | /** |
||
44 | * No additional checks here for now. |
||
45 | * |
||
46 | * @param \SiteChecker\Asset $url |
||
47 | * @return bool |
||
48 | */ |
||
49 | public function pageToCheck(Asset $url) |
||
53 | |||
54 | |||
55 | /** |
||
56 | * Log page to console. |
||
57 | * |
||
58 | * @param \SiteChecker\Asset $asset |
||
59 | * @param $response |
||
60 | * @return mixed |
||
61 | */ |
||
62 | public function pageChecked( |
||
68 | |||
69 | |||
70 | /** |
||
71 | * Show results. |
||
72 | * |
||
73 | * @param Asset[] $assets |
||
74 | */ |
||
75 | public function receiveResults(array $assets) |
||
76 | { |
||
77 | $this->showResults($assets); |
||
78 | if (!empty($this->config->reportEmail)) { |
||
79 | $this->sendEmailResults($assets); |
||
80 | } |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Show results. |
||
85 | * |
||
86 | * @param Asset[] $assets |
||
87 | */ |
||
88 | protected function sendEmailResults($assets) |
||
89 | { |
||
90 | $countFailed = 0; |
||
91 | $messages = []; |
||
92 | $assets = array_filter( |
||
93 | $assets, |
||
94 | function(Asset $asset) { |
||
95 | return $asset->isError(); |
||
96 | } |
||
97 | ); |
||
98 | /** @var Asset $asset */ |
||
99 | foreach ($assets as $asset) { |
||
100 | $countFailed++; |
||
101 | $message = ' * '. $asset->getURL(); |
||
102 | if ($asset->getParentPage() instanceof Asset) { |
||
103 | $message .= ' on ' . $asset->getParentPage()->getURL(); |
||
104 | } |
||
105 | $messages[] = $message; |
||
106 | |||
107 | } |
||
108 | if (empty($messages)) { |
||
109 | return; |
||
110 | } |
||
111 | |||
112 | $mail = new \PHPMailer(); |
||
113 | $mailFrom = $this->config->getMailFrom(); |
||
114 | $mail->setFrom($mailFrom, 'Site Checker'); |
||
115 | $addresses = $this->config->getReportEmailAddresses(); |
||
116 | foreach ($addresses as $emailAddress) { |
||
117 | $mail->addAddress($emailAddress); |
||
118 | } |
||
119 | |||
120 | $mail->Subject = 'SiteChecker report'; |
||
121 | $mail->Body = "Hi, here are some broken links on your website:\n\n"; |
||
122 | $mail->Body .= implode(PHP_EOL, $messages); |
||
123 | |||
124 | if (!$mail->send()) { |
||
125 | $this->logger->error('Message could not be sent.'); |
||
126 | $this->logger->error('Mailer Error: ' . $mail->ErrorInfo); |
||
127 | } else { |
||
128 | $this->logger->info('Message has been sent'); |
||
129 | } |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * Called when the checker has checked the given page. |
||
134 | * |
||
135 | * @param Asset $asset |
||
136 | */ |
||
137 | public function logResult($asset) |
||
159 | |||
160 | /** |
||
161 | * Called when the check was ended. |
||
162 | * |
||
163 | * @param Asset[] $assets |
||
164 | */ |
||
165 | public function showResults(array $assets) |
||
185 | } |
||
186 |