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

AddFile::execute()   C

Complexity

Conditions 7
Paths 21

Size

Total Lines 35
Code Lines 21

Duplication

Lines 9
Ratio 25.71 %

Importance

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