ModuleRecordFileField   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
c 2
b 0
f 0
lcom 1
cbo 2
dl 0
loc 60
ccs 22
cts 22
cp 1
rs 10

2 Methods

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