Completed
Push — master ( ec2c00...c3f83b )
by Markus
05:40
created

ProductFinderModel::retrieveByIdAndName()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
cc 4
eloc 4
nc 3
nop 2
dl 8
loc 8
rs 9.2
c 0
b 0
f 0
1
<?php
2
use Agavi\Model\SingletonModelInterface;
3
4
class ProductFinderModel extends SampleAppBaseModel implements SingletonModelInterface
5
{
6
	// imagine this stuff is in a database :)
7
	protected static $products = array(
8
		array(
9
			'id'    => 8172401,
10
			'name'  => 'TPS Report Cover Sheet',
11
			'price' => 0.89,
12
		),
13
		array(
14
			'id'    => 917246,
15
			'name'  => 'Weighted Companion Cube',
16
			'price' => 129.99,
17
		),
18
		array(
19
			'id'    => 7856122,
20
			'name'  => 'Longcat',
21
			'price' => 14599,
22
		),
23
		array(
24
			'id'    => 123456,
25
			'name'  => 'Red Stapler',
26
			'price' => 3.14,
27
		),
28
		array(
29
			'id'    => 3165463,
30
			'name'  => 'Sildenafil Citrate',
31
			'price' => 14.69,
32
		),
33
	);
34
	
35
	public function retrieveAll()
36
	{
37
		$retval = array();
38
		
39
		foreach(self::$products as $product) {
40
			$retval[] = $this->context->getModel('Product', null, array($product));
41
		}
42
		
43
		return $retval;
44
	}
45
	
46
	public function retrieveRandom()
47
	{
48
		return $this->context->getModel('Product', null, array(self::$products[array_rand(self::$products)]));
49
	}
50
	
51
	public function retrieveByName($productName)
52
	{
53
		foreach(self::$products as $product) {
54
			if($product['name'] == $productName) {
55
				return $this->context->getModel('Product', null, array($product));
56
			}
57
		}
58
	}
59
	
60
	public function retrieveById($productId)
61
	{
62
		foreach(self::$products as $product) {
63
			if($product['id'] == $productId) {
64
				return $this->context->getModel('Product', null, array($product));
65
			}
66
		}
67
	}
68
	
69
	public function retrieveByIdAndName($productId, $productName)
70
	{
71
		foreach(self::$products as $product) {
72
			if($product['id'] == $productId && $product['name'] == $productName) {
73
				return $this->context->getModel('Product', null, array($product));
74
			}
75
		}
76
	}
77
}
78
79