Completed
Pull Request — master (#16)
by Rimas
02:02
created

UploadFromUrl::getFields()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 15
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
namespace Dokobit\Gateway\Query\File;
3
4
use Dokobit\Gateway\Query\QueryInterface;
5
use Dokobit\Gateway\Result\File\UploadResult;
6
use Dokobit\Gateway\Result\ResultInterface;
7
use Symfony\Component\Validator\Constraints as Assert;
8
9
/**
10
 * Upload a file to Gateway from URL.
11
 * @see https://gateway-sandbox.dokobit.com/api/doc#_api_file_upload
12
 */
13
class UploadFromUrl implements QueryInterface
14
{
15
    /** @var string url of the file to be uploded */
16
    private $url;
17
18
    /** @var string SHA1 digest of the file to be uploded */
19
    private $digest;
20
21
    /** @var string|null file name which will be sent to Gateway */
22
    private $filename;
23
24
    /**
25
     * @param string $url url of the file to be uploded
26
     * @param string $digest SHA1 digest of the file to be uploded
27
     * @param string|null $filename file name which will be sent to Gateway.
28
     *                    If null or not set, original file name will be sent.
29
     */
30
    public function __construct(string $url, string $digest, ?string $filename = null)
31
    {
32
        $this->url = $url;
33
        $this->digest = $digest;
34
        $this->filename = $filename;
35
    }
36
37
    /**
38
     * Field and values association used in query
39
     * @return array
40
     */
41
    public function getFields(): array
42
    {
43
        if ($this->filename === null) {
44
            $this->filename = basename(parse_url($this->url, PHP_URL_PATH));
45
        }
46
47
        $return = [
48
            'file' => [
49
                'name' => $this->filename,
50
                'digest' => $this->digest,
51
                'url' => $this->url,
52
            ],
53
        ];
54
55
        return $return;
56
    }
57
58
    /**
59
     * Validation constraints for request data validation
60
     * @return Assert\Collection
61
     */
62
    public function getValidationConstraints(): Assert\Collection
63
    {
64
        return new Assert\Collection([
65
            'file' => new Assert\Required([
66
                new Assert\NotBlank(),
67
                new Assert\Collection([
68
                    'name' => new Assert\Required([
69
                        new Assert\NotBlank(),
70
                    ]),
71
                    'url' => new Assert\Required([
72
                        new Assert\NotBlank(),
73
                        new Assert\Url(),
74
                    ]),
75
                    'digest' => new Assert\Required([
76
                        new Assert\NotBlank(),
77
                    ]),
78
                ]),
79
            ]),
80
        ]);
81
    }
82
83
    /**
84
     * Result object for this query result
85
     * @return UploadResult
86
     */
87
    public function createResult(): ResultInterface
88
    {
89
        return new UploadResult();
90
    }
91
92
    /**
93
     * API action name, part of full API request url
94
     * @return string
95
     */
96
    public function getAction(): string
97
    {
98
        return 'file/upload';
99
    }
100
101
    /**
102
     * HTTP method to use
103
     * @return string
104
     */
105
    public function getMethod(): string
106
    {
107
        return QueryInterface::POST;
108
    }
109
}
110