ViolationExporterFactory::getOutputFormats()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
/**
3
 * Copyright MediaCT. All rights reserved.
4
 * https://www.mediact.nl
5
 */
6
7
namespace Mediact\DependencyGuard\Composer\Command\Exporter;
8
9
use Mediact\DependencyGuard\Exporter\ViolationExporterInterface;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Symfony\Component\Console\Style\SymfonyStyle;
13
14
class ViolationExporterFactory implements ViolationExporterFactoryInterface
15
{
16
    private const EXPORTERS = [
17
        'text' => TextViolationExporter::class,
18
        'json' => JsonViolationExporter::class
19
    ];
20
21
    /**
22
     * Create a violation exporter for the given input and output.
23
     *
24
     * @param InputInterface  $input
25
     * @param OutputInterface $output
26
     *
27
     * @return ViolationExporterInterface
28
     */
29 1
    public function create(
30
        InputInterface $input,
31
        OutputInterface $output
32
    ): ViolationExporterInterface {
33 1
        $format = $input->hasOption('format')
34 1
            ? $input->getOption('format')
35 1
            : static::DEFAULT_FORMAT;
36
37
        $class = (
38 1
            static::EXPORTERS[$format]
39 1
            ?? static::EXPORTERS[static::DEFAULT_FORMAT]
40
        );
41
42
        /** @var ViolationExporterInterface $exporter */
43 1
        $exporter = new $class(
44 1
            new SymfonyStyle($input, $output)
45
        );
46
47 1
        return $exporter;
48
    }
49
50
    /**
51
     * Get a list of output formats.
52
     *
53
     * @return string[]
54
     */
55 1
    public function getOutputFormats(): array
56
    {
57 1
        return array_keys(static::EXPORTERS);
58
    }
59
}
60