Code Duplication    Length = 56-57 lines in 2 locations

src/Document.php 1 location

@@ 140-195 (lines=56) @@
137
     * @param  array  $body
138
     * @return object
139
     */
140
    public function create(array $body)
141
    {
142
        // keys that are mandatory for this request
143
        $needKeys = [
144
            'Description',
145
            'FileContent',
146
            'FileMD5CheckSum',
147
            'FileName',
148
            'Language',
149
            'SigneeRefs',
150
            'SignJobId',
151
            'Title',
152
        ];
153
154
        // keys that need to be present in each signeeref
155
        $needSubKeys = [
156
            'SigneeRefId',
157
            'FirstName',
158
            'LastName',
159
            'Email',
160
        ];
161
162
        // if the body doesn't have needed fields, throw an exception
163
        if (! array_has_all_keys($body, $needKeys)) {
164
            throw new BadMethodCallException(
165
                'Missing fields in input array. Need '.implode(', ', $needKeys)
166
            );
167
        } elseif (! is_array($body['SigneeRefs'])) {
168
            throw new UnexpectedValueException('SigneeRefs key in input should be an array');
169
        } else {
170
            foreach ($body['SigneeRefs'] as $ref) {
171
                if (! is_array($ref)) {
172
                    throw new UnexpectedValueException('Each item in SigneeRefs should be an array');
173
                } elseif (! array_has_all_keys($ref, $needSubKeys)) {
174
                    throw new BadMethodCallException(
175
                        'Missing fields in SigneeRefs item. Need '.implode(', ', $needSubKeys)
176
                    );
177
                }
178
            }
179
        }
180
181
        // make the URL for this request
182
        $url = self::URI;
183
184
        // get the headers for this request
185
        $headers = $this->headers->make('POST', $url, $body, true);
186
187
        // get the response
188
        $response = $this->client->post($url, [
189
            'headers' => $headers,
190
            'json' => $body,
191
        ]);
192
193
        // return the response
194
        return $response;
195
    }
196
197
    /**
198
     * Creates a new document to sign, and returns

src/ExternalSign.php 1 location

@@ 119-175 (lines=57) @@
116
     * @param  array  $body
117
     * @return object
118
     */
119
    public function createRequest(array $body)
120
    {
121
        // keys that are mandatory for this request
122
        $needKeys = [
123
            'Description',
124
            'ExternalDocumentId',
125
            'FileContent',
126
            'Filename',
127
            'ReturnUrlError',
128
            'ReturnUrlSuccess',
129
            'ReturnUrlUserAbort',
130
            'SigneeRefs',
131
            'Title',
132
        ];
133
134
        // keys that need to be present in each signeeref
135
        $needSubKeys = [
136
            'UniqueRef',
137
            'FirstName',
138
            'LastName',
139
            'Email',
140
        ];
141
142
        // if the body doesn't have needed fields, throw an exception
143
        if (! array_has_all_keys($body, $needKeys)) {
144
            throw new BadMethodCallException(
145
                'Missing fields in input array. Need '.implode(', ', $needKeys)
146
            );
147
        } elseif (! is_array($body['SigneeRefs'])) {
148
            throw new UnexpectedValueException('SigneeRefs key in input should be an array');
149
        } else {
150
            foreach ($body['SigneeRefs'] as $ref) {
151
                if (! is_array($ref)) {
152
                    throw new UnexpectedValueException('Each item in SigneeRefs should be an array');
153
                } elseif (! array_has_all_keys($ref, $needSubKeys)) {
154
                    throw new BadMethodCallException(
155
                        'Missing fields in SigneeRefs item. Need '.implode(', ', $needSubKeys)
156
                    );
157
                }
158
            }
159
        }
160
161
        // make the URL for this request
162
        $url = self::URI;
163
164
        // get the headers for this request
165
        $headers = $this->headers->make('POST', $url, $body, true);
166
167
        // get the response
168
        $response = $this->client->post($url, [
169
            'headers' => $headers,
170
            'json' => $body,
171
        ]);
172
173
        // return the response
174
        return $response;
175
    }
176
177
    /**
178
     * Creates a app launch uri for the BankID app.