Passed
Branch master (641bc5)
by Vinicius Morais
01:58
created

Documents::deleteSigner()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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