Order::getClientId()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * This file is part of the Amplexor\XConnect library
4
 *
5
 * @license http://opensource.org/licenses/MIT
6
 * @link https://github.com/amplexor-drupal/xconnect/
7
 * @version 1.0.0
8
 * @package Amplexor.XConnect
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Amplexor\XConnect\Request;
15
16
/**
17
 * Class that represents the X-Connect order.
18
 */
19
class Order
20
{
21
    /**
22
     * The order name.
23
     *
24
     * @var string
25
     */
26
    private $orderName;
27
28
    /**
29
     * The source language.
30
     *
31
     * @var string
32
     */
33
    private $sourceLanguage;
34
35
    /**
36
     * The target languages.
37
     *
38
     * @var array
39
     */
40
    private $targetLanguages = array();
41
42
    /**
43
     * The order instruction.
44
     *
45
     * @var array
46
     */
47
    private $instructions = array();
48
49
    /**
50
     * The client reference.
51
     *
52
     * @var string
53
     */
54
    private $reference;
55
56
    /**
57
     * The configuration.
58
     *
59
     * @var array
60
     */
61
    private $config = array(
62
        'clientId'          => '',
63
        'orderNamePrefix'   => '',
64
        'templateId'        => '',
65
        'dueDate'           => 0,
66
        'issuedBy'          => '',
67
        'isConfidential'    => false,
68
        'needsConfirmation' => true,
69
        'needsQuotation'    => false,
70
    );
71
72
    /**
73
     * Array of files that are part of the order.
74
     *
75
     * @var array
76
     */
77
    private $files = array();
78
79
80
    /**
81
     * Create a new order.
82
     *
83
     * @param string $source_language
84
     *   The source language for the translation order.
85
     * @param array $config
86
     *   The configuration elements for the order:
87
     *   - clientId : The client ID to order the translations for.
88
     *   - orderNamePrefix : The order gets by default the following name:
89
     *     translation_order_<date as YmdHis><microseconds as 3 digits>.
90
     *     You can override the "translation_order_" with your own prefix.
91
     *     (optional).
92
     *   - templateId : The translation template ID. (optional).
93
     *   - dueDate : What is the deadline for the file(s) to be translated.
94
     *     The deadline should be set in days from the moment the translation
95
     *     is ordered. (optional, default 0).
96
     *   - issuedBy : The email address of the, by the translation known, issuer
97
     *     of the translation.
98
     *   - isConfidential : Is the content for the translation confidential?
99
     *     (optional, default false).
100
     *   - needsConfirmation : Should there be a conformation send when the
101
     *     translation is ready? (optional, default true).
102
     *   - needsQuotation : Should a quotation be created and send before the
103
     *     translation is performed? (optional, default false).
104
     */
105 60
    public function __construct($source_language, $config = array())
106
    {
107 60
        $this->sourceLanguage = $source_language;
108 60
        $this->setConfig($config);
109
110 60
        $this->orderName = $this->createOrderName();
111 60
    }
112
113
    /**
114
     * Get the source language.
115
     *
116
     * @return string
117
     *   The source language of the translation.
118
     */
119 6
    public function getSourceLanguage()
120
    {
121 6
        return $this->sourceLanguage;
122
    }
123
124
    /**
125
     * Helper function to generate the order name when the object is created.
126
     *
127
     * @return string
128
     */
129 60
    protected function createOrderName()
130
    {
131 60
        $prefix = !empty($this->config['orderNamePrefix'])
132 60
            ? $this->config['orderNamePrefix']
133 60
            : 'translation_order';
134
135
        // Add timestamp.
136 60
        $timestamp = new \DateTime();
137
138
        // Get a 3-digit microsecond representation.
139 60
        $microseconds = floor((substr((string) microtime(), 1, 8) * 1000));
140 60
        $microseconds = str_pad($microseconds, 3, '0', STR_PAD_LEFT);
141
142 60
        return sprintf(
143 60
            '%s_%s%s',
144 60
            $prefix,
145 60
            $timestamp->format('YmdHis'),
146
            $microseconds
147 60
        );
148
    }
149
150
    /**
151
     * Fill in the configuration based on the given config array.
152
     *
153
     * @param array $config
154
     *   The config array to store.
155
     */
156 60
    protected function setConfig($config)
157
    {
158 60
        $config_keys = array_keys($this->config);
159 60
        foreach ($config_keys as $key) {
160 60
            if (isset($config[$key])) {
161 27
                $this->config[$key] = $config[$key];
162 27
            }
163 60
        }
164 60
    }
165
166
    /**
167
     * Get the order name.
168
     *
169
     * @return string
170
     *   The order name.
171
     */
172 3
    public function getOrderName()
173
    {
174 3
        return $this->orderName;
175
    }
176
177
    /**
178
     * Get the client id.
179
     *
180
     * @return string
181
     *   The client ID.
182
     */
183 6
    public function getClientId()
184
    {
185 6
        return $this->config['clientId'];
186
    }
187
188
    /**
189
     * Get the template id.
190
     *
191
     * @return string
192
     *   The template ID.
193
     */
194 3
    public function getTemplateId()
195
    {
196 3
        return $this->config['templateId'];
197
    }
198
199
    /**
200
     * Get the request date.
201
     *
202
     * @return DateTime
203
     *   The date time presentation of the request date.
204
     */
205 3
    public function getRequestDate()
206
    {
207 3
        $date = new \DateTime();
208 3
        return $date;
209
    }
210
211
    /**
212
     * Get the request due date.
213
     *
214
     * The date is calculated based on the dueDate value in the settings.
215
     *
216
     * @return DateTime
217
     *   The date time presentation of the due date.
218
     */
219 3
    public function getDueDate()
220
    {
221
        // When no due date interval given, use today.
222 3
        $dueDate = new \DateTime();
223 3
        if (empty($this->config['dueDate'])) {
224 3
            return $dueDate;
225
        }
226
227
        // Calculate the date by adding the dueDate interval.
228 3
        $interval = new \DateInterval(
229 3
            'P' . (int) $this->config['dueDate'] . 'D'
230 3
        );
231 3
        $dueDate->add($interval);
232
233 3
        return $dueDate;
234
    }
235
236
    /**
237
     * Get the issuer identifier.
238
     *
239
     * @return string
240
     *   The email address.
241
     */
242 3
    public function getIssuedBy()
243
    {
244 3
        return $this->config['issuedBy'];
245
    }
246
247
    /**
248
     * Get if the translation content is confidential.
249
     *
250
     * @return bool
251
     *   Translation vcontent is confidential true/false.
252
     */
253 3
    public function isConfidential()
254
    {
255 3
        return (bool) $this->config['isConfidential'];
256
    }
257
258
    /**
259
     * Add a target language.
260
     *
261
     * @param string $language
262
     *   The target language to add to the order.
263
     */
264 6
    public function addTargetLanguage($language)
265
    {
266 6
        if (!in_array($language, $this->targetLanguages)) {
267 6
            $this->targetLanguages[] = $language;
268 6
        }
269 6
    }
270
271
    /**
272
     * Get the target languages.
273
     *
274
     * @return array
275
     *   The target language codes.
276
     */
277 6
    public function getTargetLanguages()
278
    {
279 6
        return $this->targetLanguages;
280
    }
281
282
    /**
283
     * Add instruction to the order.
284
     *
285
     * @param string $instruction
286
     *   The instruction.
287
     */
288 6
    public function addInstruction($instruction)
289
    {
290 6
        $this->instructions[] = $instruction;
291 6
    }
292
293
    /**
294
     * Get the client instruction.
295
     *
296
     * @return string
297
     *   The client instruction regarding the translation.
298
     */
299 6
    public function getInstructions()
300
    {
301 6
        return $this->instructions;
302
    }
303
304
    /**
305
     * Set the client reference.
306
     *
307
     * @param string $reference
308
     *   The reference to use in the order.
309
     */
310 6
    public function setReference($reference)
311
    {
312 6
        $this->reference = $reference;
313 6
    }
314
315
    /**
316
     * Get the client reference.
317
     *
318
     * @return string
319
     *   The client reference.
320
     */
321 6
    public function getReference()
322
    {
323 6
        return $this->reference;
324
    }
325
326
    /**
327
     * Is there a confirmation needed before the translation may be processed.
328
     *
329
     * @return bool
330
     *   Needs confirmation true/false.
331
     */
332 3
    public function needsConfirmation()
333
    {
334 3
        return (bool) $this->config['needsConfirmation'];
335
    }
336
337
    /**
338
     * Get if an quotation is required before translation is processed.
339
     *
340
     * @return bool
341
     *   Needs quotation true/false.
342
     */
343 3
    public function needsQuotation()
344
    {
345 3
        return (bool) $this->config['needsQuotation'];
346
    }
347
348
    /**
349
     * Add an input file to the order.
350
     *
351
     * @param string $file_name
352
     *   The file name of the file to add to the order.
353
     */
354 9
    public function addFile($file_name)
355
    {
356 9
        if (!in_array($file_name, $this->files)) {
357 9
            $this->files[] = $file_name;
358 9
        }
359 9
    }
360
361
    /**
362
     * Get the input files.
363
     *
364
     * @return array
365
     *   The input file names part of the translation order.
366
     */
367 9
    public function getFiles()
368
    {
369 9
        return $this->files;
370
    }
371
}
372