Completed
Push — master ( 1dc9cc...12d9a4 )
by Evstati
01:46 queued 28s
created

Kohana_Model_Shipping_Group::initialize()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 0
cts 0
cp 0
rs 8.9713
c 0
b 0
f 0
cc 1
eloc 19
nc 1
nop 1
crap 2
1
<?php defined('SYSPATH') OR die('No direct script access.');
2
3
/**
4
 * @package    openbuildings\shipping
5
 * @author     Ivan Kerin <[email protected]>
6
 * @copyright  (c) 2013 OpenBuildings Ltd.
7
 * @license    http://spdx.org/licenses/BSD-3-Clause
8
 */
9
class Kohana_Model_Shipping_Group extends Jam_Model {
10
11
	/**
12
	 * @codeCoverageIgnore
13
	 */
14
	public static function initialize(Jam_Meta $meta)
15
	{
16
		$meta
17
			->behaviors(array(
18
				'paranoid' => Jam::behavior('paranoid'),
19
			))
20
			->associations(array(
21
				'shipping' => Jam::association('belongsto', array('inverse_of' => 'locations')),
22
				'method'   => Jam::association('belongsto', array('foreign_model' => 'shipping_method', 'inverse_of' => 'locations')),
23
				'location' => Jam::association('belongsto', array('inverse_of' => 'shipping_group')),
24
				'shipping_items' => Jam::association('hasmany', array('inverse_of' => 'shipping_group')),
25
			))
26
			->fields(array(
27
				'id'            => Jam::field('primary'),
28
				'price'         => Jam::field('price'),
29
				'delivery_time' => Jam::field('range', array('format' => 'Model_Shipping::format_shipping_time')),
30
				'additional_item_price' => Jam::field('price', array('convert_empty' => FALSE)),
31
				'discount_threshold' => Jam::field('price'),
32
			))
33
			->validator('shipping', 'location', 'method', 'delivery_time', array('present' => TRUE))
34
			->validator('price', array('present' => array('allow_zero' => TRUE)))
35
			->validator('additional_item_price', 'discount_threshold', 'price', array('price' => array('greater_than_or_equal_to' => 0)))
36
			->validator('delivery_time', array('range' => array('consecutive' => TRUE, 'greater_than_or_equal_to' => 0)));
37
	}
38
39
	/**
40
	 * Sort Model_Shipping_Item by price, biggest price first
41
	 * @param  array  $items
42
	 * @return array
43
	 */
44 3
	public static function sort_by_price(array $items)
45
	{
46 3
		Array_Util::validate_instance_of($items, 'Model_Shipping_Group');
47
48 3
		usort($items, function($item1, $item2){
49 3
			return $item1->price->is(Jam_Price::GREATER_THAN, $item2->price) ? -1 : 1;
50 3
		});
51
52 3
		return $items;
53
	}
54
55 2
	public function total_delivery_time()
56
	{
57 2
		return $this->delivery_time;
0 ignored issues
show
Documentation introduced by
The property delivery_time does not exist on object<Kohana_Model_Shipping_Group>. 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...
58
	}
59
60
	/**
61
	 * Get the currency for pricing calculations
62
	 * @return string
63
	 * @throws Kohana_Exception If brand_purchase_shipping is NULL
64
	 */
65 2
	public function currency()
66
	{
67 2
		return $this->get_insist('shipping')->currency();
68
	}
69
70
	/**
71
	 * Return TRUE if total is bigger than discount_threshold
72
	 * @return boolean
73
	 */
74 2
	public function is_discounted(Jam_Price $total)
75
	{
76 2
		return ($this->discount_threshold AND $total->is(Jam_Price::GREATER_THAN, $this->discount_threshold));
0 ignored issues
show
Documentation introduced by
The property discount_threshold does not exist on object<Kohana_Model_Shipping_Group>. 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...
77
	}
78
}
79