Passed
Push — develop ( 4f08b4...f08141 )
by Daniel
05:37
created

AbstractFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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