Create::getValidationConstraints()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 94
Code Lines 63

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 63
dl 0
loc 94
c 0
b 0
f 0
rs 8.8072
cc 1
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace Dokobit\Gateway\Query\Signing;
3
4
use Dokobit\Gateway\DocumentTypeProvider;
5
use Dokobit\Gateway\Query\QueryInterface;
6
use Dokobit\Gateway\Result\ResultInterface;
7
use Dokobit\Gateway\Result\Signing\CreateResult;
8
use Dokobit\Gateway\SigningPurposeProvider;
9
use Dokobit\Gateway\Validator\Constraints as MyAssert;
10
use Symfony\Component\Validator\Constraints as Assert;
11
12
/**
13
 * Create a new signing from uploaded file(s).
14
 * @see https://gateway-sandbox.dokobit.com/api/doc#_api_signing_create
15
 */
16
class Create implements QueryInterface
17
{
18
    const FILE_TYPE_MAIN = 'main';
19
    const FILE_TYPE_APPENDIX = 'appendix';
20
    const FILE_TYPE_ATTACHMENT = 'attachment';
21
22
    /** @var string signing document type */
23
    private $type;
24
25
    /** @var string signing name */
26
    private $name;
27
28
    /** @var array tokens and other information about the douments to be signed */
29
    private $files;
30
31
    /** @var array|null information about document signers */
32
    private $signers;
33
34
    /** @var string|null postback URL, if specified */
35
    private $postbackUrl;
36
37
    /** @var string|null preferred Gateway UI language, if specified */
38
    private $language;
39
40
    /** @var array|null document type-specific options */
41
    private $params;
42
43
    /**
44
     * @param string $type document type
45
     * @param string $name document name
46
     * @param array $files tokens and types of the documents to be signed. Format:
47
     *       [
48
     *           [ 'token' => 'FirstUploadedFileToken', 'type' => 'main' ],
49
     *           [ 'token' => 'SecondUploadedFileToken', 'type' => 'appendix' ],
50
     *           ...
51
     *       ]
52
     *       Specifying `type` is optional, and it is only relevant when creating certain document types.
53
     * @param array|null $signers array with information about document signers. Format:
54
     *       [
55
     *           [ 'id' => 'signer1', 'name' => 'Kraft', 'surname' => 'Lawrence', ... ],
56
     *           [ 'id' => 'signer2', 'name' => 'Fleur', 'surname' => 'Boland', ... ],
57
     *           ...
58
     *       ]
59
     *       The value of `id` is entirely up to you. It is used to refer to the signer afterwards,
60
     *       e.g. when checking signing status, or removing signer from the signing.
61
     *       For all supported signer properties, check out the API method documentation.
62
     * @param string|null $postbackUrl postback URL
63
     * @param string|null $language code of language to be used when communicating with the signer.
64
     *       Currently supported values: en, et, is, lt, lv, ru.
65
     * @param array|null $params Optional parameters per file type. E.g. for PDF:
66
     *       [ 'level' => 'pades-ltv' ]
67
     *       For all supported params, check out the API method documentation.
68
     */
69
    public function __construct(
70
        string $type,
71
        string $name,
72
        array $files,
73
        ?array $signers = null,
74
        ?string $postbackUrl = null,
75
        ?string $language = null,
76
        ?array $params = null
77
    ) {
78
        $this->type = $type;
79
        $this->name = $name;
80
        $this->files = $files;
81
        $this->signers = $signers;
82
        $this->postbackUrl = $postbackUrl;
83
        $this->language = $language;
84
        $this->params = $params;
85
    }
86
87
    /**
88
     * Field and values association used in query
89
     * @return array
90
     */
91
    public function getFields(): array
92
    {
93
        $return = [
94
            'type' => $this->type,
95
            'name' => $this->name,
96
            'files' => $this->files,
97
        ];
98
99
        if ($this->signers !== null) {
100
            $return['signers'] = $this->signers;
101
        }
102
103
        if ($this->postbackUrl !== null) {
104
            $return['postback_url'] = $this->postbackUrl;
105
        }
106
107
        if ($this->language !== null) {
108
            $return['language'] = $this->language;
109
        }
110
111
        if ($this->params !== null) {
112
            $return[$this->type] = $this->params;
113
        }
114
115
        return $return;
116
    }
117
118
    /**
119
     * Validation constraints for request data validation
120
     * @return Assert\Collection
121
     */
122
    public function getValidationConstraints(): Assert\Collection
123
    {
124
        return new Assert\Collection([
125
            'type' => new Assert\Required([
126
                new Assert\NotBlank(),
127
                new Assert\Choice([
128
                    'choices' => DocumentTypeProvider::getAllDocumentTypes(),
129
                ]),
130
            ]),
131
            'name' => new Assert\Required([
132
                new Assert\NotBlank(),
133
            ]),
134
            'files' => new Assert\Required([
135
                new Assert\NotBlank(),
136
                new Assert\All([
137
                    new Assert\Collection([
138
                        'token' => new Assert\Required([
139
                            new Assert\NotBlank(),
140
                        ]),
141
                        'type' => new Assert\Optional([
142
                            new Assert\NotBlank(),
143
                            new Assert\Choice([
144
                                'choices' => [
145
                                    self::FILE_TYPE_MAIN,
146
                                    self::FILE_TYPE_APPENDIX,
147
                                    self::FILE_TYPE_ATTACHMENT,
148
                                ],
149
                            ]),
150
                        ]),
151
                    ]),
152
                ]),
153
            ]),
154
            'signers' => new Assert\Optional([
155
                new Assert\NotBlank(),
156
                new Assert\All([
157
                    new Assert\Collection([
158
                        'id' => new Assert\Required([
159
                            new Assert\NotBlank(),
160
                        ]),
161
                        'name' => new Assert\Required([
162
                            new Assert\NotBlank(),
163
                        ]),
164
                        'surname' => new Assert\Required([
165
                            new Assert\NotBlank(),
166
                        ]),
167
                        'code' => new Assert\Optional([
168
                            new Assert\NotBlank(),
169
                            new MyAssert\Code(),
170
                        ]),
171
                        'phone' => new Assert\Optional([
172
                            new Assert\NotBlank(),
173
                            new MyAssert\Phone(),
174
                        ]),
175
                        'company' => new Assert\Optional([
176
                            new Assert\NotBlank(),
177
                        ]),
178
                        'country' => new Assert\Optional([
179
                            new Assert\NotBlank(),
180
                        ]),
181
                        'country_code' => new Assert\Optional([
182
                            new Assert\NotBlank(),
183
                            new Assert\Country(),
184
                        ]),
185
                        'city' => new Assert\Optional([
186
                            new Assert\NotBlank(),
187
                        ]),
188
                        'postal_code' => new Assert\Optional([
189
                            new Assert\NotBlank(),
190
                        ]),
191
                        'position' => new Assert\Optional([
192
                            new Assert\NotBlank(),
193
                        ]),
194
                        'structural_subdivision' => new Assert\Optional([
195
                            new Assert\NotBlank(),
196
                        ]),
197
                        'signing_purpose' => new Assert\Optional([
198
                            new Assert\NotBlank(),
199
                            new Assert\Choice([
200
                                'choices' => SigningPurposeProvider::getAllSigningPurposes(),
201
                            ]),
202
                        ]),
203
                        $this->type => new Assert\Optional(),
204
                    ]),
205
                ]),
206
            ]),
207
            'postback_url' => new Assert\Optional([
208
                new Assert\NotBlank(),
209
                new Assert\Url(),
210
            ]),
211
            'language' => new Assert\Optional([
212
                new Assert\NotBlank(),
213
                new Assert\Language(),
214
            ]),
215
            $this->type => new Assert\Optional(),
216
        ]);
217
    }
218
219
    /**
220
     * Result object for this query result
221
     * @return CreateResult
222
     */
223
    public function createResult(): ResultInterface
224
    {
225
        return new CreateResult();
226
    }
227
228
    /**
229
     * API action name, part of full API request url
230
     * @return string
231
     */
232
    public function getAction(): string
233
    {
234
        return 'signing/create';
235
    }
236
237
    /**
238
     * HTTP method to use
239
     * @return string
240
     */
241
    public function getMethod(): string
242
    {
243
        return QueryInterface::POST;
244
    }
245
}
246