aboutsummaryrefslogtreecommitdiffhomepage
path: root/Digitigrade/Policy/BlockInstance.php
blob: 5f902f27d2de97b4bdcff05c20a5310431e0b512 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php
namespace Digitigrade\Policy;

use Digitigrade\Model\Actor;
use Digitigrade\Model\Interaction;
use Digitigrade\Model\Note;
use Digitigrade\Model\PushableModel;
use Digitigrade\Policy;

class BlockInstance extends Policy {
    /** @var string[] */
    private array $blockedHosts;

    public function __construct(string ...$hosts) {
        $this->blockedHosts = $hosts;
    }

    private function isBlocked(PushableModel $obj): bool {
        $objHost = hostname_from_uri($obj->uri);
        foreach ($this->blockedHosts as $host) {
            if (str_ends_with($objHost, $host)) {
                return true;
            }
        }
        return false;
    }

    public function checkActor(Actor $actor): bool {
        return !$this->isBlocked($actor);
    }
    public function checkInteraction(Interaction $interaction): bool {
        return !$this->isBlocked($interaction);
    }
    public function checkNote(Note $note): bool {
        return !$this->isBlocked($note);
    }
}