Completed
Push — master ( eb5568...bc1b58 )
by Nicolas
9s
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 Akeneo\Crowdin\Translation;
6
7
/**
8
 * Adds a new file to a Crowdin project.
9
 *
10
 * @author Julien Janvier <[email protected]>
11
 * @see https://crowdin.net/page/api/add-file
12
 */
13
class AddFile extends AbstractApi
14
{
15
    /**
16
     * @var Translation[]
17
     */
18
    protected $translations;
19
20
    /**
21
     * @var string
22
     */
23
    protected $type;
24
25
    protected $branch;
26
27
    /**
28
     * @return mixed
29
     *
30
     * @throws \InvalidArgumentException
31
     */
32
    public function execute()
33
    {
34
        if (0 === count($this->translations)) {
35
            throw new \InvalidArgumentException('There is no files to add');
36
        }
37
38
        $path = sprintf(
39
            "project/%s/add-file?key=%s",
40
            $this->client->getProjectIdentifier(),
41
            $this->client->getProjectApiKey()
42
        );
43
44
        $data = $this->parameters;
45
        if (null !== $this->getType()) {
46
            $data['type'] = $this->type;
47
        }
48
        if (null !== $this->getBranch()) {
49
            $data['branch'] = $this->getBranch();
50
        }
51
52 View Code Duplication
        foreach ($this->getTranslations() 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...
53
            $data['files['.$translation->getCrowdinPath().']'] = '@'.$translation->getLocalPath();
54
            if ($translation->getTitle()) {
55
                $data['titles['.$translation->getCrowdinPath().']'] = $translation->getTitle();
56
            }
57
            if ($translation->getExportPattern()) {
58
                $data['export_patterns['.$translation->getCrowdinPath().']'] = $translation->getExportPattern();
59
            }
60
        }
61
62
        $request = $this->client->getHttpClient()->post($path, array(), $data);
63
        $response = $request->send();
64
65
        return $response->getBody(true);
66
    }
67
68
    /**
69
     * @param string $localPath
70
     * @param string $crowdinPath
71
     * @param string $exportPattern
72
     * @param string $title
73
     *
74
     * @return $this
75
     */
76 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...
77
    {
78
        $translation = new Translation($localPath, $crowdinPath);
79
        $translation->setExportPattern($exportPattern);
80
        $translation->setTitle($title);
81
82
        $this->translations[] = $translation;
83
84
        return $this;
85
    }
86
87
    /**
88
     * @return Translation[]
89
     */
90
    public function getTranslations()
91
    {
92
        return $this->translations;
93
    }
94
95
    /**
96
     * @return string
97
     */
98
    public function getType()
99
    {
100
        return $this->type;
101
    }
102
103
    /**
104
     * @param string $type
105
     */
106
    public function setType($type)
107
    {
108
        $this->type = $type;
109
    }
110
111
    /**
112
     * @return string|null
113
     */
114
    public function getBranch()
115
    {
116
        return $this->branch;
117
    }
118
119
    /**
120
     * @param string $branch
121
     */
122
    public function setBranch($branch)
123
    {
124
        $this->branch = $branch;
125
    }
126
}
127