Passed
Push — develop ( 83a19a...5dcd0c )
by Daniel
05:43
created

AbstractFactory::getIgnoreOps()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Silverback\ApiComponentBundle\Factory\Entity;
4
5
use Doctrine\Common\Persistence\ObjectManager;
6
use Silverback\ApiComponentBundle\Exception\InvalidEntityException;
7
use Silverback\ApiComponentBundle\Exception\InvalidFactoryOptionException;
8
use Symfony\Component\Validator\Validator\ValidatorInterface;
9
10
abstract class AbstractFactory implements FactoryInterface
11
{
12
    /**
13
     * @var ObjectManager
14
     */
15
    protected $manager;
16
17
    /**
18
     * @var null|array
19
     */
20
    protected $ops;
21
22
    /**
23
     * @var ValidatorInterface
24
     */
25
    private $validator;
26
27
    /**
28
     * @param ObjectManager $manager
29
     * @param ValidatorInterface $validator
30
     */
31 61
    public function __construct(
32
        ObjectManager $manager,
33
        ValidatorInterface $validator
34
    ) {
35 61
        $this->manager = $manager;
36 61
        $this->validator = $validator;
37 61
    }
38
39
    /**
40
     * @param $component
41
     * @param array|null $ops
42
     */
43 30
    protected function init($component, ?array $ops = null): void
44
    {
45 30
        $this->setOptions($ops);
46 30
        foreach ($this->ops as $op=>$value) {
47
            if (
48 29
                null !== $value &&
49 29
                !\in_array($op, static::getIgnoreOps(), true)
50
            ) {
51 24
                $setter = $this->findSetterMethod($component, $op);
52 24
                if (\is_array($value)) {
53 1
                    $component->$setter(...$value);
54
                } else {
55 29
                    $component->$setter($value);
56
                }
57
            }
58
        }
59 30
        $this->manager->persist($component);
60 30
    }
61
62
    /**
63
     * @param $component
64
     * @param $op
65
     * @return string
66
     */
67 24
    private function findSetterMethod($component, $op): string
68
    {
69 24
        $prefixes = ['set', 'add'];
70 24
        $postfix = ucfirst($op);
71 24
        foreach ($prefixes as $prefix) {
72 24
            $setter = $prefix . $postfix;
73 24
            if (method_exists($component, $setter)) {
74 24
                return $setter;
75
            }
76
        }
77
        throw new \RuntimeException(sprintf('A preconfigured option `%s` has no setter or adder method', $op));
78
    }
79
80
    /**
81
     * @param array|null $ops
82
     * @throws \Silverback\ApiComponentBundle\Exception\InvalidFactoryOptionException
83
     */
84 32
    protected function setOptions(?array $ops): void
85
    {
86 32
        if (!$ops) {
87 6
            $ops = [];
88
        }
89 32
        $this->ops = array_filter(
90 32
            array_merge(static::defaultOps(), $ops),
91 32
            function ($key) {
92 31
                if (!array_key_exists($key, static::defaultOps())) {
93 2
                    throw new InvalidFactoryOptionException(
94 2
                        sprintf('%s is not a valid option for the factory %s', $key, \get_class($this))
95
                    );
96
                }
97 30
                return true;
98 32
            },
99 32
            ARRAY_FILTER_USE_KEY
100
        );
101 30
    }
102
103
    /**
104
     * @param $component
105
     * @return bool
106
     * @throws \Silverback\ApiComponentBundle\Exception\InvalidEntityException
107
     */
108 27
    protected function validate($component): bool
109
    {
110 27
        $errors = $this->validator->validate($component);
111 27
        if (\count($errors)) {
112
            throw new InvalidEntityException($errors);
113
        }
114 27
        return true;
115
    }
116
117
    /**
118
     * @return array
119
     */
120 3
    protected static function defaultOps(): array
121
    {
122 3
        return [];
123
    }
124
125
    /**
126
     * @return array
127
     */
128 8
    protected static function getIgnoreOps(): array
129
    {
130 8
        return [];
131
    }
132
133
    /**
134
     * @param array|null $ops
135
     */
136
    abstract public function create(?array $ops = null);
137
}
138