Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.65% covered (success)
95.65%
22 / 23
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
BlockFactory
95.65% covered (success)
95.65%
22 / 23
50.00% covered (danger)
50.00%
1 / 2
7
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 createBlocks
95.24% covered (success)
95.24%
20 / 21
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace Flow;
4
5use Flow\Block\AbstractBlock;
6use Flow\Block\BoardHistoryBlock;
7use Flow\Block\HeaderBlock;
8use Flow\Block\TopicBlock;
9use Flow\Block\TopicListBlock;
10use Flow\Block\TopicSummaryBlock;
11use Flow\Data\ManagerGroup;
12use Flow\Exception\DataModelException;
13use Flow\Exception\InvalidDataException;
14use Flow\Model\Workflow;
15use Flow\Repository\RootPostLoader;
16
17class BlockFactory {
18    /**
19     * @var ManagerGroup
20     */
21    protected $storage;
22
23    /**
24     * @var RootPostLoader
25     */
26    protected $rootPostLoader;
27
28    public function __construct(
29        ManagerGroup $storage,
30        RootPostLoader $rootPostLoader
31    ) {
32        $this->storage = $storage;
33        $this->rootPostLoader = $rootPostLoader;
34    }
35
36    /**
37     * @param Workflow $workflow
38     * @return AbstractBlock[]
39     * @throws DataModelException When the workflow type is unrecognized
40     * @throws InvalidDataException When multiple blocks share the same name
41     */
42    public function createBlocks( Workflow $workflow ) {
43        switch ( $workflow->getType() ) {
44            case 'discussion':
45                $blocks = [
46                    new HeaderBlock( $workflow, $this->storage ),
47                    new TopicListBlock( $workflow, $this->storage ),
48                    new BoardHistoryBlock( $workflow, $this->storage ),
49                ];
50                break;
51
52            case 'topic':
53                $blocks = [
54                    new TopicBlock( $workflow, $this->storage, $this->rootPostLoader ),
55                    new TopicSummaryBlock( $workflow, $this->storage ),
56                ];
57                break;
58
59            default:
60                throw new DataModelException( 'Not Implemented', 'process-data' );
61        }
62
63        $return = [];
64        /** @var AbstractBlock[] $blocks */
65        foreach ( $blocks as $block ) {
66            if ( isset( $return[$block->getName()] ) ) {
67                throw new InvalidDataException( 'Multiple blocks with same name is not yet supported', 'fail-load-data' );
68            }
69            $return[$block->getName()] = $block;
70        }
71
72        return $return;
73    }
74}