Failed Conditions
Pull Request — master (#10)
by Maximo
03:34
created

CompaniesController   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 38
dl 0
loc 121
ccs 0
cts 55
cp 0
rs 10
c 0
b 0
f 0
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A onConstruct() 0 7 1
A create() 0 16 3
A getById() 0 19 3
A edit() 0 23 4
1
<?php
0 ignored issues
show
Coding Style introduced by
End of line character is invalid; expected "\n" but found "\r\n"
Loading history...
2
3
declare(strict_types=1);
4
5
namespace Gewaer\Api\Controllers;
6
7
use Gewaer\Models\Companies;
8
use Phalcon\Http\Response;
9
use Gewaer\Exception\UnprocessableEntityHttpException;
10
11
/**
12
 * Class CompaniesController
13
 *
14
 * @package Gewaer\Api\Controllers
15
 *
16
 * @property Users $userData
17
 * @property Request $request
18
 */
19
class CompaniesController extends BaseController
20
{
21
    /*
22
     * fields we accept to create
23
     *
24
     * @var array
25
     */
26
    protected $createFields = ['name', 'profile_image', 'website', 'users_id'];
27
28
    /*
29
     * fields we accept to create
30
     *
31
     * @var array
32
     */
33
    protected $updateFields = ['name', 'profile_image', 'website'];
34
35
    /**
36
     * set objects
37
     *
38
     * @return void
39
     */
40
    public function onConstruct()
41
    {
42
        $this->model = new Companies();
0 ignored issues
show
Documentation Bug introduced by
It seems like new Gewaer\Models\Companies() of type Gewaer\Models\Companies is incompatible with the declared type array of property $model.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
43
        $this->modal->users_id = $this->userData->getId();
0 ignored issues
show
Bug Best Practice introduced by
The property modal does not exist on Gewaer\Api\Controllers\CompaniesController. Since you implemented __get, consider adding a @property annotation.
Loading history...
44
45
        $this->additionalSearchFields = [
46
            ['users_id', ':', $this->userData->getId()],
47
        ];
48
    }
49
50
    /**
51
     * Get Uer
52
     *
53
     * @param mixed $id
54
     *
55
     * @method GET
56
     * @url /v1/company/{id}
57
     *
58
     * @return Response
59
     */
60
    public function getById($id) : Response
61
    {
62
        //find the info
63
        $company = $this->model->findFirst([
64
            'id = ?0 AND is_deleted = 0 and users_id = ?1',
65
            'bind' => [$id, $this->userData->getId()],
66
        ]);
67
68
        //get relationship
69
        if ($this->request->hasQuery('relationships')) {
70
            $relationships = $this->request->getQuery('relationships', 'string');
71
72
            $company = QueryParser::parseRelationShips($relationships, $company);
0 ignored issues
show
Bug introduced by
The type Gewaer\Api\Controllers\QueryParser 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...
73
        }
74
75
        if ($company) {
76
            return $this->response($company);
77
        } else {
78
            throw new UnprocessableEntityHttpException('Record not found');
79
        }
80
    }
81
82
    /**
83
     * Add a new item
84
     *
85
     * @method POST
86
     * @url /v1/company
87
     *
88
     * @return Response
89
     */
90
    public function create() : Response
91
    {
92
        $request = $this->request->getPost();
93
94
        if (empty($request)) {
95
            $request = $this->request->getJsonRawBody(true);
96
        }
97
98
        //alwasy overwrite userid
99
        $request['users_id'] = $this->userData->getId();
100
101
        //try to save all the fields we allow
102
        if ($this->model->save($request, $this->createFields)) {
103
            return $this->response($this->model->toArray());
104
        } else {
105
            throw new UnprocessableEntityHttpException((string) $this->model->getMessages()[0]);
106
        }
107
    }
108
109
    /**
110
     * Update a User Info
111
     *
112
     * @method PUT
113
     * @url /v1/company/{id}
114
     *
115
     * @return Response
116
     */
117
    public function edit($id) : Response
118
    {
119
        $company = $this->model->findFirst([
120
            'id = ?0 AND is_deleted = 0 and users_id = ?1',
121
            'bind' => [$id, $this->userData->getId()],
122
        ]);
123
124
        if ($company) {
125
            $request = $this->request->getPut();
126
127
            if (empty($request)) {
128
                $request = $this->request->getJsonRawBody(true);
129
            }
130
131
            //update
132
            if ($company->update($request, $this->updateFields)) {
133
                return $this->response($company);
134
            } else {
135
                //didnt work
136
                throw new UnprocessableEntityHttpException(current($company->getMessages()));
137
            }
138
        } else {
139
            throw new UnprocessableEntityHttpException('Record not found');
140
        }
141
    }
142
}
143