AddFile::execute()   C
last analyzed

Complexity

Conditions 7
Paths 21

Size

Total Lines 51
Code Lines 32

Duplication

Lines 24
Ratio 47.06 %

Importance

Changes 0
Metric Value
dl 24
loc 51
rs 6.9743
c 0
b 0
f 0
cc 7
eloc 32
nc 21
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
 * Adds a new file to a Crowdin project.
12
 *
13
 * @author Julien Janvier <[email protected]>
14
 * @see https://crowdin.com/page/api/add-file
15
 */
16
class AddFile extends AbstractApi
17
{
18
    /** @var FileReader */
19
    protected $fileReader;
20
21
    /** @var Translation[] */
22
    protected $translations;
23
24
    /** @var string */
25
    protected $type;
26
27
    /** @var string */
28
    protected $branch;
29
30
    /**
31
     * @param Client     $client
32
     * @param FileReader $fileReader
33
     */
34
    public function __construct(Client $client, FileReader $fileReader)
35
    {
36
        parent::__construct($client);
37
        $this->fileReader = $fileReader;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function execute()
44
    {
45
        if (0 === count($this->translations)) {
46
            throw new InvalidArgumentException('There is no files to add.');
47
        }
48
        
49
        $this->addUrlParameter('key', $this->client->getProjectApiKey());
50
51
        $path = sprintf(
52
            "project/%s/add-file?%s",
53
            $this->client->getProjectIdentifier(),
54
            $this->getUrlQueryString()
55
        );
56
57
        $data = $this->parameters;
58
        if (null !== $this->type) {
59
            $data[] = [
60
                'name'      => 'type',
61
                'contents'  => $this->type
62
            ];
63
        }
64 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...
65
            $data[] = [
66
                'name'      => 'branch',
67
                'contents'  => $this->branch
68
            ];
69
        }
70 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...
71
            $data[] = [
72
                'name'      => 'files['.$translation->getCrowdinPath().']',
73
                'contents'  => $this->fileReader->readTranslation($translation)
74
            ];
75
            if ($translation->getTitle()) {
76
                $data[] = [
77
                    'name'      => 'titles['.$translation->getCrowdinPath().']',
78
                    'contents'  => $translation->getTitle()
79
                ];
80
            }
81
            if ($translation->getExportPattern()) {
82
                $data[] = [
83
                    'name'      => 'export_patterns['.$translation->getCrowdinPath().']',
84
                    'contents'  => $translation->getExportPattern()
85
                ];
86
            }
87
        }
88
89
        $data = ['multipart' => $data];
90
        $response = $this->client->getHttpClient()->post($path, $data);
91
92
        return $response->getBody();
93
    }
94
95
    /**
96
     * @param string $localPath
97
     * @param string $crowdinPath
98
     * @param string $exportPattern
99
     * @param string $title
100
     *
101
     * @return $this
102
     */
103 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...
104
    {
105
        $translation = new Translation($localPath, $crowdinPath);
106
        $translation->setExportPattern($exportPattern);
107
        $translation->setTitle($title);
108
109
        $this->translations[] = $translation;
110
111
        return $this;
112
    }
113
114
    /**
115
     * @return Translation[]
116
     */
117
    public function getTranslations()
118
    {
119
        return $this->translations;
120
    }
121
122
    /**
123
     * @return string
124
     */
125
    public function getType()
126
    {
127
        return $this->type;
128
    }
129
130
    /**
131
     * @param string $type
132
     *
133
     * @return AddFile
134
     */
135
    public function setType($type)
136
    {
137
        $this->type = $type;
138
139
        return $this;
140
    }
141
142
    /**
143
     * @return string|null
144
     */
145
    public function getBranch()
146
    {
147
        return $this->branch;
148
    }
149
150
    /**
151
     * @param string $branch
152
     *
153
     * @return AddFile
154
     */
155
    public function setBranch($branch)
156
    {
157
        $this->branch = $branch;
158
159
        return $this;
160
    }
161
}
162