Kohana_Model_Promotion_Promocode_Giftcard   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 6
dl 0
loc 52
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 12 1
A validate_purchase() 0 9 2
A price_for_purchase_item() 0 23 3
1
<?php defined('SYSPATH') OR die('No direct script access.');
2
3
/**
4
 * @package    openbuildings\promotions
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_Promotion_Promocode_Giftcard extends Model_Promotion_Promocode {
10
11
	/**
12
	 * @codeCoverageIgnore
13
	 */
14
	public static function initialize(Jam_Meta $meta)
15
	{
16
		parent::initialize($meta);
17
18
		$meta
19
			->fields(array(
20
				'amount' => Jam::field('price'),
21
				'requirement' => Jam::field('price'),
22
			))
23
			->validator('amount', array('price' => array('greater_than' => 0)))
24
			->validator('requirement', array('price' => array('greater_than_or_equal_to' => 0)));
25
	}
26
27 2
	public function validate_purchase(Model_Purchase $purchase)
28
	{
29 2
		$purchase_price = $purchase->total_price('product');
30
31 2
		if ($purchase_price->is(Jam_Price::LESS_THAN, $this->requirement))
0 ignored issues
show
Documentation introduced by
The property requirement does not exist on object<Kohana_Model_Promotion_Promocode_Giftcard>. 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...
32
		{
33 1
			$purchase->errors()->add('promo_code_text', 'requirement', array(':more_than' => $this->requirement->humanize()));
0 ignored issues
show
Documentation introduced by
The property requirement does not exist on object<Kohana_Model_Promotion_Promocode_Giftcard>. 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...
34
		}
35 2
	}
36
37 1
	public function price_for_purchase_item(Model_Purchase_Item $purchase_item)
38
	{
39 1
		$brand_purchase = $purchase_item->get_insist('brand_purchase');
40 1
		$brand_total = $brand_purchase->total_price('product');
41
42 1
		$purchase = $brand_purchase->get_insist('purchase');
43
44
		$totals = array_map(function ($brand_purchase) {
45 1
			return $this->applies_to($brand_purchase)
46 1
				? $brand_purchase->total_price('product')
47 1
				: 0;
48 1
		}, $purchase->brand_purchases->as_array());
49
50 1
		$total = Jam_Price::sum($totals, $purchase_item->currency(), $purchase_item->monetary());
51
52 1
		$multiplier = $total->is(Jam_Price::GREATER_THAN, 0)
53 1
			? $brand_total->amount() / $total->amount()
54 1
			: 1;
55
56 1
		return $this->amount
0 ignored issues
show
Documentation introduced by
The property amount does not exist on object<Kohana_Model_Promotion_Promocode_Giftcard>. 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...
57 1
			->monetary($purchase_item->monetary())
58 1
			->multiply_by(-$multiplier);
59
	}
60
}
61