Completed
Push — master ( c7c88d...7873ee )
by Mike
02:24
created

ModuleRecordFileField::configureData()   C

Complexity

Conditions 8
Paths 7

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 24
rs 5.7377
cc 8
eloc 16
nc 7
nop 1
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 (!empty($this->Options)){
36
            $fileField = end($this->Options);
37
        }else{
38
            throw new RequiredOptionsException(get_called_class());
0 ignored issues
show
Bug introduced by
The call to RequiredOptionsException::__construct() misses a required argument $data.

This check looks for function calls that miss required arguments.

Loading history...
39
        }
40
        if (is_string($data) && isset($fileField)){
41
            $data = array(
42
                $fileField => $data
43
            );
44
        }
45
        if (is_array($data)){
46
            if (isset($fileField)){
47
                $data[$fileField] = $this->setFileFieldValue($data[$fileField]);
48
            }else{
49
                foreach ($this->Data as $key => $value){
50
                    if (!array_key_exists($key,$this->_REQUIRED_DATA)){
51
                        $data[$key] = $this->setFileFieldValue($value);
52
                    }
53
                }
54
            }
55
        }
56
        parent::configureData($data);
57
    }
58
59
    /**
60
     * Configure the filepath to have an @ symbol in front if one is not found
61
     * @param string $value
62
     * @return string
63
     */
64
    protected function setFileFieldValue($value){
65
        if (strpos($value, '@')===FALSE){
66
            $value = '@'.$value;
67
        }
68
        return $value;
69
    }
70
71
}