Completed
Push — master ( 7c8f3d...23b833 )
by Níckolas Daniel
05:40
created

JigsawGenerateOldPagesAfterBuild::handle()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 37
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 37
ccs 0
cts 23
cp 0
rs 9.552
c 0
b 0
f 0
cc 3
nc 1
nop 1
crap 12
1
<?php
2
3
namespace PODEntender\Infrastructure\Application\StaticSite;
4
5
use Illuminate\Filesystem\Filesystem;
6
use Illuminate\Support\Str;
7
use PODEntender\Domain\Model\FileProcessing\OutputFile;
8
use PODEntender\Domain\Model\FileProcessing\OutputFileRepository;
9
use Symfony\Component\DomCrawler\Crawler;
10
use TightenCo\Jigsaw\Jigsaw;
11
12
class JigsawGenerateOldPagesAfterBuild implements JigsawEventHandler
13
{
14
    public function handle(Jigsaw $jigsaw): void
15
    {
16
        /** @var OutputFileRepository $outputFileRepository */
17
        $outputFileRepository = $jigsaw->app->make(OutputFileRepository::class);
18
        $filesystem = new Filesystem();
19
20
        $outputFileRepository->all()
21
            ->filter(function (OutputFile $file) {
22
                if (!Str::contains($file->path(), 'episodio/')) {
23
                    return false;
24
                }
25
26
                $crawler = new Crawler();
27
                $crawler->addHtmlContent($file->content());
28
29
                return $crawler->filter('link[rel="oldLink"]')->count();
30
            })
31
            ->each(function (OutputFile $file) use ($jigsaw, $filesystem) {
32
                $crawler = new Crawler();
33
                $crawler->addHtmlContent($file->content());
34
35
                $newFile = Str::replaceFirst(
36
                    'https://podentender.com',
37
                    $jigsaw->getDestinationPath(),
38
                    $crawler->filter('link[rel="oldLink"]')->first()->attr('href')
39
                );
40
41
                $dirname = $filesystem->dirname($newFile);
42
                if (!$filesystem->exists($dirname)) {
43
                    $filesystem->makeDirectory(
44
                        $filesystem->dirname($newFile),
45
                        0777,
46
                        true
47
                    );
48
                }
49
50
                $filesystem->put($newFile, $file->content());
51
            });
52
    }
53
}
54