Completed
Push — fix-checkstyle ( 525678...700890 )
by
unknown
03:06
created

UpdateFile::execute()   B

Complexity

Conditions 6
Paths 11

Size

Total Lines 31
Code Lines 19

Duplication

Lines 9
Ratio 29.03 %

Importance

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