Completed
Push — master ( 8253ba...a023d2 )
by Daniel
16:49 queued 05:23
created

AbstractComponentFactory   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Test Coverage

Coverage 93.1%

Importance

Changes 0
Metric Value
dl 0
loc 96
ccs 27
cts 29
cp 0.931
rs 10
c 0
b 0
f 0
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 5 1
A validate() 0 7 2
A __construct() 0 7 1
A setOptions() 0 16 3
A defaultOps() 0 4 1
1
<?php
2
3
namespace Silverback\ApiComponentBundle\Factory\Fixtures\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 10
    public function __construct(
34
        ObjectManager $manager,
35
        ValidatorInterface $validator
36
    )
37
    {
38 10
        $this->manager = $manager;
39 10
        $this->validator = $validator;
40 10
    }
41
42
    /**
43
     * @param AbstractComponent $component
44
     * @param array|null $ops
45
     * @throws \Silverback\ApiComponentBundle\Exception\InvalidFactoryOptionException
46
     */
47 10
    protected function init(AbstractComponent $component, ?array $ops = null): void
48
    {
49 10
        $this->setOptions($ops);
50 6
        $component->setClassName($this->ops['className']);
51 6
        $this->manager->persist($component);
52 6
    }
53
54
    /**
55
     * @param array|null $ops
56
     * @throws \Silverback\ApiComponentBundle\Exception\InvalidFactoryOptionException
57
     */
58 10
    protected function setOptions(?array $ops): void
59
    {
60 10
        if (!$ops) {
61
            $ops = [];
62
        }
63 10
        $this->ops = array_filter(
64 10
            array_merge(static::defaultOps(), $ops),
65 10
            function ($key) {
66 10
                if (!array_key_exists($key, static::defaultOps())) {
67 4
                    throw new InvalidFactoryOptionException(
68 4
                        sprintf('%s is not a valid option for the factory %s', $key, \get_class($this))
69
                    );
70
                }
71 10
                return true;
72 10
            },
73 10
            ARRAY_FILTER_USE_KEY
74
        );
75 6
    }
76
77
    /**
78
     * @param AbstractComponent $component
79
     * @return bool
80
     * @throws \Silverback\ApiComponentBundle\Exception\InvalidEntityException
81
     */
82 6
    protected function validate(AbstractComponent $component): bool
83
    {
84 6
        $errors = $this->validator->validate($component);
85 6
        if (\count($errors)) {
86
            throw new InvalidEntityException($errors);
87
        }
88 6
        return true;
89
    }
90
91
    /**
92
     * @return array
93
     */
94 10
    protected static function defaultOps(): array
95
    {
96
        return [
97 10
            'className' => null
98
        ];
99
    }
100
101
    /**
102
     * @param array|null $ops
103
     * @param null|AbstractContent $owner
104
     * @throws \Silverback\ApiComponentBundle\Exception\InvalidFactoryOptionException
105
     * @throws \Silverback\ApiComponentBundle\Exception\InvalidEntityException
106
     */
107
    abstract public function create(?array $ops = null, ?AbstractContent $owner = null);
108
}
109