Passed
Pull Request — master (#14)
by Rimas
02:08
created

Upload   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 20
dl 0
loc 81
c 0
b 0
f 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getFields() 0 13 2
A getValidationConstraints() 0 8 1
A createResult() 0 3 1
A __construct() 0 4 1
A getMethod() 0 3 1
A getAction() 0 3 1
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
            ),
63
            'filename' => new Assert\Optional(
64
                [new Assert\NotBlank()]
65
            ),
66
        ]);
67
    }
68
69
    /**
70
     * Result object for this query result
71
     * @return UploadResult
72
     */
73
    public function createResult(): ResultInterface
74
    {
75
        return new UploadResult();
76
    }
77
78
    /**
79
     * API action name, part of full API request url
80
     * @return string
81
     */
82
    public function getAction(): string
83
    {
84
        return 'file/upload';
85
    }
86
87
    /**
88
     * HTTP method to use
89
     * @return string
90
     */
91
    public function getMethod(): string
92
    {
93
        return QueryInterface::POST;
94
    }
95
}
96