Passed
Pull Request — develop (#19)
by Paulius
02:32
created

Upload::getAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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