Cart_model::get_all()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 40
rs 9.28
c 0
b 0
f 0
cc 3
nc 3
nop 0
1
<?php
2
3
/**
4
 * @property CI_Session      $session
5
 * @property Inventory_model $inventory_model
6
 */
7
class Cart_model extends CI_Model {
8
9
	public function __construct()
10
	{
11
		parent::__construct();
12
		$this->load->library('session');
0 ignored issues
show
Documentation introduced by
The property load does not exist on object<Cart_model>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
13
		$this->load->model('shop/inventory_model');
0 ignored issues
show
Documentation introduced by
The property load does not exist on object<Cart_model>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
14
	}
15
16
	/**
17
	 * カゴに追加/削除
18
	 * 
19
	 * @param int $id 商品ID
20
	 * @param int $qty 数量
21
	 */
22
	public function add($id, $qty)
23
	{
24
# 商品IDと数量を引数として渡され、数量が0以下の場合は、セッションクラスの
25
# unset_userdata()メソッドで、セッションデータからその商品を削除します。
26
		if ($qty <= 0)
27
		{
28
			$this->session->unset_userdata('item' . $id);
29
		}
30
# 指定の数量が1以上の場合は、その商品が存在するかチェックした後に、商品と数量を
31
# セッションデータに追加します。セッションの項目名は「item+商品ID」とします。
32
		elseif ($this->inventory_model->is_available_product_item($id))
33
		{
34
			$this->session->set_userdata('item' . $id, $qty);
35
		}
36
	}
37
38
	/**
39
	 * 買い物カゴの情報を取得
40
	 * 
41
	 * @return array
42
	 */
43
	public function get_all()
44
	{
45
		$items = [];	// 商品情報の配列
46
		$total = 0;		// 合計金額
47
		$line  = 0;		// 行数
48
49
# セッションクラスのuserdata()メソッドですべてのセッションデータを取得し、
50
# ループで回して必要な情報を取り出します。
51
		foreach ($this->session->userdata() as $key => $val)
52
		{
53
# 配列のキーが「item」で始まる場合、商品情報です。
54
			if (substr($key, 0, 4) == 'item')
55
			{
56
				$line++;
57
# 配列のキーから商品IDを取り出します。
58
				$id     = (int) substr($key, 4);
59
# get_product_item()メソッドを使い、商品データを取得します。
60
				$item   = $this->inventory_model->get_product_item($id);
61
# 単価に数量を掛けて金額を計算します。
62
				$amount = $item->price * $val;
63
# 以上の情報を連想配列に代入します。
64
				$items[$line] = [
65
					'id'     => $id,
66
					'qty'    => $val,
67
					'name'   => $item->name,
68
					'price'  => $item->price,
69
					'amount' => $amount
70
				];
71
# 合計金額を計算します。
72
				$total = $total + $amount;
73
			}
74
		}
75
76
		$cart = [];
77
		$cart['items'] = $items;	// 商品情報の配列
78
		$cart['line']  = $line;		// 商品アイテム数
79
		$cart['total'] = $total;	// 合計金額
80
81
		return $cart;
82
	}
83
84
	/**
85
	 * カゴに入っている商品アイテム数を返す
86
	 * 
87
	 * @return int
88
	 */
89
	public function count()
90
	{
91
		$cart = $this->get_all();
92
		return $cart['line'];
93
	}
94
95
}
96