1 | <?php |
||
2 | |||
3 | namespace ProtoneMedia\ApiHealth\Checkers; |
||
4 | |||
5 | use Spatie\SslCertificate\Downloader; |
||
6 | use Spatie\SslCertificate\Exceptions\CouldNotDownloadCertificate; |
||
7 | use Spatie\SslCertificate\Exceptions\InvalidUrl; |
||
8 | |||
9 | abstract class AbstractSslCertificateChecker extends AbstractChecker |
||
10 | { |
||
11 | /** |
||
12 | * The Spatie SSL downloader. |
||
13 | * |
||
14 | * @var \Spatie\SslCertificate\Downloader $downloader |
||
15 | */ |
||
16 | protected $downloader; |
||
17 | |||
18 | /** |
||
19 | * The hostname that must be checked. |
||
20 | * |
||
21 | * @var string |
||
22 | */ |
||
23 | protected $hostname; |
||
24 | |||
25 | /** |
||
26 | * The port to request the certificate on. |
||
27 | * |
||
28 | * @var string |
||
29 | */ |
||
30 | protected $port = 443; |
||
31 | |||
32 | /** |
||
33 | * Creates a new instance of this checker with a Ssl Certificate Downloader. |
||
34 | * |
||
35 | * @param \Spatie\SslCertificate\Downloader $downloader |
||
36 | */ |
||
37 | public function __construct(Downloader $downloader) |
||
38 | { |
||
39 | $this->downloader = $downloader; |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * Requests the URL and handles any thrown exceptions. |
||
44 | * |
||
45 | * @return null |
||
46 | */ |
||
47 | public function run() |
||
48 | { |
||
49 | try { |
||
50 | $certificate = $this->downloader->usingPort($this->port)->forHost($this->hostname); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
51 | } catch (InvalidUrl $urlException) { |
||
52 | throw new CheckerHasFailed("Could not check the Ssl Certificate for \"{$this->hostname}\": {$urlException->getMessage()}"); |
||
53 | } catch (CouldNotDownloadCertificate $downloadException) { |
||
54 | throw new CheckerHasFailed("Could not download the Ssl Certificate for \"{$this->hostname}\": {$downloadException->getMessage()}"); |
||
55 | } |
||
56 | |||
57 | if ($certificate->isValid()) { |
||
58 | return; |
||
59 | } |
||
60 | |||
61 | throw new CheckerHasFailed("The Ssl Certificate for \"{$this->hostname}\" is not valid. The expiration date is {$certificate->expirationDate()}."); |
||
62 | } |
||
63 | } |
||
64 |