Kohana_Model_Promotion::price_for_purchase_item()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php defined('SYSPATH') OR die('No direct script access.');
2
3
/**
4
 * This is a base
5
 *
6
 * @package    openbuildings\promotions
7
 * @author     Ivan Kerin <[email protected]>
8
 * @author     Yasen Yanev <[email protected]>
9
 * @copyright  (c) 2013 OpenBuildings Ltd.
10
 * @license    http://spdx.org/licenses/BSD-3-Clause
11
 */
12
class Kohana_Model_Promotion extends Jam_Model implements Sellable {
13
14
	/**
15
	 * @codeCoverageIgnore
16
	 */
17
	public static function initialize(Jam_Meta $meta)
18
	{
19
		$meta
0 ignored issues
show
Bug introduced by
The method table cannot be called on $meta->unique_key(functi...'id' : 'identifier'; }) (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
20
			->unique_key(function($key){
0 ignored issues
show
Documentation introduced by
function ($key) { re... 'id' : 'identifier'; } is of type object<Closure>, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
21
				return is_numeric($key) ? 'id' : 'identifier';
22
			})
23
			->table('promotions')
24
			->associations(array(
25
				'purchase_items' => Jam::association('hasmany', array(
26
					'as' => 'reference',
27
					'foreign_model' => 'purchase_item_promotion',
28
				)),
29
				'promo_codes' => Jam::association('hasmany', array(
30
					'inverse_of' => 'promotion',
31
				))
32
			))
33
			->fields(array(
34
				'id' => Jam::field('primary'),
35
				'name' => Jam::field('string'),
36
				'currency' => Jam::field('string'),
37
				'identifier' => Jam::field('string'),
38
				'description' => Jam::field('text'),
39
				'priority' => Jam::field('integer', array('default' => 1)),
40
				'model' => Jam::field('polymorphic'),
41
				'created_at' => Jam::field('timestamp', array(
42
					'format' => 'Y-m-d H:i:s',
43
					'auto_now_create' => TRUE,
44
				)),
45
				'expires_at' => Jam::field('timestamp', array(
46
					'format' => 'Y-m-d H:i:s',
47
				)),
48
			))
49
			->validator('name', array('present' => TRUE))
50
			->validator('currency', array('currency' => TRUE));
51
	}
52
53
	/**
54
	 * @codeCoverageIgnore
55
	 */
56
	public function price_for_purchase_item(Model_Purchase_Item $purchase_item)
57
	{
58
		throw new Kohana_Exception('Not a valid promotion');
59
	}
60
61
	/**
62
	 * @codeCoverageIgnore
63
	 */
64
	public function applies_to(Model_Brand_Purchase $purchase)
65
	{
66
		throw new Kohana_Exception('Not a valid promotion');
67
	}
68
69
	/**
70
	 * Build a purchase item for this promotion (inverseof "reference")
71
	 * @return Model_Purchase_Item
72
	 */
73 1
	public function build_purchase_item()
74
	{
75 1
		return $this->purchase_items->build(array('model' => 'purchase_item_promotion'));
0 ignored issues
show
Documentation introduced by
The property purchase_items does not exist on object<Kohana_Model_Promotion>. 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...
76
	}
77
78 1
	public function currency()
79
	{
80 1
		return $this->currency;
0 ignored issues
show
Documentation introduced by
The property currency does not exist on object<Kohana_Model_Promotion>. 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...
81
	}
82
83 3
	public function is_expired()
84
	{
85 3
		return ($this->expires_at !== NULL AND strtotime($this->expires_at) < time());
0 ignored issues
show
Documentation introduced by
The property expires_at does not exist on object<Kohana_Model_Promotion>. 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...
86
	}
87
88
	/**
89
	 * If the promotion applies to the brand_purchase - add a purchase_item for this promotion, otherwise remove the associated purchase item
90
	 * @param  Model_Brand_Purchase $brand_purchase
0 ignored issues
show
Bug introduced by
There is no parameter named $brand_purchase. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
91
	 */
92 1
	public function update_brand_purchase_items($applies, & $items)
93
	{
94 1
		$promo_item = Jam::build('purchase_item_promotion', array('reference' => $this));
95
96 1
		$has_existing_promotion = false;
97 1
		foreach ($items as $index => $item){
98 1
			if ($item->is_same($promo_item)) {
99 1
				$has_existing_promotion = true;
100 1
				if ( ! $applies) {
101 1
					unset($items[$index]);
102
				}
103
			}
104
		}
105
106 1
		if ($applies && ! $has_existing_promotion) {
107 1
			$items[] = $promo_item;
108
		}
109 1
	}
110
}
111