Test Failed
Push — master ( d9b525...db8114 )
by Mike
02:47
created

ModuleRecordFileField::setFileFieldValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 0
cts 2
cp 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 6
1
<?php
2
3
namespace SugarAPI\SDK\EntryPoint\POST;
4
5
use SugarAPI\SDK\EntryPoint\Abstracts\POST\AbstractPostFileEntryPoint;
6
use SugarAPI\SDK\Exception\EntryPoint\RequiredOptionsException;
7
8
class ModuleRecordFileField extends AbstractPostFileEntryPoint {
9
10
    /**
11
     * @inheritdoc
12
     */
13
    protected $_URL = '$module/$record/file/$field';
14
15
    /**
16
     * @inheritdoc
17
     */
18
    protected $_DATA_TYPE = 'array';
19
20
    /**
21
     * @inheritdoc
22
     */
23
    protected $_REQUIRED_DATA = array(
24
        'format' => 'sugar-html-json',
25
        'delete_if_fails' => FALSE
26
    );
27
28
    /**
29
     * Allow for shorthand calling of attachFile method.
30
     * Users can simply submit the File in via string, or pass the filename => path
31
     * @param $data
32
     * @throws RequiredOptionsException
33
     */
34
    protected function configureData($data){
35
        if (is_string($data)) {
36
            if (!empty($this->Options)) {
37
                $fileField = end($this->Options);
38
                $data = array(
39
                    $fileField => $data
40
                );
41
            } else {
42
                throw new RequiredOptionsException(get_called_class(), "Options are required, when passing String for data.");
43
            }
44
        }
45
        if (is_array($data)){
46
            foreach ($data as $key => $value){
47
                if (!array_key_exists($key,$this->_REQUIRED_DATA)){
48
                    $data[$key] = $this->setFileFieldValue($value);
49
                }
50
            }
51
        }
52
        parent::configureData($data);
53
    }
54
55
    /**
56
     * Configure the filepath to have an @ symbol in front if one is not found
57
     * @param string $value
58
     * @return string
59
     */
60
    protected function setFileFieldValue($value){
61
        if (strpos($value, '@')===FALSE){
62
            $value = '@'.$value;
63
        }
64
        return $value;
65
    }
66
67
}