CompaniesBranchesController::getById()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 12
ccs 0
cts 7
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 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...
8
use Canvas\Http\Exception\UnprocessableEntityException;
9
use Canvas\Models\CompaniesBranches;
10
11
/**
12
 * Class CompaniesController.
13
 *
14
 * @package Canvas\Api\Controllers
15
 *
16
 * @property Users $userData
17
 * @property Request $request
18
 */
19
class CompaniesBranchesController extends BaseController
20
{
21
    /*
22
     * fields we accept to create
23
     *
24
     * @var array
25
     */
26
    protected $createFields = [
27
        'name',
28
        'address',
29
        'email',
30
        'zipcode',
31
        'phone',
32
        'is_default'
33
    ];
34
35
    /*
36
     * fields we accept to create
37
     *
38
     * @var array
39
     */
40
    protected $updateFields = [
41
        'name',
42
        'address',
43
        'email',
44
        'zipcode',
45
        'phone',
46
        'is_default'
47
    ];
48
49
    /**
50
     * set objects.
51
     *
52
     * @return void
53
     */
54
    public function onConstruct()
55
    {
56
        $this->model = new CompaniesBranches();
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...
57
        $this->model->users_id = $this->userData->getId();
58
        $this->model->companies_id = $this->userData->currentCompanyId();
59
60
        $this->additionalSearchFields = [
0 ignored issues
show
Bug Best Practice introduced by
The property additionalSearchFields does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
61
            ['companies_id', ':', $this->userData->currentCompanyId()],
62
        ];
63
    }
64
65
    /**
66
     * Get Uer.
67
     *
68
     * @param mixed $id
69
     *
70
     * @method GET
71
     * @url /v1/company/{id}
72
     *
73
     * @return Response
74
     */
75
    public function getById($id): Response
76
    {
77
        //find the info
78
        $record = $this->model->findFirstOrFail([
79
            'id = ?0 AND is_deleted = 0 and companies_id = ?1',
80
            'bind' => [$id, $this->userData->currentCompanyId()],
81
        ]);
82
83
        //get the results and append its relationships
84
        $result = $this->appendRelationshipsToResult($this->request, $record);
85
86
        return $this->response($this->processOutput($result));
87
    }
88
89
    /**
90
     * Update a User Info.
91
     *
92
     * @method PUT
93
     * @url /v1/company/{id}
94
     *
95
     * @return Response
96
     */
97
    public function edit($id) : Response
98
    {
99
        $company = $this->model->findFirstOrFail([
100
            'id = ?0 AND is_deleted = 0 and companies_id = ?1',
101
            'bind' => [$id, $this->userData->currentCompanyId()],
102
        ]);
103
104
        $request = $this->request->getPutData();
105
106
        //update
107
        if ($company->update($request, $this->updateFields)) {
108
            return $this->response($this->processOutput($company));
109
        } else {
110
            //didnt work
111
            throw new UnprocessableEntityException((string) current($company->getMessages()));
112
        }
113
    }
114
}
115