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 && count($options->getSection()) > 0) { |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
61 | $sectionConfig = $this->sectionManager->readByHandle( |
||
62 | $options->getSection()[0]->toHandle() |
||
63 | )->getConfig(); |
||
64 | } |
||
65 | |||
66 | // Make sure we are passing the fully qualified class name as the section |
||
67 | if (count($options->getSection()) > 0) { |
||
68 | $optionsArray = $options->toArray(); |
||
69 | $optionsArray[ReadOptions::SECTION] = (string)$sectionConfig->getFullyQualifiedClassName(); |
||
70 | $options = ReadOptions::fromArray($optionsArray); |
||
71 | } |
||
72 | |||
73 | /** @var ReadSectionInterface $reader */ |
||
74 | foreach ($this->readers as $reader) { |
||
75 | foreach ($reader->read($options, $sectionConfig) as $entry) { |
||
76 | $sectionData->append($entry); |
||
77 | } |
||
78 | } |
||
79 | |||
80 | $this->dispatcher->dispatch( |
||
81 | SectionDataRead::NAME, |
||
82 | new SectionDataRead($sectionData, $options, $sectionConfig) |
||
83 | ); |
||
84 | |||
85 | return $sectionData; |
||
86 | } |
||
87 | |||
88 | public function flush(): void |
||
89 | { |
||
90 | foreach ($this->readers as $reader) { |
||
91 | $reader->flush(); |
||
92 | } |
||
93 | } |
||
94 | } |
||
95 |