Skip to main content
Version: 1.2.3

Creating a custom SslCertificateCheck model

If you want to add new features or improve the original MoonGuard SslCertificateCheck model, we allow you to create a custom SslCertificateCheck Model for your project doing the following steps.

  1. Create a new SslCertificateCheck class that extends from  Illuminate\Database\Eloquent\Model and implements the  Taecontrol\MoonGuard\Contracts\MoonGuardSslCertificateCheck interface.

    <?php

    use Illuminate\Database\Eloquent\Model;
    use Taecontrol\MoonGuard\Contracts\MoonGuardSslCertificateCheck;

    class SslCertificateCheck extends Model implements MoonGuardSslCertificateCheck
    {
    //Contract implementation
    }
  2. Implement all the properties and methods required, you can guide yourself with model reference.

  3. Replace the SslCertificateCheck class in the configuration file.

    <?php
    [
    'ssl_certificate_check' => [
    /*
    * Enable or disable ssl certificate checks globally.
    */
    'enabled' => true,

    /*
    * The ssl certificate check model to use.
    */
    'model' => \Taecontrol\MoonGuard\Models\SslCertificateCheck::class, -> // replace model

    /*
    * The number of days before a certificate expires to send a notification.
    */
    'notify_expiring_soon_if_certificate_expires_within_days' => 7,
    ]
    ]

Model Reference

<?php

namespace Taecontrol\MoonGuard\Models;

use Exception;
use Spatie\Url\Url;
use Illuminate\Database\Eloquent\Model;
use Spatie\SslCertificate\SslCertificate;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Taecontrol\MoonGuard\Enums\SslCertificateStatus;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Taecontrol\MoonGuard\Repositories\SiteRepository;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Taecontrol\MoonGuard\Contracts\MoonGuardSslCertificateCheck;
use Taecontrol\MoonGuard\Repositories\SslCertificateCheckRepository;

class SslCertificateCheck extends Model implements MoonGuardSslCertificateCheck
{
use HasFactory;

protected $casts = [
'status' => SslCertificateStatus::class,
'expiration_date' => 'immutable_datetime',
];

public function site(): BelongsTo
{
return $this->belongsTo(SiteRepository::resolveModelClass());
}

public function saveCertificate(SslCertificate $certificate, Url $url): void
{
$this->status = $certificate->isValid($url)
? SslCertificateStatus::VALID
: SslCertificateStatus::INVALID;

$this->expiration_date = $certificate->expirationDate();
$this->issuer = $certificate->getIssuer();
$this->check_failure_reason = null;

$this->save();
}

public function saveError(Exception $exception): void
{
$this->status = SslCertificateStatus::INVALID;
$this->expiration_date = null;
$this->issuer = '';
$this->check_failure_reason = $exception->getMessage();

$this->save();
}

public function certificateIsValid(): bool
{
return $this->status === SslCertificateStatus::VALID;
}

public function certificateIsInvalid(): bool
{
return $this->status === SslCertificateStatus::INVALID;
}

public function certificateIsAboutToExpire(int $maxDaysToExpire): bool
{
return $this->expiration_date?->diffInDays() <= $maxDaysToExpire;
}

public function isEnabled(): Attribute
{
return Attribute::make(
get: fn () => SslCertificateCheckRepository::isEnabled(),
);
}
}

Casts

Property NameCast TypeDescription
statusSslCertificateStatusThe status of the SSL certificate check, casted to the SslCertificateStatus enum
expiration_dateimmutable_datetimeThe expiration date of the SSL certificate, casted to an immutable datetime object

Methods

Function NameReturn TypeDescription
site()BelongsToReturns a BelongsTo relationship between the SslCertificateCheck model and the Site model. This method allows you to retrieve the site associated with the SSL certificate check.
saveCertificate(SslCertificate $certificate, Url $url)voidSaves the result of a successful SSL certificate check. This method sets the status of the SSL certificate check to "VALID" if the certificate is valid, or "INVALID" if the certificate is invalid. It also saves the expiration date and issuer of the certificate.
saveError(Exception $exception)voidSaves the result of a failed SSL certificate check. This method sets the status of the SSL certificate check to "INVALID", and saves the failure reason.
certificateIsValid()boolReturns a boolean indicating whether the SSL certificate is valid.
certificateIsInvalid()boolReturns a boolean indicating whether the SSL certificate is invalid.
certificateIsAboutToExpire(int $maxDaysToExpire)boolReturns a boolean indicating whether the SSL certificate is about to expire within the specified number of days.
isEnabled()AttributeReturns an Attribute instance that indicates whether the SSL certificate check is enabled.