Failed Conditions
Push — master ( 55bfad...b74acd )
by Maximo
02:48
created

CustomFieldsModulesController::onConstruct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 7
ccs 4
cts 5
cp 0.8
crap 2.032
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gewaer\Api\Controllers;
6
7
use Gewaer\Models\CustomFieldsModules;
8
use Gewaer\CustomFields\CustomFields;
9
use Phalcon\Http\Response;
10
use Gewaer\Exception\NotFoundHttpException;
11
use Gewaer\Exception\PermissionException;
12
13
/**
14
 * Class LanguagesController
15
 *
16
 * @package Gewaer\Api\Controllers
17
 * @property Users $userData
18
 * @property Apps $app
19
 *
20
 */
21
class CustomFieldsModulesController extends BaseController
22
{
23
    /*
24
     * fields we accept to create
25
     *
26
     * @var array
27
     */
28
    protected $createFields = ['name'];
29
30
    /*
31
     * fields we accept to create
32
     *
33
     * @var array
34
     */
35
    protected $updateFields = ['name'];
36
37
    /**
38
     * set objects
39
     *
40
     * @return void
41
     */
42 3
    public function onConstruct()
43
    {
44 3
        $this->model = new CustomFieldsModules();
45 3
        $this->model->apps_id = $this->app->getId();
46
47 3
        if (!$this->userData->hasRole('Defaults.Admins')) {
48
            throw new PermissionException('You dont have permission to run this action ');
49
        }
50 3
    }
51
52
    /**
53
     * Fetch all Custom Fields of a Module
54
     * @param integer $id
55
     * @return Response
56
     */
57 1
    public function customFieldsByModulesId(int $id): Response
58
    {
59
        //Verify that module exists
60 1
        $module = $this->model::findFirst([
61 1
            'conditions' => 'id = ?0 and apps_id = ?1 and is_deleted = 0',
62 1
            'bind' => [$id, $this->app->getId()]
63
        ]);
64
65 1
        if (!is_object($module)) {
66
            throw new NotFoundHttpException('Module not found');
67
        }
68
69
        //List all Custom Fields by module_id, apps and companies
70 1
        $customFields = CustomFields::find([
71 1
            'conditions' => 'companies_id = ?0 and apps_id = ?1 and is_deleted = 0',
72 1
            'bind' => [$this->userData->default_company, $this->app->getId()]
73
        ]);
74
75 1
        if (!is_object($customFields)) {
76
            throw new NotFoundHttpException('Custom Fields not found');
77
        }
78
79 1
        return $this->response($customFields);
80
    }
81
}
82