PostProcessFiles::decorateParagraphs()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 28
ccs 17
cts 17
cp 1
rs 9.7333
c 0
b 0
f 0
cc 4
nc 8
nop 1
crap 4
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 PODEntender\Domain\Model\Post\Post;
9
use PODEntender\Domain\Model\Post\PostRepository;
10
use Symfony\Component\DomCrawler\Crawler;
11
12
class PostProcessFiles
13
{
14
    private $outputFilesRepository;
15
16
    private $postRepository;
17
18 5
    public function __construct(OutputFileRepository $outputFilesRepository, PostRepository $postRepository)
19
    {
20 5
        $this->outputFilesRepository = $outputFilesRepository;
21 5
        $this->postRepository = $postRepository;
22 5
    }
23
24 5
    public function execute(bool $production): OutputFileCollection
25
    {
26 5
        $files = $this->outputFilesRepository->all();
27 5
        $processedFiles = new OutputFileCollection();
28
29 5
        foreach ($files as $file) {
30 5
            $crawler = new Crawler();
31 5
            $crawler->addHtmlContent($file->content());
32
33 5
            $this->decorateParagraphs($crawler);
34 5
            $this->decorateHeadings($crawler);
35
36 5
            $content = $crawler->getNode(0)->ownerDocument->saveHTML();
37
38 5
            if ($production) {
39 4
                $content = $this->decorateJekyllRedirects($crawler, $content);
40
            }
41
42 5
            $processedFiles->add(
43 5
                new OutputFile($file->path(), $content)
44
            );
45
        }
46
47 5
        return $processedFiles;
48
    }
49
50 4
    private function decorateJekyllRedirects(Crawler $crawler, string $content): string
51
    {
52 4
        $canonical = $crawler->filter('link[rel="canonical"]');
53 4
        if ($canonical->count() === 0) {
54
            return $content;
55
        }
56
57 4
        $canonicalLink = $canonical->first()->attr('href');
58
        $post = $this->postRepository->withAudio()->filter(function (Post $post) use ($canonicalLink) {
59 4
            return $canonicalLink === $post->url();
60 4
        })->first();
61
62 4
        if (is_null($post) || count($post->redirects()) === 0) {
63
            return $content;
64
        }
65
66
        $header = [
67 4
            '---' ,
68
            'redirect_from:',
69
        ];
70
71 4
        foreach ($post->redirects() as $redirect) {
72 4
            $uri = parse_url($redirect)['path'];
73 4
            if ($uri !== '/') {
74 4
                $header[] = sprintf('  - "%s"', parse_url($redirect)['path']);
75
            }
76
        }
77
78 4
        $header[] = '---';
79
80 4
        return implode(PHP_EOL, $header) . PHP_EOL . $content;
81
    }
82
83 5
    private function decorateParagraphs(Crawler $crawler): void
84
    {
85 5
        foreach ($crawler->filter('.paragraphs-list p') as $paragraph) {
86 5
            $classes = array_merge(
87 5
                explode(' ', $paragraph->getAttribute('class')),
88 5
                ['paragraph']
89
            );
90
91 5
            $paragraph->setAttribute('class', trim(implode(' ', $classes)));
92
        }
93
94 5
        foreach ($crawler->filter('.paragraphs-list strong') as $boldItem) {
95 5
            $classes = array_merge(
96 5
                explode(' ', $boldItem->getAttribute('class')),
97 5
                ['paragraph--bold']
98
            );
99
100 5
            $boldItem->setAttribute('class', trim(implode(' ', $classes)));
101
        }
102
103 5
        foreach ($crawler->filter('.paragraphs-list a') as $linkItem) {
104 5
            $classes = array_merge(
105 5
                explode(' ', $linkItem->getAttribute('class')),
106 5
                ['link']
107
            );
108
109 5
            $linkItem->setAttribute('class', trim(implode(' ', $classes)));
110 5
            $linkItem->setAttribute('target', '_blank');
111
        }
112 5
    }
113
114 5
    private function decorateHeadings(Crawler $crawler): void
115
    {
116 5
        $secondaryHeadings = ['.paragraphs-list h2', '.paragraphs-list h3'];
117 5
        $filter = implode(',', $secondaryHeadings);
118 5
        foreach ($crawler->filter($filter) as $heading) {
119 5
            $classes = array_merge(
120 5
                explode(' ', $heading->getAttribute('class')),
121 5
                ['heading', 'heading__secondary']
122
            );
123
124 5
            $heading->setAttribute('class', trim(implode(' ', $classes)));
125
        }
126 5
    }
127
}
128