Completed
Push — master ( fc9daf...df9d2c )
by Dion
01:00
created

CreateSection::save()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 13
nc 4
nop 1
1
<?php
2
3
/*
4
 * This file is part of the SexyField package.
5
 *
6
 * (c) Dion Snoeijen <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare (strict_types = 1);
13
14
namespace Tardigrades\SectionField\Service;
15
16
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
17
use Tardigrades\SectionField\Event\SectionEntryBeforeCreate;
18
use Tardigrades\SectionField\Event\SectionEntryCreated;
19
use Tardigrades\SectionField\Generator\CommonSectionInterface;
20
use Tardigrades\SectionField\ValueObject\FullyQualifiedClassName;
21
22
/**
23
 * {@inheritdoc}
24
 */
25
class CreateSection implements CreateSectionInterface
26
{
27
    /** @var CreateSectionInterface[] */
28
    private $creators;
29
30
    /** @var EventDispatcherInterface */
31
    private $dispatcher;
32
33
    /** @var CacheInterface */
34
    private $cache;
35
36
    public function __construct(
37
        array $creators,
38
        EventDispatcherInterface $dispatcher,
39
        CacheInterface $cache
40
    ) {
41
        $this->creators = $creators;
42
        $this->dispatcher = $dispatcher;
43
        $this->cache = $cache;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function save(CommonSectionInterface $sectionEntryEntity)
50
    {
51
        $this->dispatcher->dispatch(
52
            SectionEntryBeforeCreate::NAME,
53
            new SectionEntryBeforeCreate($sectionEntryEntity)
54
        );
55
56
        $update = !empty($sectionEntryEntity->getId());
57
58
        /** @var CreateSectionInterface $writer */
59
        foreach ($this->creators as $writer) {
60
            $writer->save($sectionEntryEntity);
61
        }
62
63
        try {
64
            $this->cache->invalidateForSection(
65
                FullyQualifiedClassName::fromString(get_class($sectionEntryEntity))
66
            );
67
        } catch (\Psr\Cache\InvalidArgumentException $exception) {
68
            //
69
        }
70
71
        $this->dispatcher->dispatch(
72
            SectionEntryCreated::NAME,
73
            new SectionEntryCreated($sectionEntryEntity, $update)
74
        );
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function persist(CommonSectionInterface $sectionEntryEntity)
81
    {
82
        /** @var CreateSectionInterface $writer */
83
        foreach ($this->creators as $writer) {
84
            $writer->persist($sectionEntryEntity);
85
        }
86
87
        try {
88
            $this->cache->invalidateForSection(
89
                FullyQualifiedClassName::fromString(get_class($sectionEntryEntity))
90
            );
91
        } catch (\Psr\Cache\InvalidArgumentException $exception) {
92
            //
93
        }
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99
    public function flush()
100
    {
101
        /** @var CreateSectionInterface $writer */
102
        foreach ($this->creators as $writer) {
103
            $writer->flush();
104
        }
105
    }
106
}
107