Completed
Push — master ( fd48c6...d6eea8 )
by Mike
02:42
created

RecordFileField::configureData()   C

Complexity

Conditions 14
Paths 28

Size

Total Lines 30
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 30
rs 5.0864
cc 14
eloc 22
nc 28
nop 1

How to fix   Complexity   

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 SugarAPI\SDK\EntryPoint\POST;
4
5
use SugarAPI\SDK\EntryPoint\Abstracts\POST\FileEntryPoint as POSTFileEntryPoint;
6
7
class RecordFileField extends POSTFileEntryPoint {
8
9
    protected $_URL = '$module/$record/file/$field';
10
11
    /**
12
     * @inheritdoc
13
     */
14
    protected $_REQUIRED_DATA = array(
15
        'format',
16
        'delete_if_fails',
17
        'oauth_token'
18
    );
19
20
    /**
21
     * Allow for shorthand calling of attachFile method.
22
     * Users can simply submit the File in via string, or pass the filename => path. Options must be configured before shorthand works completely
23
     * @param mixed $data
24
     * @return array|mixed
25
     */
26
    protected function configureData($data){
27
        if (!empty($this->Options)){
28
            $fileField = end($this->Options);
29
        }
30
        if (is_string($data) && isset($fileField)){
31
            $data = array(
32
                $fileField => $data
33
            );
34
        }
35
        if (is_array($data)) {
36
            if (isset($fileField)) {
37
                $data[$fileField] = $this->setFileFieldValue($data[$fileField]);
38
            }else {
39
                foreach ($data as $key => $value) {
40
                    if (strtolower($key) !== 'oauth_token' || strtolower($key) !== 'delete_if_fails' || strtolower($key) !== 'format') {
41
                        $data[$key] = $this->setFileFieldValue($value);
42
                    }
43
                }
44
            }
45
            $data['oauth_token'] = $this->accessToken;
46
            $data['delete_if_fails'] = (isset($data['delete_if_fails']) ? $data['delete_if_fails'] : TRUE);
47
            $data['format'] = 'sugar-html-json';
48
        }elseif (is_object($data) && isset($fileField)){
49
            $data->$fileField = $this->setFileFieldValue($data->$fileField);
50
            $data->oauth_token = $this->accessToken;
51
            $data->delete_if_fails = (isset($data->delete_if_fails) ? $data->delete_if_fails : TRUE);
52
            $data->format = 'sugar-html-json';
53
        }
54
        return $data;
55
    }
56
57
    /**
58
     * Configure the filepath to have an @ symbol in front if one is not found
59
     * @param string $value
60
     * @return string
61
     */
62
    protected function setFileFieldValue($value){
63
        if (strpos($value, '@') === FALSE) {
64
            $value = '@' . $value;
65
        }
66
        return $value;
67
    }
68
69
}