Completed
Push — master ( a2ba7d...ad276d )
by Dion
13s queued 10s
created

ReadSection::flush()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
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\SectionBeforeRead;
18
use Tardigrades\SectionField\Event\SectionDataRead;
19
use Tardigrades\SectionField\ValueObject\SectionConfig;
20
21
class ReadSection implements ReadSectionInterface
22
{
23
    /** @var ReadSectionInterface[] */
24
    private $readers;
25
26
    /** @var SectionManagerInterface */
27
    private $sectionManager;
28
29
    /** @var EventDispatcherInterface */
30
    private $dispatcher;
31
32
    public function __construct(
33
        array $readers,
34
        SectionManagerInterface $sectionManager,
35
        EventDispatcherInterface $dispatcher
36
    ) {
37
        $this->readers = $readers;
38
        $this->sectionManager = $sectionManager;
39
        $this->dispatcher = $dispatcher;
40
    }
41
42
    /**
43
     * Read from one or more data-sources
44
     *
45
     * @param ReadOptionsInterface $options
46
     * @param SectionConfig|null $sectionConfig
47
     * @return \ArrayIterator
48
     */
49
    public function read(
50
        ReadOptionsInterface $options,
51
        SectionConfig $sectionConfig = null
52
    ): \ArrayIterator {
53
        $sectionData = new \ArrayIterator();
54
55
        $this->dispatcher->dispatch(
56
            SectionBeforeRead::NAME,
57
            new SectionBeforeRead($sectionData, $options, $sectionConfig)
58
        );
59
60
        if ($sectionConfig === null) {
61
            $sectionConfig = $this->sectionManager->readByHandle(
62
                $options->getSection()[0]->toHandle()
0 ignored issues
show
Bug introduced by
The method getSection() does not exist on Tardigrades\SectionField...ce\ReadOptionsInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Tardigrades\SectionField...ce\ReadOptionsInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

62
                $options->/** @scrutinizer ignore-call */ 
63
                          getSection()[0]->toHandle()
Loading history...
63
            )->getConfig();
64
        }
65
66
        // Make sure we are passing the fully qualified class name as the section
67
        $optionsArray = $options->toArray();
68
        $optionsArray[ReadOptions::SECTION] = (string) $sectionConfig->getFullyQualifiedClassName();
69
        // For now, we call DoctrineRead options, this will of course be fixed in a later release.
70
        $options = ReadOptions::fromArray($optionsArray);
71
72
        /** @var ReadSectionInterface $reader */
73
        foreach ($this->readers as $reader) {
74
            foreach ($reader->read($options, $sectionConfig) as $entry) {
75
                $sectionData->append($entry);
76
            }
77
        }
78
79
        $this->dispatcher->dispatch(
80
            SectionDataRead::NAME,
81
            new SectionDataRead($sectionData, $options, $sectionConfig)
82
        );
83
84
        return $sectionData;
85
    }
86
87
    public function flush(): void
88
    {
89
        foreach ($this->readers as $reader) {
90
            $reader->flush();
91
        }
92
    }
93
}
94