FactoryMuffin   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 23
rs 10
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
A generate() 0 12 6
1
<?php
2
3
namespace OCA\Polls\Tests\Unit;
4
5
use League\FactoryMuffin\FactoryMuffin as OriginalFactoryMuffin;
6
use OCP\AppFramework\Db\Entity;
7
8
class FactoryMuffin extends OriginalFactoryMuffin {
9
	/**
10
	 * Generate and set the model attributes.
11
	 * NOTE: Patch the original method to support dynamic setter and getter
12
	 *        of the OCP\AppFramework\Db\Entity class
13
	 *
14
	 * @param object $model The model instance.
15
	 * @param array  $attr  The model attributes.
16
	 *
17
	 * @return void
18
	 */
19
	protected function generate($model, array $attr = []) {
20
		foreach ($attr as $key => $kind) {
21
			$value = $this->factory->generate($kind, $model, $this);
22
23
			$setter = 'set' . ucfirst(static::camelize($key));
24
			// check if there is a setter and use it instead
25
			if ($model instanceof Entity && is_callable([$model, $setter])) {
26
				$model->$setter($value);
27
			} elseif (method_exists($model, $setter) && is_callable([$model, $setter])) {
28
				$model->$setter($value);
29
			} else {
30
				$model->$key = $value;
31
			}
32
		}
33
	}
34
}
35