Completed
Push — master ( 346372...987832 )
by Matt
04:57
created

ProblemService   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 44
c 2
b 0
f 0
dl 0
loc 94
rs 10
wmc 12

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 1
B createProblemReport() 0 57 11
1
<?php
2
3
namespace App\Service;
4
5
use App\Entity\Problem;
6
use App\Repository\ImageRepository;
7
use App\Repository\ProblemRepository;
8
use App\Repository\WanderRepository;
9
use Doctrine\ORM\EntityManagerInterface;
10
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
11
use Symfony\Component\Routing\RouterInterface;
12
13
class ProblemService
14
{
15
    /** @var ProblemRepository */
16
    private $problemRepository;
17
18
    /** @var WanderRepository */
19
    private $wanderRepository;
20
21
    /** @var ImageRepository */
22
    private $imageRepository;
23
24
    /** @var RouterInterface */
25
    private $router;
26
27
    /** @var MarkdownService */
28
    private $markdownService;
29
30
    /** @var EntityManagerInterface */
31
    private $entityManager;
32
33
    public function __construct(
34
        problemRepository $problemRepository,
35
        WanderRepository $wanderRepository,
36
        ImageRepository $imageRepository,
37
        RouterInterface $router,
38
        MarkdownService $markdownService,
39
        EntityManagerInterface $entityManager
40
        )
41
    {
42
        $this->problemRepository = $problemRepository;
43
        $this->wanderRepository = $wanderRepository;
44
        $this->imageRepository = $imageRepository;
45
        $this->router = $router;
46
        $this->markdownService = $markdownService;
47
        $this->entityManager = $entityManager;
48
    }
49
50
    public function createProblemReport(): void
51
    {
52
        // I don't care about concurrency; there's only me using this and
53
        // I can always run it again if there's a problem; it's an unimportant
54
        // operation.
55
        $this->problemRepository->clearAllProblems();
56
57
        // Build a hash of all possible valid URLs for wanders and
58
        // images to compare what we find in descriptions, etc. with.
59
        $validUris = [];
60
        $wanders = $this->wanderRepository->findAll();
61
        foreach ($wanders as $wander) {
62
            $uri = $this->router->generate('wanders_show', [ 'id' => $wander->getId()], UrlGeneratorInterface::ABSOLUTE_URL);
63
            $validUris[$uri] = true; // Really I just want a hash that doesn't actually map
64
        }
65
66
        $images = $this->imageRepository->findAll();
67
        foreach ($images as $image) {
68
            $uri = $this->router->generate('image_show', [ 'id' => $image->getId()], UrlGeneratorInterface::ABSOLUTE_URL);
69
            $validUris[$uri] = true; // Really I just want a hash that doesn't actually map
70
        }
71
        //dd($validUris);
72
73
        $homepage = $this->router->generate('home', [], RouterInterface::ABSOLUTE_URL);
74
75
        // Okay, now we've got a list of all valid URIs, let's have a look through all our descriptions
76
77
        // Wanders first...
78
        foreach($wanders as $wander) {
79
            $uris = $this->markdownService->findLinks($wander->getDescription());
80
            foreach ($uris as $uri) {
81
                if (substr($uri, 0, strlen($homepage)) == $homepage) {
82
                    if (!array_key_exists($uri, $validUris)) {
83
                        $problem = new Problem();
84
                        $problem->setDescription('Wander ' . $wander->getId() . ' links to invalid URI: ' . $uri);
85
                        $problem->setUri($this->router->generate('wanders_show', [ 'id' => $wander->getId()], UrlGeneratorInterface::ABSOLUTE_URL));
86
                        $this->entityManager->persist($problem);
87
                    }
88
                }
89
            }
90
        }
91
        $this->entityManager->flush();
92
        // ...then Images:
93
        foreach($images as $image) {
94
            $uris = $this->markdownService->findLinks($image->getDescription());
95
            foreach ($uris as $uri) {
96
                if (substr($uri, 0, strlen($homepage)) == $homepage) {
97
                    if (!array_key_exists($uri, $validUris)) {
98
                        $problem = new Problem();
99
                        $problem->setDescription('Image ' . $image->getId() . ' links to invalid URI: ' . $uri);
100
                        $problem->setUri($this->router->generate('image_show', [ 'id' => $image->getId()], UrlGeneratorInterface::ABSOLUTE_URL));
101
                        $this->entityManager->persist($problem);
102
                    }
103
                }
104
            }
105
        }
106
        $this->entityManager->flush();
107
    }
108
}