Failed Conditions
Pull Request — 0.2 (#77)
by Rafael
05:57
created

AppsSettingsController::onConstruct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
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\Apps;
8
use Canvas\Dto\AppsSettings;
9
use Phalcon\Http\Response;
10
11
/**
12
 * Class LanguagesController.
13
 *
14
 * @package Canvas\Api\Controllers
15
 *
16
 */
17
class AppsSettingsController extends BaseController
18
{
19
    /*
20
     * fields we accept to create
21
     *
22
     * @var array
23
     */
24
    protected $createFields = [];
25
26
    /*
27
     * fields we accept to create
28
     *
29
     * @var array
30
     */
31
    protected $updateFields = [];
32
33
    /**
34
     * set objects.
35
     *
36
     * @return void
37
     */
38
    public function onConstruct()
39
    {
40
        $this->model = new Apps();
41
    }
42
43
    /**
44
     * Given the model list the records based on the  filter.
45
     *
46
     * @return Response
47
     */
48
    public function index(): Response
49
    {
50
        return $this->response([]);
51
    }
52
53
    /**
54
     * Create new record.
55
     *
56
     * @return Response
57
     */
58
    public function create(): Response
59
    {
60
        return $this->response([]);
61
    }
62
63
    /**
64
     * Get the record by its primary key.
65
     *
66
     * @param mixed $id
67
     *
68
     * @throws Exception
69
     * @return Response
70
     */
71
    public function getById($id): Response
72
    {
73
        return $this->response([]);
74
    }
75
76
    /**
77
     * Delete a Record.
78
     *
79
     * @throws Exception
80
     * @return Response
81
     */
82
    public function delete($id): Response
83
    {
84
        return $this->response([]);
85
    }
86
87
    /**
88
     * Format output.
89
     *
90
     * @param [type] $results
91
     * @return void
92
     */
93
    protected function processOutput($results)
94
    {
95
        //DTOAppsSettings
96
        $this->dtoConfig->registerMapping(Apps::class, AppsSettings::class)
97
          ->forMember('settings', function (Apps $app) {
98
              $settings = [];
99
              foreach ($app->settingsApp->toArray() as $setting) {
100
                  $settings[$setting['name']] = $setting['value'];
101
              }
102
              return $settings;
103
          });
104
105
        return is_iterable($results) ?
106
                $this->mapper->mapMultiple(iterator_to_array($results), AppsSettings::class)
107
                : $this->mapper->map($results, AppsSettings::class);
108
    }
109
110
    /**
111
     * get item.
112
     *
113
     * @param mixed $id
114
     *
115
     * @method GET
116
     * @url /v1/data/{id}
117
     *
118
     * @return \Phalcon\Http\Response
119
     */
120
    public function getByKey($key = null): Response
121
    {
122
        //find the info
123
        $record = $this->model->findFirst([
124
            'key = ?0 AND is_deleted = 0',
125
            'bind' => [$key],
126
        ]);
127
128
        //get the results and append its relationships
129
        $result = $this->appendRelationshipsToResult($this->request, $record);
130
131
        return $this->response($this->processOutput($result));
132
    }
133
}
134