SchemaController::getBySlug()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 9
ccs 0
cts 6
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Canvas\Api\Controllers;
6
7
use Canvas\Models\SystemModules;
8
use Phalcon\Http\Response;
0 ignored issues
show
Bug introduced by
The type Phalcon\Http\Response 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 Canvas\Exception\ServerErrorHttpException;
10
use Baka\Http\Api\BaseController as BakaBaseController;
0 ignored issues
show
Bug introduced by
The type Baka\Http\Api\BaseController 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...
11
use Canvas\Dto\ListSchema;
12
use Canvas\Mapper\ListSchemaMapper;
13
use Phalcon\Db\Column;
0 ignored issues
show
Bug introduced by
The type Phalcon\Db\Column 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...
14
15
/**
16
 * Class LanguagesController.
17
 *
18
 * @package Canvas\Api\Controllers
19
 *
20
 */
21
class SchemaController extends BakaBaseController
22
{
23
    /**
24
     * set objects.
25
     *
26
     * @return void
27
     */
28
    public function onConstruct()
29
    {
30
        $this->model = new SystemModules();
0 ignored issues
show
Bug Best Practice introduced by
The property model does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
31
    }
32
33
    /**
34
     * Overwrite inde schema.
35
     *
36
     * @return Response
37
     */
38
    public function index(): Response
39
    {
40
        return $this->response(['We cant list this apps schema']);
41
    }
42
43
    /**
44
     * Given the slug get the schema.
45
     *
46
     * @param string $slug
47
     * @return Response
48
     */
49
    public function getBySlug(string $slug): Response
50
    {
51
        $schema = SystemModules::getBySlug($slug);
52
53
        //add a mapper
54
        $this->dtoConfig->registerMapping(SystemModules::class, ListSchema::class)
55
            ->useCustomMapper(new ListSchemaMapper());
56
57
        return $this->response($this->mapper->map($schema, ListSchema::class));
58
    }
59
60
    /**
61
     * Give the slug we give you the DB schema
62
     * this route is only meant to be run by an admin.
63
     *
64
     * @param string $slug
65
     * @return Response
66
     */
67
    public function getModelDescription(string $slug): Response
68
    {
69
        //none admin users can only edit themselves
70
        if (!$this->userData->hasRole('Default.Admins')) {
71
            throw new ServerErrorHttpException('No route found');
0 ignored issues
show
Deprecated Code introduced by
The class Canvas\Exception\ServerErrorHttpException has been deprecated: version 0.1.5 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

71
            throw /** @scrutinizer ignore-deprecated */ new ServerErrorHttpException('No route found');
Loading history...
72
        }
73
74
        $schema = SystemModules::getBySlug($slug);
75
        $model = new $schema->model_name;
76
77
        $columns = $this->db->describeColumns($model->getSource());
78
        $structure = [];
79
        $type = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $type is dead and can be removed.
Loading history...
80
81
        foreach ($columns as $column) {
82
            switch ($column->getType()) {
83
                case Column::TYPE_INTEGER:
84
                    $structure[$column->getName()] = 'integer';
85
                    break;
86
                case Column::TYPE_BIGINTEGER:
87
                    $structure[$column->getName()] = 'long';
88
                    break;
89
                case Column::TYPE_TEXT:
90
                case Column::TYPE_VARCHAR:
91
                case Column::TYPE_CHAR:
92
                    $structure[$column->getName()] = 'text';
93
                    break;
94
                case Column::TYPE_DATE:
95
                    // We define a format for date structure.
96
                    $structure[$column->getName()] = ['date', 'yyyy-MM-dd'];
97
                    break;
98
                case Column::TYPE_DATETIME:
99
                    // We define a format for datetime structure.
100
                    $structure[$column->getName()] = ['date', 'yyyy-MM-dd HH:mm:ss'];
101
                    break;
102
                case Column::TYPE_DECIMAL:
103
                    $structure[$column->getName()] = 'float';
104
                    break;
105
            }
106
        }
107
108
        if ($model->hasCustomFields()) {
109
            $structure = array_merge($structure, $model->getCustomFields());
110
        }
111
112
        return $this->response($structure);
113
    }
114
}
115