Passed
Branch development (0505ff)
by Jonathan
08:42
created

PostTrait   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 240
Duplicated Lines 9.58 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 25
c 0
b 0
f 0
lcom 0
cbo 3
dl 23
loc 240
rs 10
ccs 115
cts 115
cp 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
B uploadTemplate() 0 30 3
B convertDocument() 0 30 3
C mergeDocument() 13 68 11
B findAndReplaceDocument() 10 59 8

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * ReportingCloud PHP Wrapper
5
 *
6
 * PHP wrapper for ReportingCloud Web API. Authored and supported by Text Control GmbH.
7
 *
8
 * @link      http://www.reporting.cloud to learn more about ReportingCloud
9
 * @link      https://github.com/TextControl/txtextcontrol-reportingcloud-php for the canonical source repository
10
 * @license   https://raw.githubusercontent.com/TextControl/txtextcontrol-reportingcloud-php/master/LICENSE.md
11
 * @copyright © 2018 Text Control GmbH
12
 */
13
14
namespace TxTextControl\ReportingCloud;
15
16
use GuzzleHttp\Psr7\Response;
17
use GuzzleHttp\RequestOptions;
18
use TxTextControl\ReportingCloud\Exception\InvalidArgumentException;
19
use TxTextControl\ReportingCloud\Filter\StaticFilter;
20
use TxTextControl\ReportingCloud\Validator\StaticValidator;
21
22
trait PostTrait
23
{
24
    /**
25
     * Upload a template to template storage
26
     *
27
     * @param string $templateFilename Template name
28
     *
29
     * @throws InvalidArgumentException
30
     *
31
     * @return bool
32
     */
33 13
    public function uploadTemplate($templateFilename)
34
    {
35 13
        $ret = false;
36
37 13
        StaticValidator::execute($templateFilename, 'TemplateExtension');
38 10
        StaticValidator::execute($templateFilename, 'FileExists');
39
40 9
        $templateFilename = realpath($templateFilename);
41 9
        $templateName     = basename($templateFilename);
42
43
        $query = [
44 9
            'templateName' => $templateName,
45 9
        ];
46
47 9
        $json = file_get_contents($templateFilename);
48 9
        $json = base64_encode($json);
49
50
        $options = [
51 9
            RequestOptions::QUERY => $query,
52 9
            RequestOptions::JSON  => $json,
53 9
        ];
54
55 9
        $response = $this->request('POST', $this->uri('/templates/upload'), $options);
0 ignored issues
show
Bug introduced by
It seems like uri() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
Bug introduced by
It seems like request() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
56
57 9
        if ($response instanceof Response && 201 === $response->getStatusCode()) {
58 9
            $ret = true;
59 9
        }
60
61 9
        return $ret;
62
    }
63
64
    /**
65
     * Convert a document on the local file system to a different format
66
     *
67
     * @param string $documentFilename Document filename
68
     * @param string $returnFormat     Return format
69
     *
70
     * @throws InvalidArgumentException
71
     *
72
     * @return null|resource
73
     */
74 6
    public function convertDocument($documentFilename, $returnFormat)
75
    {
76 6
        $ret = null;
77
78 6
        StaticValidator::execute($documentFilename, 'DocumentExtension');
79 3
        StaticValidator::execute($documentFilename, 'FileExists');
80 2
        StaticValidator::execute($returnFormat, 'ReturnFormat');
81
82
        $query = [
83 1
            'returnFormat' => $returnFormat,
84 1
        ];
85
86 1
        $documentFilename = realpath($documentFilename);
87
88 1
        $json = file_get_contents($documentFilename);
89 1
        $json = base64_encode($json);
90
91
        $options = [
92 1
            RequestOptions::QUERY => $query,
93 1
            RequestOptions::JSON  => $json,
94 1
        ];
95
96 1
        $response = $this->request('POST', $this->uri('/document/convert'), $options);
0 ignored issues
show
Bug introduced by
It seems like uri() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
Bug introduced by
It seems like request() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
97
98 1
        if ($response instanceof Response && 200 === $response->getStatusCode()) {
99 1
            $ret = base64_decode($response->getBody());
100 1
        }
101
102 1
        return $ret;
103
    }
104
105
    /**
106
     * Merge data into a template and return an array of binary data.
107
     * Each record in the array is the binary data of one document
108
     *
109
     * @param array  $mergeData        Array of merge data
110
     * @param string $returnFormat     Return format
111
     * @param string $templateName     Template name
112
     * @param string $templateFilename Template filename on local file system
113
     * @param bool   $append           Append flag
114
     * @param array  $mergeSettings    Array of merge settings
115
     *
116
     * @throws InvalidArgumentException
117
     *
118
     * @return null|string
119
     */
120 14
    public function mergeDocument(
121
        $mergeData,
122
        $returnFormat,
123
        $templateName = null,
124
        $templateFilename = null,
125
        $append = null,
126
        $mergeSettings = []
127
    ) {
128 14
        $ret = null;
129
130 14
        StaticValidator::execute($mergeData, 'TypeArray');
131 14
        StaticValidator::execute($returnFormat, 'ReturnFormat');
132
133 13
        if (null !== $templateName) {
134 2
            StaticValidator::execute($templateName, 'TemplateName');
135 1
        }
136
137 12 View Code Duplication
        if (null !== $templateFilename) {
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...
138 11
            StaticValidator::execute($templateFilename, 'TemplateExtension');
139 8
            StaticValidator::execute($templateFilename, 'FileExists');
140 7
            $templateFilename = realpath($templateFilename);
141 7
        }
142
143 8
        if (null !== $append) {
144 7
            $append = StaticFilter::execute($append, 'BooleanToString');
145 6
        }
146
147 7
        StaticValidator::execute($mergeSettings, 'TypeArray');
148
149
        $query = [
150 6
            'returnFormat' => $returnFormat,
151 6
            'append'       => $append,
152 6
        ];
153
154 6
        if (null !== $templateName) {
155 1
            $query['templateName'] = $templateName;
156 1
        }
157
158
        $json = [
159 6
            'mergeData' => $mergeData,
160 6
        ];
161
162 6 View Code Duplication
        if (null !== $templateFilename) {
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...
163 5
            $template         = file_get_contents($templateFilename);
164 5
            $template         = base64_encode($template);
165 5
            $json['template'] = $template;
166 5
        }
167
168 6
        if (count($mergeSettings) > 0) {
169 5
            $json['mergeSettings'] = $this->buildMergeSettingsArray($mergeSettings);
0 ignored issues
show
Bug introduced by
It seems like buildMergeSettingsArray() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
170 2
        }
171
172
        $options = [
173 3
            RequestOptions::QUERY => $query,
174 3
            RequestOptions::JSON  => $json,
175 3
        ];
176
177 3
        $response = $this->request('POST', $this->uri('/document/merge'), $options);
0 ignored issues
show
Bug introduced by
It seems like uri() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
Bug introduced by
It seems like request() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
178
179 3
        if ($response instanceof Response && 200 === $response->getStatusCode()) {
180 3
            $body = json_decode($response->getBody(), true);
181 3 View Code Duplication
            if (is_array($body) && count($body) > 0) {
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...
182 3
                $ret = array_map('base64_decode', $body);
183 3
            }
184 3
        }
185
186 3
        return $ret;
187
    }
188
189
    /**
190
     * Perform find and replace in document and return binary data.
191
     *
192
     * @param array  $findAndReplaceData Array of find and replace data
193
     * @param string $returnFormat       Return format
194
     * @param string $templateName       Template name
195
     * @param string $templateFilename   Template filename on local file system
196
     * @param array  $mergeSettings      Array of merge settings
197
     *
198
     * @throws InvalidArgumentException
199
     *
200
     * @return null|string
201
     */
202 11
    public function findAndReplaceDocument(
203
        $findAndReplaceData,
204
        $returnFormat,
205
        $templateName = null,
206
        $templateFilename = null,
207
        $mergeSettings = []
208
    ) {
209 11
        $ret = null;
210
211 11
        StaticValidator::execute($findAndReplaceData, 'TypeArray');
212 11
        StaticValidator::execute($returnFormat, 'ReturnFormat');
213
214 10
        if (null !== $templateName) {
215 2
            StaticValidator::execute($templateName, 'TemplateName');
216 1
        }
217
218 9 View Code Duplication
        if (null !== $templateFilename) {
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...
219 8
            StaticValidator::execute($templateFilename, 'TemplateExtension');
220 5
            StaticValidator::execute($templateFilename, 'FileExists');
221 4
            $templateFilename = realpath($templateFilename);
222 4
        }
223
224 5
        StaticValidator::execute($mergeSettings, 'TypeArray');
225
226
        $query = [
227 4
            'returnFormat' => $returnFormat,
228 4
        ];
229
230 4
        if (null !== $templateName) {
231 1
            $query['templateName'] = $templateName;
232 1
        }
233
234
        $json = [
235 4
            'findAndReplaceData' => $this->buildFindAndReplaceDataArray($findAndReplaceData),
0 ignored issues
show
Bug introduced by
It seems like buildFindAndReplaceDataArray() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
236 4
        ];
237
238 4 View Code Duplication
        if (null !== $templateFilename) {
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...
239 3
            $template         = file_get_contents($templateFilename);
240 3
            $template         = base64_encode($template);
241 3
            $json['template'] = $template;
242 3
        }
243
244 4
        if (count($mergeSettings) > 0) {
245 4
            $json['mergeSettings'] = $this->buildMergeSettingsArray($mergeSettings);
0 ignored issues
show
Bug introduced by
It seems like buildMergeSettingsArray() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
246 2
        }
247
248
        $options = [
249 2
            RequestOptions::QUERY => $query,
250 2
            RequestOptions::JSON  => $json,
251 2
        ];
252
253 2
        $response = $this->request('POST', $this->uri('/document/findandreplace'), $options);
0 ignored issues
show
Bug introduced by
It seems like uri() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
Bug introduced by
It seems like request() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
254
255 2
        if ($response instanceof Response && 200 === $response->getStatusCode()) {
256 2
            $ret = base64_decode($response->getBody());
257 2
        }
258
259 2
        return $ret;
260
    }
261
}
262