Passed
Pull Request — master (#672)
by Tortue
03:52
created

FactoryMuffin   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 25
rs 10
wmc 6

1 Method

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