Test Failed
Pull Request — master (#144)
by Maximo
05:16
created

CustomFieldsController::processInput()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 8
ccs 0
cts 6
cp 0
crap 12
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Canvas\Api\Controllers;
6
7
use Canvas\CustomFields\CustomFields;
8
9
/**
10
 * Class LanguagesController.
11
 *
12
 * @package Canvas\Api\Controllers
13
 * @property Users $userData
14
 * @property Apps $app
15
 *
16
 */
17
class CustomFieldsController extends BaseController
18
{
19
    /*
20
     * fields we accept to create
21
     *
22
     * @var array
23
     */
24
    protected $createFields = ['name', 'label', 'custom_fields_modules_id', 'fields_type_id', 'attributes'];
25
26
    /*
27
     * fields we accept to create
28
     *
29
     * @var array
30
     */
31
    protected $updateFields = ['name', 'label', 'custom_fields_modules_id', 'fields_type_id', 'attributes'];
32
33
    /**
34
     * set objects.
35
     *
36
     * @return void
37
     */
38
    public function onConstruct()
39
    {
40
        $this->model = new CustomFields();
41
        $this->model->users_id = $this->userData->getId();
42
        $this->model->companies_id = $this->userData->currentCompanyId();
43
        $this->model->apps_id = $this->app->getId();
44
45
        $this->additionalSearchFields = [
46
            ['apps_id', ':', $this->app->getId()],
47
            ['companies_id', ':', $this->userData->currentCompanyId()],
48
        ];
49
    }
50
51
    /**
52
     * Process the input data.
53
     *
54
     * @param array $request
55
     * @return array
56
     */
57
    protected function processInput(array $request): array
58
    {
59
        //encode the attribute field from #teamfrontend
60
        if (!empty($request['attributes']) && is_array($request['attributes'])) {
61
            $request['attributes'] = json_encode($request['attributes']);
62
        }
63
64
        return $request;
65
    }
66
    
67
    /**
68
     * Format output
69
     *
70
     * @param mixed $results
71
     * @return mixed
72
     */
73
    public function processOutput($results)
74
    {
75
        //decode json to format output
76
        if (isset($results->attributes) && !empty($results->attributes)) {
77
            $results->attributes = json_decode($results->attributes);
78
        }
79
80
        return $results;
81
    }
82
}
83