Documents::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 5
rs 10
1
<?php
2
3
namespace vinicinbgs\Autentique;
4
5
use vinicinbgs\Autentique\Utils\Query;
6
use vinicinbgs\Autentique\BaseResource;
7
8
/**
9
 * Class Documents
10
 * @package vinicinbgs\Autentique
11
 * @see https://beadev.net/autentique-v2/documents
12
 */
13
class Documents extends BaseResource
14
{
15
    /**
16
     * GraphQL Query
17
     * @var Query
18
     */
19
    private $query;
20
21
    /**
22
     * Documents constructor.
23
     *
24
     * @param string|null $token Autentique API Token
25
     * @param int $timeout=60 Request Timeout in seconds
26
     */
27
    public function __construct(string $token = null, int $timeout = 60)
28
    {
29
        parent::__construct($token, $timeout);
30
31
        $this->query = new Query($this->resourcesEnum::DOCUMENTS);
32
    }
33
34
    /**
35
     * List all documents
36
     * @api
37
     * @param  int  $page Page number (starts at 1)
38
     * @return array
39
     */
40
    public function listAll(int $page = 1): array
41
    {
42
        $graphQuery = $this->query->query(__FUNCTION__);
43
44
        $graphQuery = $this->query->setVariables(
45
            ["page", "sandbox"],
46
            [$page, $this->sandbox],
47
            $graphQuery
48
        );
49
50
        return $this->api->request($this->token, $graphQuery, "json");
51
    }
52
53
    /**
54
     * List document by id
55
     * @api
56
     * @see https://docs.autentique.com.br/api/queries/resgatando-documentos#resgatando-um-documento-especifico
57
     * @param string $documentId Document UUID
58
     * @return array
59
     */
60
    public function listById(string $documentId): array
61
    {
62
        $graphQuery = $this->query->query(__FUNCTION__);
63
        $graphQuery = $this->query->setVariables("documentId", $documentId, $graphQuery);
64
65
        return $this->api->request($this->token, $graphQuery, "json");
66
    }
67
68
    /**
69
     * Create Document
70
     * @api
71
     * @see https://docs.autentique.com.br/api/mutations/criando-um-documento
72
     * @param array $attributes ["document", "signers", "file", "organization_id"]
73
     * @return array
74
     */
75
    public function create(array $attributes): array
76
    {
77
        $variables = [
78
            "document" => $attributes["document"],
79
            "signers" => $attributes["signers"],
80
            "file" => null,
81
        ];
82
83
        $queryFile = __FUNCTION__;
84
85
        if (isset($attributes["organization_id"]) && !empty($attributes["organization_id"])) {
86
            $variables["organization_id"] = $attributes["organization_id"];
87
            $queryFile = __FUNCTION__ . "WithOrganization";
88
        }
89
90
        $graphMutation = $this->query->query($queryFile);
91
        $graphMutation = $this->query->setVariables(
92
            ["variables", "sandbox"],
93
            [json_encode($variables), $this->sandbox],
94
            $graphMutation
95
        );
96
97
        return $this->api->request($this->token, $graphMutation, "form", $attributes["file"]);
98
    }
99
100
    /**
101
     * Update Document
102
     * @api
103
     * @see https://docs.autentique.com.br/api/mutations/editando-um-documento Attributes
104
     * @param string $id        Document UUID
105
     * @param array $attributes ["document"]
106
     * @return array
107
     */
108
    public function update(string $id, array $attributes): array
109
    {
110
        $variables = [
111
            "id" => $id,
112
            "document" => $attributes["document"],
113
        ];
114
115
        $graphMutation = $this->query->query(__FUNCTION__);
116
        $graphMutation = $this->query->setVariables(
117
            ["variables", "sandbox"],
118
            [json_encode($variables), $this->sandbox],
119
            $graphMutation
120
        );
121
122
        return $this->api->request($this->token, $graphMutation, "json");
123
    }
124
125
    /**
126
     * Sign document by id
127
     * @api
128
     * @see https://docs.autentique.com.br/api/mutations/assinando-um-documento
129
     * @param string $documentId Document UUID
130
     * @return array
131
     */
132
    public function signById(string $documentId): array
133
    {
134
        $graphQuery = $this->query->query(__FUNCTION__);
135
        $graphQuery = $this->query->setVariables("documentId", $documentId, $graphQuery);
136
137
        return $this->api->request($this->token, $graphQuery, "json");
138
    }
139
140
    /**
141
     * Delete document by id
142
     * @api
143
     * @param string $documentId Document UUID
144
     * @return array
145
     */
146
    public function deleteById(string $documentId)
147
    {
148
        $graphQuery = $this->query->query(__FUNCTION__);
149
        $graphQuery = $this->query->setVariables("documentId", $documentId, $graphQuery);
150
151
        return $this->api->request($this->token, $graphQuery, "json");
152
    }
153
154
    /**
155
     * Move document to folder
156
     * @api
157
     * @see https://docs.autentique.com.br/api/mutations/movendo-documento-para-pasta
158
     * @param string $documentId Document UUID
159
     * @param string $folderId Folder UUID
160
     * @return array
161
     */
162
    public function moveToFolder(string $documentId, string $folderId): array
163
    {
164
        $graphQuery = $this->query->query(__FUNCTION__);
165
166
        $graphQuery = $this->query->setVariables(
167
            ["documentId", "folderId"],
168
            [$documentId, $folderId],
169
            $graphQuery
170
        );
171
172
        return $this->api->request($this->token, $graphQuery, "json");
173
    }
174
175
    /**
176
     * Move document from folder to folder
177
     * @api
178
     * @see https://docs.autentique.com.br/api/mutations/movendo-documento-para-pasta
179
     * @param string $documentId Document UUID
180
     * @param string $folderId Folder UUID
181
     * @param string $currentFolderId Current Folder UUID
182
     * @return array
183
     */
184
    public function moveToFolderByFolder(
185
        string $documentId,
186
        string $folderId,
187
        string $currentFolderId
188
    ): array {
189
        $graphQuery = $this->query->query(__FUNCTION__);
190
191
        $graphQuery = $this->query->setVariables(
192
            ["documentId", "folderId", "currentFolderId"],
193
            [$documentId, $folderId, $currentFolderId],
194
            $graphQuery
195
        );
196
197
        return $this->api->request($this->token, $graphQuery, "json");
198
    }
199
200
    /**
201
     * Delete signer from document
202
     * @api
203
     * @param string $documentId Document UUID
204
     * @param string $signerPublicId Signer Public UUID
205
     * @return array
206
     */
207
    public function deleteSigner(string $documentId, string $signerPublicId): array
208
    {
209
        $graphQuery = $this->query->query(__FUNCTION__);
210
211
        $graphQuery = $this->query->setVariables(
212
            ["documentId", "signerPublicId"],
213
            [$documentId, $signerPublicId],
214
            $graphQuery
215
        );
216
217
        return $this->api->request($this->token, $graphQuery, "json");
218
    }
219
220
    /**
221
     * Create signer to document
222
     * @api
223
     * @param string $documentId Document UUID
224
     * @param array $signer Signer attributes
225
     * @return array
226
     */
227
    public function createSigner(string $documentId, array $attributes): array
228
    {
229
        $variables = [
230
            "document_id" => $documentId,
231
            "signer" => $attributes["signer"],
232
        ];
233
234
        $graphMutation = $this->query->query(__FUNCTION__);
235
236
        $graphMutation = $this->query->setVariables(
237
            ["variables"],
238
            [json_encode($variables)],
239
            $graphMutation
240
        );
241
242
        return $this->api->request($this->token, $graphMutation, "json");
243
    }
244
}
245