Completed
Push — master ( 766aca...d9f01f )
by Nicolas
7s
created

AddFile::getType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Akeneo\Crowdin\Api;
4
5
use \InvalidArgumentException;
6
use Akeneo\Crowdin\Client;
7
use Akeneo\Crowdin\FileReader;
8
use Akeneo\Crowdin\Translation;
9
10
/**
11
 * Adds a new file to a Crowdin project.
12
 *
13
 * @author Julien Janvier <[email protected]>
14
 * @see https://crowdin.net/page/api/add-file
15
 */
16
class AddFile extends AbstractApi
17
{
18
    /** @var FileReader */
19
    protected $fileReader;
20
21
    /** @var Translation[] */
22
    protected $translations;
23
24
    /** @var string */
25
    protected $type;
26
27
    /** @var string */
28
    protected $branch;
29
30
    /**
31
     * @param Client     $client
32
     * @param FileReader $fileReader
33
     */
34
    public function __construct(Client $client, FileReader $fileReader)
35
    {
36
        parent::__construct($client);
37
        $this->fileReader = $fileReader;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function execute()
44
    {
45
        if (0 === count($this->translations)) {
46
            throw new InvalidArgumentException('There is no files to add.');
47
        }
48
49
        $path = sprintf(
50
            "project/%s/add-file?key=%s",
51
            $this->client->getProjectIdentifier(),
52
            $this->client->getProjectApiKey()
53
        );
54
55
        $data = $this->parameters;
56
        if (null !== $this->type) {
57
            $data[] = [
58
                'name'      => 'type',
59
                'contents'  => $this->type
60
            ];
61
        }
62 View Code Duplication
        if (null !== $this->branch) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
63
            $data[] = [
64
                'name'      => 'branch',
65
                'contents'  => $this->branch
66
            ];
67
        }
68 View Code Duplication
        foreach ($this->translations as $translation) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
69
            $data[] = [
70
                'name'      => 'files['.$translation->getCrowdinPath().']',
71
                'contents'  => $this->fileReader->readTranslation($translation)
72
            ];
73
            if ($translation->getTitle()) {
74
                $data[] = [
75
                    'name'      => 'titles['.$translation->getCrowdinPath().']',
76
                    'contents'  => $translation->getTitle()
77
                ];
78
            }
79
            if ($translation->getExportPattern()) {
80
                $data[] = [
81
                    'name'      => 'export_patterns['.$translation->getCrowdinPath().']',
82
                    'contents'  => $translation->getExportPattern()
83
                ];
84
            }
85
        }
86
87
        $data = ['multipart' => $data];
88
        $response = $this->client->getHttpClient()->post($path, $data);
89
90
        return $response->getBody();
91
    }
92
93
    /**
94
     * @param string $localPath
95
     * @param string $crowdinPath
96
     * @param string $exportPattern
97
     * @param string $title
98
     *
99
     * @return $this
100
     */
101 View Code Duplication
    public function addTranslation($localPath, $crowdinPath, $exportPattern = null, $title = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
102
    {
103
        $translation = new Translation($localPath, $crowdinPath);
104
        $translation->setExportPattern($exportPattern);
105
        $translation->setTitle($title);
106
107
        $this->translations[] = $translation;
108
109
        return $this;
110
    }
111
112
    /**
113
     * @return Translation[]
114
     */
115
    public function getTranslations()
116
    {
117
        return $this->translations;
118
    }
119
120
    /**
121
     * @return string
122
     */
123
    public function getType()
124
    {
125
        return $this->type;
126
    }
127
128
    /**
129
     * @param string $type
130
     *
131
     * @return AddFile
132
     */
133
    public function setType($type)
134
    {
135
        $this->type = $type;
136
137
        return $this;
138
    }
139
140
    /**
141
     * @return string|null
142
     */
143
    public function getBranch()
144
    {
145
        return $this->branch;
146
    }
147
148
    /**
149
     * @param string $branch
150
     *
151
     * @return AddFile
152
     */
153
    public function setBranch($branch)
154
    {
155
        $this->branch = $branch;
156
157
        return $this;
158
    }
159
}
160