Completed
Push — develop ( fbdd82...317691 )
by Mike
09:29
created

RenderHandler::getTemplateFromDefinition()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 27
rs 8.439
cc 6
eloc 15
nc 8
nop 2
1
<?php
2
/**
3
 * This file is part of phpDocumentor.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @copyright 2010-2015 Mike van Riel<[email protected]>
9
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
10
 * @link      http://phpdoc.org
11
 */
12
13
namespace phpDocumentor\Application;
14
15
use League\Event\Emitter;
16
use League\Event\EmitterInterface;
17
use League\Flysystem\Filesystem;
18
use League\Tactician\CommandBus;
19
use phpDocumentor\Application\Configuration\ConfigurationFactory;
20
use phpDocumentor\DomainModel\Dsn;
21
use phpDocumentor\Application\Render;
22
use phpDocumentor\DomainModel\Parser\Documentation;
23
use phpDocumentor\DomainModel\ReadModel\ReadModels;
24
use phpDocumentor\DomainModel\Renderer\Assets;
25
use phpDocumentor\DomainModel\Renderer\Template\Action;
26
use phpDocumentor\DomainModel\Uri;
27
use phpDocumentor\Infrastructure\FileSystemFactory;
28
use phpDocumentor\DomainModel\Path;
29
use phpDocumentor\DomainModel\Renderer\Template;
30
use phpDocumentor\DomainModel\Renderer\TemplateFactory;
31
use phpDocumentor\DomainModel\Renderer\RenderContext;
32
use phpDocumentor\DomainModel\Renderer\RenderActionCompleted;
33
use phpDocumentor\DomainModel\Renderer\RenderingFinished;
34
use phpDocumentor\DomainModel\Renderer\RenderingStarted;
35
use phpDocumentor\Infrastructure\Renderer\FlySystemArtefacts;
36
use phpDocumentor\Infrastructure\Renderer\FlySystemAssets;
37
38
final class RenderHandler
0 ignored issues
show
Complexity introduced by
The class RenderHandler has a coupling between objects value of 27. Consider to reduce the number of dependencies under 13.
Loading history...
39
{
40
    /** @var TemplateFactory */
41
    private $templateFactory;
42
43
    /** @var CommandBus */
44
    private $commandBus;
45
46
    /** @var FileSystemFactory */
47
    private $filesystemFactory;
48
49
    /** @var EmitterInterface */
50
    private $emitter;
51
52
    /** @var Assets */
53
    private $assets;
54
55
    /**
56
     * RenderHandler constructor.
57
     *
58
     * @param TemplateFactory $templateFactory
59
     * @param CommandBus $commandBus
60
     * @param FileSystemFactory $filesystemFactory
61
     * @param EmitterInterface $emitter
62
     * @param Assets $assets
63
     */
64
    public function __construct(
65
        TemplateFactory   $templateFactory,
66
        CommandBus        $commandBus,
67
        FileSystemFactory $filesystemFactory,
68
        EmitterInterface  $emitter,
69
        Assets            $assets
70
    ) {
71
        $this->templateFactory   = $templateFactory;
72
        $this->commandBus        = $commandBus;
73
        $this->filesystemFactory = $filesystemFactory;
74
        $this->emitter           = $emitter;
75
        $this->assets            = $assets;
76
    }
77
78
    public function __invoke(Render $command)
79
    {
80
        $this->emitter->emit(new RenderingStarted());
81
82
        $this->renderTemplates(
83
            $command->getTemplates(),
84
            new RenderContext(
85
                $this->createReadModels($command->getDocumentation()),
86
                $this->assets,
87
                $this->createArtefactsLocation($command->getTarget())
0 ignored issues
show
Documentation introduced by
$command->getTarget() is of type string, but the function expects a object<League\Flysystem\Filesystem>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
88
            )
89
        );
90
91
        $this->emitter->emit(new RenderingFinished());
92
    }
93
94
    /**
95
     * @param array $templates
96
     * @param RenderContext $renderContext
97
     */
98
    private function renderTemplates(array $templates, RenderContext $renderContext)
99
    {
100
        foreach ($templates as $templateDefinition) {
101
            $this->renderTemplate($templateDefinition, $renderContext);
102
        }
103
    }
104
105
    /**
106
     * @param array         $templateDefinition
107
     * @param RenderContext $renderContext
108
     */
109
    private function renderTemplate(array $templateDefinition, RenderContext $renderContext)
110
    {
111
        $template = $this->getTemplateFromDefinition($templateDefinition, $renderContext);
112
113
        $this->renderActions($template->getActions());
114
    }
115
116
    /**
117
     * @param array $templateDefinition
118
     * @param RenderContext $renderContext
119
     *
120
     * @return Template
121
     */
122
    private function getTemplateFromDefinition(array $templateDefinition, RenderContext $renderContext)
123
    {
124
        $template = null;
125
        if (isset($templateDefinition['name'])) {
126
            $template = $this->templateFactory->createFromName($renderContext, $templateDefinition['name']);
127
128
            if (! $template instanceof Template) {
129
                throw new \InvalidArgumentException(
130
                    sprintf('The template "%s" could not be found', $templateDefinition['name'])
131
                );
132
            }
133
        } elseif (isset($templateDefinition['path'])) {
134
            $template = $this->templateFactory->createFromUri($renderContext, new Uri($templateDefinition['path']));
135
136
            if (! $template instanceof Template) {
137
                throw new \InvalidArgumentException(
138
                    sprintf('The template at location "%s" could not be found', $templateDefinition['path'])
139
                );
140
            }
141
        }
142
143
        if (! $template instanceof Template) {
144
            throw new \InvalidArgumentException('No template could not be found');
145
        }
146
147
        return $template;
148
    }
149
150
    /**
151
     * @param Action[] $actions
152
     */
153
    private function renderActions(array $actions)
154
    {
155
        foreach ($actions as $action) {
156
            $this->renderAction($action);
157
        }
158
    }
159
160
    /**
161
     * @param Action $action
162
     */
163
    private function renderAction(Action $action)
164
    {
165
        $this->commandBus->handle($action);
166
        $this->emitter->emit(new RenderActionCompleted($action));
167
    }
168
169
    /**
170
     * @param Documentation $documentation
171
     *
172
     * @return ReadModels
173
     */
174
    private function createReadModels(Documentation $documentation)
0 ignored issues
show
Unused Code introduced by
The parameter $documentation is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
175
    {
176
        // TODO: Convert to a series of ReadModels
177
        return new ReadModels();
178
    }
179
180
    /**
181
     * @param Filesystem $destination
182
     *
183
     * @return FlySystemArtefacts
184
     */
185
    private function createArtefactsLocation(Filesystem $destination)
186
    {
187
        return new FlySystemArtefacts($destination);
188
    }
189
}
190