Completed
Push — master ( 0645c0...b651ea )
by Jaap
03:02
created

ParseGuides::__invoke()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
nc 7
nop 1
dl 0
loc 39
ccs 0
cts 28
cp 0
crap 30
rs 8.9848
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of phpDocumentor.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * @link https://phpdoc.org
12
 */
13
14
namespace phpDocumentor\Pipeline\Stage\Parser;
15
16
use League\Tactician\CommandBus;
17
use phpDocumentor\Descriptor\GuideSetDescriptor;
18
use phpDocumentor\Guides\Configuration;
19
use phpDocumentor\Guides\Formats\Format;
20
use phpDocumentor\Guides\RestructuredText\ParseDirectoryCommand;
21
use phpDocumentor\Parser\FlySystemFactory;
22
use Psr\Log\LoggerInterface;
23
use Psr\Log\LogLevel;
24
25
final class ParseGuides
26
{
27
    /** @var CommandBus */
28
    private $commandBus;
29
30
    /** @var LoggerInterface */
31
    private $logger;
32
33
    /** @var iterable<Format> */
34
    private $outputFormats;
35
36
    /** @var FlySystemFactory */
37
    private $flySystemFactory;
38
39
    /**
40
     * @param iterable<Format> $outputFormats
0 ignored issues
show
Documentation introduced by
The doc-type iterable<Format> could not be parsed: Expected "|" or "end of type", but got "<" at position 8. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
41
     */
42
    public function __construct(
43
        CommandBus $commandBus,
44
        LoggerInterface $logger,
45
        FlySystemFactory $flySystemFactory,
46
        iterable $outputFormats
47
    ) {
48
        $this->commandBus = $commandBus;
49
        $this->logger = $logger;
50
        $this->outputFormats = $outputFormats;
51
        $this->flySystemFactory = $flySystemFactory;
52
    }
53
54
    public function __invoke(Payload $payload) : Payload
55
    {
56
        if ($payload->getConfig()['phpdocumentor']['settings']['guides.enabled'] !== true) {
57
            return $payload;
58
        }
59
60
        /*
61
         * For now settings of the first guides are used.
62
         * We need to change this later, when we accept more different things
63
         */
64
        $version = $payload->getBuilder()->getProjectDescriptor()->getVersions()->get(0);
65
        $guideDocumentationSet = null;
66
        foreach ($version->getDocumentationSets() as $set) {
67
            if ($set instanceof GuideSetDescriptor) {
68
                $guideDocumentationSet = $set;
69
                break;
70
            }
71
        }
72
73
        if ($guideDocumentationSet === null) {
74
            return $payload;
75
        }
76
77
        $this->log('Parsing guides', LogLevel::NOTICE);
78
79
        $dsn = $guideDocumentationSet->getSource()->dsn();
80
        $origin = $this->flySystemFactory->create($dsn);
81
        $directory = $guideDocumentationSet->getSource()->paths()[0] ?? '';
82
83
        $configuration = new Configuration(
84
            $guideDocumentationSet->getInputFormat(),
85
            $this->outputFormats
86
        );
87
        $configuration->setOutputFolder($guideDocumentationSet->getOutput());
88
89
        $this->commandBus->handle(new ParseDirectoryCommand($configuration, $origin, (string) $directory));
90
91
        return $payload;
92
    }
93
94
    /**
95
     * Dispatches a logging request.
96
     *
97
     * @param string $priority The logging priority as declared in the LogLevel PSR-3 class.
98
     * @param string[] $parameters
99
     */
100
    private function log(string $message, string $priority = LogLevel::INFO, array $parameters = []) : void
101
    {
102
        $this->logger->log($priority, $message, $parameters);
103
    }
104
}
105