Test Failed
Pull Request — master (#14)
by Maximo
16:08 queued 12:34
created

CompaniesController::onConstruct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
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
use Baka\Http\QueryParser;
11
12
/**
13
 * Class CompaniesController
14
 *
15
 * @package Gewaer\Api\Controllers
16
 *
17
 * @property Users $userData
18
 * @property Request $request
19
 */
20
class CompaniesController extends BaseController
21
{
22
    /*
23
     * fields we accept to create
24
     *
25
     * @var array
26
     */
27
    protected $createFields = ['name', 'profile_image', 'website', 'users_id'];
28
29
    /*
30
     * fields we accept to create
31
     *
32
     * @var array
33
     */
34
    protected $updateFields = ['name', 'profile_image', 'website'];
35
36
    /**
37
     * set objects
38
     *
39
     * @return void
40
     */
41 5
    public function onConstruct()
42
    {
43 5
        $this->model = new Companies();
44 5
        $this->model->users_id = $this->userData->getId();
45
46 5
        $this->additionalSearchFields = [
47 5
            ['users_id', ':', $this->userData->getId()],
48
        ];
49 5
    }
50
51
    /**
52
     * Get Uer
53
     *
54
     * @param mixed $id
55
     *
56
     * @method GET
57
     * @url /v1/company/{id}
58
     *
59
     * @return Response
60
     */
61 1
    public function getById($id) : Response
62
    {
63
        //find the info
64 1
        $company = $this->model->findFirst([
65 1
            'id = ?0 AND is_deleted = 0 and users_id = ?1',
66 1
            'bind' => [$id, $this->userData->getId()],
67
        ]);
68
69
        //get relationship
70 1
        if ($this->request->hasQuery('relationships')) {
71
            $relationships = $this->request->getQuery('relationships', 'string');
72
73
            $company = QueryParser::parseRelationShips($relationships, $company);
74
        }
75
76 1
        if ($company) {
77 1
            return $this->response($company);
78
        } else {
79
            throw new UnprocessableEntityHttpException('Record not found');
80
        }
81
    }
82
83
    /**
84
     * Add a new item
85
     *
86
     * @method POST
87
     * @url /v1/company
88
     *
89
     * @return Response
90
     */
91 1
    public function create() : Response
92
    {
93 1
        $request = $this->request->getPost();
94
95 1
        if (empty($request)) {
96
            $request = $this->request->getJsonRawBody(true);
97
        }
98
99
        //alwasy overwrite userid
100 1
        $request['users_id'] = $this->userData->getId();
101
102
        //try to save all the fields we allow
103 1
        if ($this->model->save($request, $this->createFields)) {
104 1
            return $this->response($this->model->toArray());
105
        } else {
106
            throw new UnprocessableEntityHttpException((string) $this->model->getMessages()[0]);
107
        }
108
    }
109
110
    /**
111
     * Update a User Info
112
     *
113
     * @method PUT
114
     * @url /v1/company/{id}
115
     *
116
     * @return Response
117
     */
118 1
    public function edit($id) : Response
119
    {
120 1
        $company = $this->model->findFirst([
121 1
            'id = ?0 AND is_deleted = 0 and users_id = ?1',
122 1
            'bind' => [$id, $this->userData->getId()],
123
        ]);
124
125 1
        if ($company) {
126 1
            $request = $this->request->getPut();
127
128 1
            if (empty($request)) {
129
                $request = $this->request->getJsonRawBody(true);
130
            }
131
132
            //update
133 1
            if ($company->update($request, $this->updateFields)) {
134 1
                return $this->response($company);
135
            } else {
136
                //didnt work
137
                throw new UnprocessableEntityHttpException((string) current($company->getMessages()));
138
            }
139
        } else {
140
            throw new UnprocessableEntityHttpException('Record not found');
141
        }
142
    }
143
}
144