Test Failed
Branch add-core-tests (a060d8)
by Rafael
05:48
created

CustomFieldsController   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 5
Bugs 1 Features 1
Metric Value
eloc 28
dl 0
loc 104
ccs 0
cts 43
cp 0
rs 10
c 5
b 1
f 1
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A processEdit() 0 11 2
A onConstruct() 0 10 1
A processOutput() 0 8 2
A processCreate() 0 11 2
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
use Phalcon\Mvc\ModelInterface;
0 ignored issues
show
Bug introduced by
The type Phalcon\Mvc\ModelInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Phalcon\Http\Request;
10
use Canvas\Dto\CustomFields as CustomFieldsDto;
11
use Canvas\Mapper\CustomFieldsMapper;
12
13
/**
14
 * Class LanguagesController.
15
 *
16
 * @package Canvas\Api\Controllers
17
 * @property Users $userData
18
 * @property Apps $app
19
 *
20
 */
21
class CustomFieldsController extends BaseController
22
{
23
    /*
24
     * fields we accept to create
25
     *
26
     * @var array
27
     */
28
    protected $createFields = ['name', 'label', 'custom_fields_modules_id', 'fields_type_id', 'attributes'];
29
30
    /*
31
     * fields we accept to create
32
     *
33
     * @var array
34
     */
35
    protected $updateFields = ['name', 'label', 'custom_fields_modules_id', 'fields_type_id', 'attributes'];
36
37
    /**
38
     * set objects.
39
     *
40
     * @return void
41
     */
42
    public function onConstruct()
43
    {
44
        $this->model = new CustomFields();
45
        $this->model->users_id = $this->userData->getId();
46
        $this->model->companies_id = $this->userData->currentCompanyId();
47
        $this->model->apps_id = $this->app->getId();
48
49
        $this->additionalSearchFields = [
50
            ['apps_id', ':', $this->app->getId()],
51
            ['companies_id', ':', $this->userData->currentCompanyId()],
52
        ];
53
    }
54
55
    /**
56
     * Process the input data.
57
     *
58
     * @param array $request
59
     * @return array
60
     */
61
    protected function processInput(array $request): array
62
    {
63
        //encode the attribute field from #teamfrontend
64
        if (!empty($request['attributes']) && is_array($request['attributes'])) {
65
            $request['attributes'] = json_encode($request['attributes']);
66
        }
67
68
        return $request;
69
    }
70
71
    /**
72
     * Pass the resultset to a DTO Mapper.
73
     *
74
     * @param mixed $results
75
     * @return void
76
     */
77
    protected function processOutput($results)
78
    {
79
        $this->dtoConfig->registerMapping(CustomFields::class, CustomFieldsDto::class)
80
            ->useCustomMapper(new CustomFieldsMapper());
81
82
        return is_iterable($results) ?
83
        $this->mapper->mapMultiple($results, CustomFieldsDto::class)
84
        : $this->mapper->map($results, CustomFieldsDto::class);
85
    }
86
87
    /**
88
    * Process the create request and trecurd the boject.
89
    *
90
    * @return ModelInterface
91
    * @throws Exception
92
    */
93
    protected function processCreate(Request $request): ModelInterface
94
    {
95
        $model = parent::processCreate($request);
96
        $request = $request->getPostData();
97
98
        //add values to the custom field
99
        if (is_array($request['values'])) {
100
            $model->addValues($request['values']);
101
        }
102
103
        return $model;
104
    }
105
106
    /**
107
     * Process the update request and return the object.
108
     *
109
     * @param Request $request
110
     * @param ModelInterface $record
111
     * @throws Exception
112
     * @return ModelInterface
113
     */
114
    protected function processEdit(Request $request, ModelInterface $record): ModelInterface
115
    {
116
        //process the input
117
        $record = parent::processEdit($request, $record);
118
        $request = $request->getPostData();
119
120
        //add values to the custom field
121
        if (is_array($request['values'])) {
122
            $record->addValues($request['values']);
123
        }
124
        return $record;
125
    }
126
}
127