Document::upload()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 10
c 3
b 1
f 0
dl 0
loc 17
rs 9.9332
cc 2
nc 2
nop 3
1
<?php
2
3
namespace Lyal\Checkr\Entities\Resources;
4
5
use Lyal\Checkr\Client;
6
7
class Document extends AbstractResource
8
{
9
    /**
10
     * Document constructor.
11
     *
12
     * @param null|string|array $values
13
     * @param null|Client $client
14
     */
15
    public function __construct($values = null, Client $client = null)
16
    {
17
        $this->setHidden([
18
            'report_id',
19
            'hidden',
20
        ]);
21
22
        parent::__construct($values, $client);
23
    }
24
25
26
    /**
27
     * Upload a document using a multipart form.
28
     *
29
     * @param string $type
30
     * @param string $file
31
     * @param string|null $candidateId
32
     *
33
     * @return $this
34
     */
35
    public function upload($type, $file, $candidateId = null)
36
    {
37
        if ($candidateId !== null) {
38
            $this->setAttribute('candidate_id', $candidateId);
39
        }
40
41
        $response = $this->uploadRequest($this->processPath('candidates/:candidate_id/documents',
42
            ['candidate_id' => $this->getAttribute('candidate_id')]),
43
            ['multipart' => [
44
                ['name' => 'type', 'contents' => $type],
45
                ['name' => 'file', 'contents' => fopen($file, 'rb')],
46
            ],
47
            ]);
48
        $this->setAttributes([]);
49
        $this->setValues($response);
50
51
        return $this;
52
    }
53
54
    protected function uploadRequest($path, $options)
55
    {
56
        return $this->getClient()->request('post', $path, $options);
57
    }
58
}
59