Failed Conditions
Pull Request — master (#9)
by Maximo
03:21
created

CompaniesController::getById()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 4
nop 1
dl 0
loc 19
ccs 0
cts 14
cp 0
crap 12
rs 9.9332
c 0
b 0
f 0
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
 * Base controller
13
 *
14
 */
15
class CompaniesController extends BaseController
16
{
17
    /*
18
     * fields we accept to create
19
     *
20
     * @var array
21
     */
22
    protected $createFields = ['name', 'profile_image', 'website', 'users_id'];
23
24
    /*
25
     * fields we accept to create
26
     *
27
     * @var array
28
     */
29
    protected $updateFields = ['name', 'profile_image', 'website'];
30
31
    /**
32
     * set objects
33
     *
34
     * @return void
35
     */
36
    public function onConstruct()
37
    {
38
        $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...
39
        $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...
Bug Best Practice introduced by
The property userData does not exist on Gewaer\Api\Controllers\CompaniesController. Since you implemented __get, consider adding a @property annotation.
Loading history...
40
41
        $this->additionalSearchFields = [
42
            ['users_id', ':', $this->userData->getId()],
43
        ];
44
    }
45
46
    /**
47
     * Get Uer
48
     *
49
     * @param mixed $id
50
     *
51
     * @method GET
52
     * @url /v1/company/{id}
53
     *
54
     * @return Phalcon\Http\Response
0 ignored issues
show
Bug introduced by
The type Gewaer\Api\Controllers\Phalcon\Http\Response was not found. Did you mean Phalcon\Http\Response? If so, make sure to prefix the type with \.
Loading history...
55
     */
56
    public function getById($id) : Response
57
    {
58
        //find the info
59
        $company = $this->model->findFirst([
60
            'id = ?0 AND is_deleted = 0 and users_id = ?1',
61
            'bind' => [$id, $this->userData->getId()],
0 ignored issues
show
Bug Best Practice introduced by
The property userData does not exist on Gewaer\Api\Controllers\CompaniesController. Since you implemented __get, consider adding a @property annotation.
Loading history...
62
        ]);
63
64
        //get relationship
65
        if ($this->request->hasQuery('relationships')) {
66
            $relationships = $this->request->getQuery('relationships', 'string');
67
68
            $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...
69
        }
70
71
        if ($company) {
72
            return $this->response($company);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->response($company) returns the type Phalcon\Http\Response which is incompatible with the documented return type Gewaer\Api\Controllers\Phalcon\Http\Response.
Loading history...
73
        } else {
74
            throw new UnprocessableEntityHttpException('Record not found');
75
        }
76
    }
77
78
    /**
79
     * Add a new item
80
     *
81
     * @method POST
82
     * @url /v1/company
83
     *
84
     * @return Phalcon\Http\Response
85
     */
86
    public function create() : Response
87
    {
88
        $request = $this->request->getPost();
89
90
        if (empty($request)) {
91
            $request = $this->request->getJsonRawBody(true);
92
        }
93
94
        //alwasy overwrite userid
95
        $request['users_id'] = $this->userData->getId();
0 ignored issues
show
Bug Best Practice introduced by
The property userData does not exist on Gewaer\Api\Controllers\CompaniesController. Since you implemented __get, consider adding a @property annotation.
Loading history...
96
97
        //try to save all the fields we allow
98
        if ($this->model->save($request, $this->createFields)) {
99
            return $this->response($this->model->toArray());
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->response($this->model->toArray()) returns the type Phalcon\Http\Response which is incompatible with the documented return type Gewaer\Api\Controllers\Phalcon\Http\Response.
Loading history...
100
        } else {
101
            throw new UnprocessableEntityHttpException((string) $this->model->getMessages()[0]);
102
        }
103
    }
104
105
    /**
106
     * Update a User Info
107
     *
108
     * @method PUT
109
     * @url /v1/company/{id}
110
     *
111
     * @return Phalcon\Http\Response
112
     */
113
    public function edit($id) : Response
114
    {
115
        $company = $this->model->findFirst([
116
            'id = ?0 AND is_deleted = 0 and users_id = ?1',
117
            'bind' => [$id, $this->userData->getId()],
0 ignored issues
show
Bug Best Practice introduced by
The property userData does not exist on Gewaer\Api\Controllers\CompaniesController. Since you implemented __get, consider adding a @property annotation.
Loading history...
118
        ]);
119
120
        if ($company) {
121
            $request = $this->request->getPut();
0 ignored issues
show
Bug introduced by
The method getPut() does not exist on Phalcon\Http\RequestInterface. Did you maybe mean getPost()? ( Ignorable by Annotation )

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

121
            /** @scrutinizer ignore-call */ 
122
            $request = $this->request->getPut();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
122
123
            if (empty($request)) {
124
                $request = $this->request->getJsonRawBody(true);
125
            }
126
127
            //update
128
            if ($company->update($request, $this->updateFields)) {
129
                return $this->response($company);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->response($company) returns the type Phalcon\Http\Response which is incompatible with the documented return type Gewaer\Api\Controllers\Phalcon\Http\Response.
Loading history...
130
            } else {
131
                //didnt work
132
                throw new UnprocessableEntityHttpException(current($company->getMessages()));
133
            }
134
        } else {
135
            throw new UnprocessableEntityHttpException('Record not found');
136
        }
137
    }
138
}
139