Completed
Push — master ( a33dc3...ad6b9e )
by Marco
10s
created

MarkdownPipedToSymfonyConsoleFormatter::write()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 17
nc 1
nop 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Roave\ApiCompare\Formatter;
5
6
use Roave\ApiCompare\Change;
7
use Roave\ApiCompare\Changes;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
final class MarkdownPipedToSymfonyConsoleFormatter implements OutputFormatter
11
{
12
    /**
13
     * @var OutputInterface
14
     */
15
    private $output;
16
17
    public function __construct(OutputInterface $output)
18
    {
19
        $this->output = $output;
20
    }
21
22
    public function write(Changes $changes) : void
23
    {
24
        $arrayOfChanges = $changes->getIterator()->getArrayCopy();
25
26
        $this->output->writeln(
27
            "# Added\n"
28
            . implode('', $this->convertFilteredChangesToMarkdownBulletList(
29
                function (Change $change) : bool {
30
                    return $change->isAdded();
31
                },
32
                ...$arrayOfChanges
33
            ))
34
            . "\n# Changed\n"
35
            . implode('', $this->convertFilteredChangesToMarkdownBulletList(
36
                function (Change $change) : bool {
37
                    return $change->isChanged();
38
                },
39
                ...$arrayOfChanges
40
            ))
41
            . "\n# Removed\n"
42
            . implode('', $this->convertFilteredChangesToMarkdownBulletList(
43
                function (Change $change) : bool {
44
                    return $change->isRemoved();
45
                },
46
                ...$arrayOfChanges
47
            ))
48
        );
49
    }
50
51
    private function convertFilteredChangesToMarkdownBulletList(callable $filterFunction, Change ...$changes) : array
52
    {
53
        return array_map(
54
            function (Change $change) : string {
55
                return ' - ' . str_replace(['ADDED: ', 'CHANGED: ', 'REMOVED: '], '', trim((string)$change)) . "\n";
56
            },
57
            array_filter($changes, $filterFunction)
58
        );
59
    }
60
}
61