Jacek Kowalski
2022-08-15 927c6bf2e6c55ec2bbd3d02ae334e045161ef77b
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
 
namespace SIPL\UCRM\wFirma;
 
class UcrmHelper {
    protected $rootDirectory;
    protected $api = NULL;
    protected $attributes = NULL;
    protected $paymentMethods = NULL;
    protected $config = NULL;
    protected $event = NULL;
 
    function __construct(?string $rootDirectory = NULL) {
        if ($rootDirectory === NULL) {
            $rootDirectory = __DIR__ . '/..';
        }
        $this->rootDirectory = $rootDirectory;
    }
 
    function getRootDirectory() {
        return $this->rootDirectory;
    }
 
    function getApi() {
        if ($this->api === NULL) {
            $this->api = \Ubnt\UcrmPluginSdk\Service\UcrmApi::create($this->rootDirectory);
        }
        return $this->api;
    }
 
    function getAttributes() {
        if ($this->attributes === NULL) {
            $this->attributes = new UcrmAttributes($this);
        }
        return $this->attributes;
    }
 
    function getPaymentMethods() {
        if ($this->paymentMethods === NULL) {
            $this->paymentMethods = new UcrmPaymentMethods($this);
        }
        return $this->paymentMethods;
    }
 
    function getConfig() {
        if ($this->config === NULL) {
            $configManager = \Ubnt\UcrmPluginSdk\Service\PluginConfigManager::create($this->rootDirectory);
            $this->config = $configManager->loadConfig();
        }
        return $this->config;
    }
 
    function getCurrentEvent() {
        if ($this->event === NULL) {
            try {
                if (($_SERVER['REQUEST_METHOD'] ?? '') !== 'POST') {
                    throw new \RuntimeException('Failed to process event - wrong request method');
                }
 
                $data = file_get_contents('php://input');
                $data = json_decode($data, TRUE);
                if (!is_array($data)) {
                    throw new \RuntimeException('Failed to process event - invalid JSON');
                }
                if (!isset($data['uuid'])) {
                    throw new \RuntimeException('Failed to process event - missing UUID');
                }
                if (!ctype_alnum(strtr($data['uuid'], ['-' => '', '_' => '']))) {
                    throw new \RuntimeException('Failed to process event - invalid UUID');
                }
 
                $event = $this->getApi()->get('/webhook-events/' . $data['uuid']);
                if ($event['uuid'] !== $data['uuid']) {
                    throw new \RuntimeException('Failed to process event - event not found');
                }
 
                if (!is_int($event['entityId'])) {
                    throw new \RuntimeException('Failed to process event - invalid entity ID');
                }
 
                $this->event = $event;
            } catch (\Throwable $e) {
                $this->event = $e;
            }
        }
 
        if ($this->event instanceof \Throwable) {
            throw $this->event;
        }
 
        return $this->event;
    }
}