Completed
Push — master ( eb5568...bc1b58 )
by Nicolas
9s
created

UpdateFile::getTranslations()   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
 * Upload latest version of your localization file to Crowdin.
9
 *
10
 * @author Nicolas Dupont <[email protected]>
11
 * @see http://crowdin.net/page/api/update-file
12
 */
13
class UpdateFile extends AbstractApi
14
{
15
    /**
16
     * @var Translation[]
17
     */
18
    protected $translations;
19
20
    /** @var string|null */
21
    protected $branch;
22
23
    /**
24
     * @return mixed
25
     *
26
     * @throws \InvalidArgumentException
27
     */
28
    public function execute()
29
    {
30
        if (count($this->translations) === 0) {
31
            throw new \InvalidArgumentException('There is no files to update');
32
        }
33
        $path = sprintf(
34
            "project/%s/update-file?key=%s",
35
            $this->client->getProjectIdentifier(),
36
            $this->client->getProjectApiKey()
37
        );
38
39
        $data = $this->parameters;
40
        if (null !== $this->getBranch()) {
41
            $data['branch'] = $this->getBranch();
42
        }
43
44 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...
45
            $data['files['.$translation->getCrowdinPath().']'] = '@'.$translation->getLocalPath();
46
            if ($translation->getTitle()) {
47
                $data['titles['.$translation->getCrowdinPath().']'] = $translation->getTitle();
48
            }
49
            if ($translation->getExportPattern()) {
50
                $data['export_patterns['.$translation->getCrowdinPath().']'] = $translation->getExportPattern();
51
            }
52
        }
53
54
        $request = $this->client->getHttpClient()->post($path, array(), $data);
55
        $response = $request->send();
56
57
        return $response->getBody(true);
58
    }
59
60
    /**
61
     * @param string $localPath
62
     * @param string $crowdinPath
63
     * @param string $exportPattern
64
     * @param string $title
65
     *
66
     * @return $this
67
     */
68 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...
69
    {
70
        $translation = new Translation($localPath, $crowdinPath);
71
        $translation->setExportPattern($exportPattern);
72
        $translation->setTitle($title);
73
74
        $this->translations[] = $translation;
75
76
        return $this;
77
    }
78
79
    /**
80
     * @return Translation[]
81
     */
82
    public function getTranslations()
83
    {
84
        return $this->translations;
85
    }
86
87
    /**
88
     * @return null|string
89
     */
90
    public function getBranch()
91
    {
92
        return $this->branch;
93
    }
94
95
    /**
96
     * @param $branch
97
     */
98
    public function setBranch($branch)
99
    {
100
        $this->branch = $branch;
101
    }
102
}
103