Test Failed
Pull Request — master (#20)
by
unknown
03:25
created

ModulesController::onConstruct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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