UpdateFile::addTranslation()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
dl 10
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 4
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
 * Upload latest version of your localization file to Crowdin.
12
 *
13
 * @author Nicolas Dupont <[email protected]>
14
 * @see https://crowdin.com/page/api/update-file
15
 */
16
class UpdateFile extends AbstractApi
17
{
18
    /** @var FileReader */
19
    protected $fileReader;
20
21
    /** @var Translation[] */
22
    protected $translations;
23
24
    /** @var string|null */
25
    protected $branch;
26
27
    /**
28
     * @param Client     $client
29
     * @param FileReader $fileReader
30
     */
31
    public function __construct(Client $client, FileReader $fileReader)
32
    {
33
        parent::__construct($client);
34
        $this->fileReader = $fileReader;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function execute()
41
    {
42
        if (count($this->translations) === 0) {
43
            throw new InvalidArgumentException('There is no files to update');
44
        }
45
        
46
        $this->addUrlParameter('key', $this->client->getProjectApiKey());
47
        
48
        $path = sprintf(
49
            "project/%s/update-file?%s",
50
            $this->client->getProjectIdentifier(),
51
            $this->getUrlQueryString()
52
        );
53
54
        $data = $this->parameters;
55 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...
56
            $data[] = [
57
                'name'      => 'branch',
58
                'contents'  => $this->branch
59
            ];
60
        }
61
62 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...
63
            $data[] = [
64
                'name'      => 'files['.$translation->getCrowdinPath().']',
65
                'contents'  => $this->fileReader->readTranslation($translation)
66
            ];
67
            if ($translation->getTitle()) {
68
                $data[] = [
69
                    'name'      => 'titles['.$translation->getCrowdinPath().']',
70
                    'contents'  => $translation->getTitle()
71
                ];
72
            }
73
            if ($translation->getExportPattern()) {
74
                $data[] = [
75
                    'name'      => 'export_patterns['.$translation->getCrowdinPath().']',
76
                    'contents'  => $translation->getExportPattern()
77
                ];
78
            }
79
        }
80
81
        $data = ['multipart' => $data];
82
        $response = $this->client->getHttpClient()->post($path, $data);
83
84
        return $response->getBody();
85
    }
86
87
    /**
88
     * @param string $localPath
89
     * @param string $crowdinPath
90
     * @param string $exportPattern
91
     * @param string $title
92
     *
93
     * @return $this
94
     */
95 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...
96
    {
97
        $translation = new Translation($localPath, $crowdinPath);
98
        $translation->setExportPattern($exportPattern);
99
        $translation->setTitle($title);
100
101
        $this->translations[] = $translation;
102
103
        return $this;
104
    }
105
106
    /**
107
     * @return Translation[]
108
     */
109
    public function getTranslations()
110
    {
111
        return $this->translations;
112
    }
113
114
    /**
115
     * @return null|string
116
     */
117
    public function getBranch()
118
    {
119
        return $this->branch;
120
    }
121
122
    /**
123
     * @param $branch
124
     */
125
    public function setBranch($branch)
126
    {
127
        $this->branch = $branch;
128
    }
129
}
130