Completed
Push — master ( 353f9b...9d046b )
by Níckolas Daniel
05:04
created

PostProcessFiles::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 18
ccs 11
cts 11
cp 1
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
namespace PODEntender\Application\Service\FileProcessing;
4
5
use PODEntender\Domain\Model\FileProcessing\OutputFile;
6
use PODEntender\Domain\Model\FileProcessing\OutputFileCollection;
7
use PODEntender\Domain\Model\FileProcessing\OutputFileRepository;
8
use Symfony\Component\DomCrawler\Crawler;
9
10
class PostProcessFiles
11
{
12
    private $outputFilesRepository;
13
14 3
    public function __construct(OutputFileRepository $outputFilesRepository)
15
    {
16 3
        $this->outputFilesRepository = $outputFilesRepository;
17 3
    }
18
19 3
    public function execute(): OutputFileCollection
20
    {
21 3
        $files = $this->outputFilesRepository->all();
22 3
        $processedFiles = new OutputFileCollection();
23
24 3
        foreach ($files as $file) {
25 3
            $crawler = new Crawler();
26 3
            $crawler->addHtmlContent($file->content());
27
28 3
            $this->decorateParagraphs($crawler);
29 3
            $this->decorateHeadings($crawler);
30
31 3
            $processedFiles->add(
32 3
                new OutputFile($file->path(), $crawler->getNode(0)->ownerDocument->saveHTML())
33
            );
34
        }
35
36 3
        return $processedFiles;
37
    }
38
39 3
    private function decorateParagraphs(Crawler $crawler): void
40
    {
41 3
        foreach ($crawler->filter('.paragraphs-list p') as $paragraph) {
42 3
            $classes = array_merge(
43 3
                explode(' ', $paragraph->getAttribute('class')),
44 3
                ['paragraph paragraph--justified']
45
            );
46
47 3
            $paragraph->setAttribute('class', trim(implode(' ', $classes)));
48
        }
49
50 3
        foreach ($crawler->filter('.paragraphs-list strong') as $boldItem) {
51 3
            $classes = array_merge(
52 3
                explode(' ', $boldItem->getAttribute('class')),
53 3
                ['paragraph--bold']
54
            );
55
56 3
            $boldItem->setAttribute('class', trim(implode(' ', $classes)));
57
        }
58
59 3
        foreach ($crawler->filter('.paragraphs-list a') as $linkItem) {
60 3
            $classes = array_merge(
61 3
                explode(' ', $linkItem->getAttribute('class')),
62 3
                ['link']
63
            );
64
65 3
            $linkItem->setAttribute('class', trim(implode(' ', $classes)));
66 3
            $linkItem->setAttribute('target', '_blank');
67
        }
68 3
    }
69
70 3
    private function decorateHeadings(Crawler $crawler): void
71
    {
72 3
        $secondaryHeadings = ['.paragraphs-list h2', '.paragraphs-list h3'];
73 3
        $filter = implode(',', $secondaryHeadings);
74 3
        foreach ($crawler->filter($filter) as $heading) {
75 3
            $classes = array_merge(
76 3
                explode(' ', $heading->getAttribute('class')),
77 3
                ['heading', 'heading__secondary']
78
            );
79
80 3
            $heading->setAttribute('class', trim(implode(' ', $classes)));
81
        }
82 3
    }
83
}
84