aboutsummaryrefslogtreecommitdiffhomepage
path: root/Digitigrade/Job/ProcessIncomingSimplePush.php
blob: a48a70bef3bdfcbfae066b9590f877981051c736 (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
38
39
40
41
42
43
44
<?php
namespace Digitigrade\Job;

use Digitigrade\Job;
use Digitigrade\JobQueue;
use Digitigrade\Logger;

class ProcessIncomingSimplePush extends Job {
    public string $uri;

    public function __construct(string $uri) {
        $this->uri = $uri;
        $this->remainingTries = 3;
    }

    public function run() {
        $log = Logger::getInstance();

        $log->info("processing simple push for $this->uri");

        $result = send_request_authenticated($this->uri);
        if (!str_contains($result->headers[0], '200')) {
            throw new \RuntimeException("unable to fetch given simple push uri: $this->uri");
        }
        $obj = json_decode($result->body);

        if ($obj === null) {
            throw new \RuntimeException("fetched content doesn't look like valid json");
        }
        if (!isset($obj->type, $obj->self)) {
            throw new \RuntimeException('the object needs to have `type` and `self` properties');
        }
        if ($obj->self != $this->uri) {
            throw new \RuntimeException("fetched object has a different uri from the one provided ($obj->self != $this->uri)");
        }

        $log->info('fetch successful, pushed object will be dealt with soon');
        (new ProcessIncomingPush($obj))->submit();
    }

    public function submitDelayed(int $delaySecs) {
        JobQueue::getInstance()->submitDelayed($this, $delaySecs);
    }
}