Product   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 42
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A listProducts() 0 5 1
A addProduct() 0 23 2
1
<?php
2
/**
3
 * This file is part of product_management
4
 * User: Sinan TURGUT <[email protected]>
5
 * Date: 24.06.2019
6
 * php version 7.2
7
 *
8
 * @category Assessment
9
 * @package  ProductManagement
10
 * @author   Sinan TURGUT <[email protected]>
11
 * @license  See LICENSE file
12
 * @link     https://dev.sinanturgut.com.tr
13
 */
14
15
namespace App\Controller;
16
17
use App\Utility;
18
19
/**
20
 * Class Product
21
 *
22
 * @category Assessment
23
 * @package  App\Controller
24
 * @author   Sinan TURGUT <[email protected]>
25
 * @license  See LICENSE file
26
 * @link     https://dev.sinanturgut.com.tr
27
 */
28
class Product
29
{
30
    /**
31
     * List products
32
     *
33
     * @return object
34
     */
35
    public function listProducts()
36
    {
37
        $db = new \App\Model\Product();
38
        $products = $db->query("SELECT * FROM products where is_active")->data();
39
        return Utility\Response::write(true, $products);
40
    }
41
42
    /**
43
     * Add new product
44
     *
45
     * @return string
46
     */
47
    public function addProduct()
48
    {
49
        // Get user info from user management service
50
        $user = Utility\Request::getUser();
51
52
        // Die if user is not admin
53
        if(!$user['user']['is_admin']) {
54
            return Utility\Response::write(true, ['error'=>'authentication error!']);
0 ignored issues
show
Bug introduced by
Are you sure the usage of App\Utility\Response::wr...uthentication error!')) targeting App\Utility\Response::write() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
55
        }
56
57
        $params = json_decode(file_get_contents('php://input'),true);
58
59
        $db = new \App\Model\Product();
60
        $product = $db->query(
61
            "INSERT INTO products VALUES (NULL, :productName, :category, :summary, :price, 1)",
62
            [
63
                "productName" => $params['ProductName'],
64
                "category"=>$params['Category'],
65
                "summary"=>$params['Description'],
66
                "price"=>$params['Price']
67
            ]
68
        )->data();
69
        return Utility\Response::write(true, $params);
70
71
    }
72
73
}
74