Test Failed
Pull Request — master (#160)
by Maximo
07:12
created

CustomFieldsController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 5
Bugs 1 Features 1
Metric Value
eloc 26
c 5
b 1
f 1
dl 0
loc 93
ccs 0
cts 37
cp 0
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A processEdit() 0 11 2
A onConstruct() 0 13 1
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
use Canvas\Contracts\Controllers\ProcessOutputMapperTrait;
13
14
/**
15
 * Class LanguagesController.
16
 *
17
 * @package Canvas\Api\Controllers
18
 * @property Users $userData
19
 * @property Apps $app
20
 *
21
 */
22
class CustomFieldsController extends BaseController
23
{
24
    use ProcessOutputMapperTrait;
25
    
26
    /*
27
     * fields we accept to create
28
     *
29
     * @var array
30
     */
31
    protected $createFields = ['name', 'label', 'custom_fields_modules_id', 'fields_type_id', 'attributes'];
32
33
    /*
34
     * fields we accept to create
35
     *
36
     * @var array
37
     */
38
    protected $updateFields = ['name', 'label', 'custom_fields_modules_id', 'fields_type_id', 'attributes'];
39
40
    /**
41
     * set objects.
42
     *
43
     * @return void
44
     */
45
    public function onConstruct()
46
    {
47
        $this->model = new CustomFields();
48
        $this->dto = CustomFieldsDto::class;
49
        $this->dtoMapper = new CustomFieldsMapper();
50
51
        $this->model->users_id = $this->userData->getId();
52
        $this->model->companies_id = $this->userData->currentCompanyId();
53
        $this->model->apps_id = $this->app->getId();
54
55
        $this->additionalSearchFields = [
56
            ['apps_id', ':', $this->app->getId()],
57
            ['companies_id', ':', $this->userData->currentCompanyId()],
58
        ];
59
    }
60
61
    /**
62
     * Process the input data.
63
     *
64
     * @param array $request
65
     * @return array
66
     */
67
    protected function processInput(array $request): array
68
    {
69
        //encode the attribute field from #teamfrontend
70
        if (!empty($request['attributes']) && is_array($request['attributes'])) {
71
            $request['attributes'] = json_encode($request['attributes']);
72
        }
73
74
        return $request;
75
    }
76
77
    /**
78
    * Process the create request and trecurd the boject.
79
    *
80
    * @return ModelInterface
81
    * @throws Exception
82
    */
83
    protected function processCreate(Request $request): ModelInterface
84
    {
85
        $model = parent::processCreate($request);
86
        $request = $request->getPostData();
87
88
        //add values to the custom field
89
        if (is_array($request['values'])) {
90
            $model->addValues($request['values']);
91
        }
92
93
        return $model;
94
    }
95
96
    /**
97
     * Process the update request and return the object.
98
     *
99
     * @param Request $request
100
     * @param ModelInterface $record
101
     * @throws Exception
102
     * @return ModelInterface
103
     */
104
    protected function processEdit(Request $request, ModelInterface $record): ModelInterface
105
    {
106
        //process the input
107
        $record = parent::processEdit($request, $record);
108
        $request = $request->getPostData();
109
110
        //add values to the custom field
111
        if (is_array($request['values'])) {
112
            $record->addValues($request['values']);
113
        }
114
        return $record;
115
    }
116
}
117