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

CustomFieldsController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 16
c 2
b 0
f 0
dl 0
loc 64
ccs 0
cts 24
cp 0
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A onConstruct() 0 10 1
A processOutput() 0 8 3
A processInput() 0 8 3
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