Test Failed
Push — master ( c223c8...a9e334 )
by Maximo
01:54
created

CompaniesController::edit()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 13
nc 5
nop 1
dl 0
loc 23
rs 9.8333
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 Exception;
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'];
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 Exception('Record not found');
75
        }
76
    }
77
78
    /**
79
     * Update a User Info
80
     *
81
     * @method PUT
82
     * @url /v1/company/{id}
83
     *
84
     * @return Phalcon\Http\Response
85
     */
86
    public function edit($id) : Response
87
    {
88
        $company = $this->model->findFirst([
89
            'id = ?0 AND is_deleted = 0 and users_id = ?1',
90
            '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...
91
        ]);
92
93
        if ($company) {
94
            $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

94
            /** @scrutinizer ignore-call */ $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...
95
96
            if (empty($request)) {
97
                $request = $this->request->getJsonRawBody(true);
98
            }
99
100
            //update
101
            if ($company->update($request, $this->updateFields)) {
102
                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...
103
            } else {
104
                //didnt work
105
                throw new Exception(current($company->getMessages()));
106
            }
107
        } else {
108
            throw new Exception('Record not found');
109
        }
110
    }
111
}
112