Completed
Push — master ( e6e660...2d183f )
by Pavel
02:25
created

Row   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 17
c 2
b 0
f 1
lcom 1
cbo 3
dl 0
loc 122
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A getId() 0 4 1
B getValue() 0 22 5
B getEntityProperty() 0 22 4
A getItem() 0 4 1
A hasGroupAction() 0 6 2
A hasAction() 0 6 2
1
<?php
2
3
/**
4
 * @copyright   Copyright (c) 2015 ublaboo <[email protected]>
5
 * @author      Pavel Janda <[email protected]>
6
 * @package     Ublaboo
7
 */
8
9
namespace Ublaboo\DataGrid;
10
11
use Nette;
12
use LeanMapper;
13
use DibiRow;
14
15
class Row extends Nette\Object
16
{
17
18
	/**
19
	 * @var DataGrid
20
	 */
21
	protected $datagrid;
22
23
	/**
24
	 * @var mixed
25
	 */
26
	protected $item;
27
28
	/**
29
	 * @var string
30
	 */
31
	protected $primary_key;
32
33
	/**
34
	 * @var mixed
35
	 */
36
	protected $id;
37
38
39
	/**
40
	 * @param mixed  $item
41
	 * @param string $primary_key
42
	 */
43
	public function __construct(DataGrid $datagrid, $item, $primary_key)
44
	{
45
		$this->datagrid = $datagrid;
46
		$this->item = $item;
47
		$this->primary_key = $primary_key;
48
49
		$this->id = is_object($item) ? $item->{$primary_key} : $item[$primary_key];
50
	}
51
52
53
	/**
54
	 * Get id value of item
55
	 * @return mixed
56
	 */
57
	public function getId()
58
	{
59
		return $this->id;
60
	}
61
62
63
	public function getValue($key)
64
	{
65
		if ($this->item instanceof LeanMapper\Entity) {
0 ignored issues
show
Bug introduced by
The class LeanMapper\Entity does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
66
			return $this->getEntityProperty($this->item, $key);
67
68
		} else if ($this->item instanceof DibiRow) {
0 ignored issues
show
Bug introduced by
The class DibiRow does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
69
			return $this->item->{$key};
70
71
		} else if ($this->item instanceof Nette\Database\Table\ActiveRow) {
0 ignored issues
show
Bug introduced by
The class Nette\Database\Table\ActiveRow does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
72
			return $this->item->{$key};
73
74
		} else if (is_array($this->item)) {
75
			return $this->item[$key];
76
77
		} else {
78
			/**
79
			 * Doctrine entity
80
			 */
81
			return $this->getEntityProperty($this->item, $key);
82
83
		}
84
	}
85
86
87
	public function getEntityProperty($item, $key)
88
	{
89
		$properties = explode('.', $key);
90
		$value = $item;
91
92
		while ($property = array_shift($properties)) {
93
			if (!isset($value->{$property})) {
94
				if ($this->datagrid->strict_entity_property) {
95
					throw new DataGridException(sprintf(
96
						'Target Property [%s] is not an object or is empty, trying to get [%s]',
97
						$value, str_replace('.', '->', $key)
98
					));
99
				}
100
101
				return NULL;
102
			}
103
104
			$value = $value->{$property};
105
		}
106
107
		return $value;
108
	}
109
110
111
	/**
112
	 * Get original item
113
	 * @return mixed
114
	 */
115
	public function getItem()
116
	{
117
		return $this->item;
118
	}
119
120
121
	public function hasGroupAction()
122
	{
123
		$condition = $this->datagrid->getRowCondition('group_action');
124
125
		return $condition ? $condition($this->item) : TRUE;
126
	}
127
128
129
	public function hasAction($key)
130
	{
131
		$condition = $this->datagrid->getRowCondition('action', $key);
132
133
		return $condition ? $condition($this->item) : TRUE;
134
	}
135
136
}
137