Passed
Push — develop ( 53a66f...07e04a )
by Daniel
05:35
created

AbstractComponentFactory::__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\Component;
4
5
use Doctrine\Common\Persistence\ObjectManager;
6
use Silverback\ApiComponentBundle\Entity\Component\AbstractComponent;
7
use Silverback\ApiComponentBundle\Entity\Content\AbstractContent;
8
use Silverback\ApiComponentBundle\Exception\InvalidEntityException;
9
use Silverback\ApiComponentBundle\Exception\InvalidFactoryOptionException;
10
use Symfony\Component\Validator\Validator\ValidatorInterface;
11
12
abstract class AbstractComponentFactory implements ComponentFactoryInterface
13
{
14
    /**
15
     * @var ObjectManager
16
     */
17
    protected $manager;
18
19
    /**
20
     * @var null|array
21
     */
22
    protected $ops;
23
24
    /**
25
     * @var ValidatorInterface
26
     */
27
    private $validator;
28
29
    /**
30
     * @param ObjectManager $manager
31
     * @param ValidatorInterface $validator
32
     */
33 27
    public function __construct(
34
        ObjectManager $manager,
35
        ValidatorInterface $validator
36
    ) {
37 27
        $this->manager = $manager;
38 27
        $this->validator = $validator;
39 27
    }
40
41
    /**
42
     * @param AbstractComponent $component
43
     * @param array|null $ops
44
     * @throws \Silverback\ApiComponentBundle\Exception\InvalidFactoryOptionException
45
     */
46 14
    protected function init(AbstractComponent $component, ?array $ops = null): void
47
    {
48 14
        $this->setOptions($ops);
49 14
        $component->setClassName($this->ops['className']);
50 14
        $this->manager->persist($component);
51 14
    }
52
53
    /**
54
     * @param array|null $ops
55
     * @throws \Silverback\ApiComponentBundle\Exception\InvalidFactoryOptionException
56
     */
57 15
    protected function setOptions(?array $ops): void
58
    {
59 15
        if (!$ops) {
60
            $ops = [];
61
        }
62 15
        $this->ops = array_filter(
63 15
            array_merge(static::defaultOps(), $ops),
64 15
            function ($key) {
65 15
                if (!array_key_exists($key, static::defaultOps())) {
66 1
                    throw new InvalidFactoryOptionException(
67 1
                        sprintf('%s is not a valid option for the factory %s', $key, \get_class($this))
68
                    );
69
                }
70 15
                return true;
71 15
            },
72 15
            ARRAY_FILTER_USE_KEY
73
        );
74 14
    }
75
76
    /**
77
     * @param AbstractComponent $component
78
     * @return bool
79
     * @throws \Silverback\ApiComponentBundle\Exception\InvalidEntityException
80
     */
81 12
    protected function validate(AbstractComponent $component): bool
82
    {
83 12
        $errors = $this->validator->validate($component);
84 12
        if (\count($errors)) {
85
            throw new InvalidEntityException($errors);
86
        }
87 12
        return true;
88
    }
89
90
    /**
91
     * @return array
92
     */
93 27
    protected static function defaultOps(): array
94
    {
95
        return [
96 27
            'className' => null
97
        ];
98
    }
99
100
    /**
101
     * @param array|null $ops
102
     */
103
    abstract public function create(?array $ops = null);
104
}
105