ViolationExporterFactory   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 44
ccs 11
cts 11
cp 1
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 19 2
A getOutputFormats() 0 3 1
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