Completed
Push — master ( 8cd7b5...0a4adb )
by Matt
05:29
created

ProblemController::throw()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace App\Controller\Admin;
4
5
use App\Entity\Wander;
6
use App\Repository\ImageRepository;
7
use App\Repository\ProblemRepository;
8
use App\Repository\WanderRepository;
9
use App\Service\ProblemService;
10
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
11
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Component\HttpFoundation\Response;
13
use Symfony\Component\Routing\Annotation\Route;
14
15
/**
16
 * @Route("/admin/problems", name="admin_problems_")
17
 */
18
class ProblemController extends AbstractController
19
{
20
   /**
21
     * @Route("/throw", name="throw", methods={"GET"})
22
     */
23
    public function throw(): Response
24
    {
25
        throw new \Exception("DIEDIEDIE!");
26
    }
27
28
    /**
29
     * @Route("/", name="index", methods={"GET"})
30
     */
31
    public function index(
32
        WanderRepository $wanderRepository,
33
        ImageRepository $imageRepository,
34
        ProblemRepository $problemRepository
35
        ): Response
36
    {
37
        // Problems that might take time/resources to find, that are
38
        // popped into a table on specific demand rather than every
39
        // time we load this page:
40
        $builtProblems = $problemRepository->findAll();
41
42
        // TODO: Might be sensible to do everything here into the problems
43
        // table, depending on how much we add to this.
44
        // Other issues, found on the fly:
45
        $qb = $wanderRepository->createQueryBuilder('w');
46
47
        // TODO: This doesn't work, as e.g. keywords being an empty array
48
        //
49
        $problems = $qb
50
            ->join('w.images', 'i')
51
            ->select('w AS wander')
52
            ->addSelect('COUNT(i) AS image_count')
53
            ->addSelect('SUM(CASE WHEN i.title IS NULL THEN 1 ELSE 0 END) AS no_title')
54
            ->addSelect('SUM(CASE WHEN i.latlng IS NULL THEN 1 ELSE 0 END) AS no_latlng')
55
            ->addSelect('SUM(CASE WHEN i.location IS NULL THEN 1 ELSE 0 END) AS no_location')
56
            ->addSelect('SUM(CASE WHEN i.rating IS NULL OR i.rating = 0 THEN 1 ELSE 0 END) AS no_rating')
57
            // TODO: This is a hideous bodge and will break when we finally give in and move
58
            // keywords and auto-tags to being related entities rather than a dirty PHP
59
            // array, but it's good enough for a problem admin page for now.
60
            //->addSelect("SUM(CASE WHEN i.keywords IS NULL OR i.keywords = 'a:0:{}' THEN 1 ELSE 0 END) AS no_keywords")
61
            ->addSelect("SUM(CASE WHEN i.tags is empty THEN 1 ELSE 0 END) AS no_tags")
62
            ->addSelect("SUM(CASE WHEN i.auto_tags IS NULL OR i.auto_tags = 'a:0:{}' THEN 1 ELSE 0 END) AS no_auto_tags")
63
64
            ->addSelect(
65
                "(SUM(CASE WHEN i.title IS NULL THEN 1 ELSE 0 END)) + " .
66
                "(SUM(CASE WHEN i.latlng IS NULL THEN 1 ELSE 0 END)) + " .
67
                "(SUM(CASE WHEN i.location IS NULL THEN 1 ELSE 0 END)) + " .
68
                "(SUM(CASE WHEN i.rating IS NULL OR i.rating = 0 THEN 1 ELSE 0 END)) + " .
69
                "(SUM(CASE WHEN i.tags is empty THEN 1 ELSE 0 END)) AS total_problems_excl_auto")
70
            ->addSelect(
71
                "(SUM(CASE WHEN i.title IS NULL THEN 1 ELSE 0 END)) + " .
72
                "(10 * SUM(CASE WHEN i.latlng IS NULL THEN 1 ELSE 0 END)) + " .
73
                "(2 * SUM(CASE WHEN i.location IS NULL THEN 1 ELSE 0 END)) + " .
74
                "(5 * SUM(CASE WHEN i.rating IS NULL OR i.rating = 0 THEN 1 ELSE 0 END)) + " .
75
                "(0.01 * SUM(CASE WHEN i.tags is empty THEN 1 ELSE 0 END)) + " .
76
                "(0.001 * SUM(CASE WHEN i.auto_tags IS NULL OR i.auto_tags = 'a:0:{}' THEN 1 ELSE 0 END)) AS weighted_problem_score")
77
            ->addGroupBy('w')
78
            ->having('no_title > 0')
79
            ->orHaving('no_latlng > 0')
80
            ->orHaving('no_location > 0')
81
            ->orHaving('no_tags > 0')
82
            ->orHaving('no_auto_tags > 0')
83
            ->orderBy('weighted_problem_score', 'desc')
84
            ->getQuery()
85
            ->getResult();
86
87
        $orphans = $imageRepository->findWithNoWander();
88
89
        return $this->render('admin/problems/index.html.twig', [
90
            'problems' => $problems,
91
            'orphans' => $orphans,
92
            'built_problems' => $builtProblems
93
        ]);
94
    }
95
96
    /**
97
     * @Route("/regenerate", name="regenerate", methods={"POST"})
98
     */
99
    public function regenerateProblems(Request $request, ProblemService $problemService): Response
100
    {
101
        if ($this->isCsrfTokenValid('problems_regenerate', $request->request->get('_token'))) {
102
            $problemService->createProblemReport();
103
        }
104
105
        return $this->redirectToRoute('admin_problems_index');
106
    }
107
108
109
    // TODO: These all share a lot of things in common, but I'm not sure
110
    // how much this is going to get used, so I'm keeping it fairly brain-dead
111
    // for now. Revisit later.
112
113
    /**
114
     * @Route("/no_title/wander/{id}", name="no_title", methods={"GET"})
115
     */
116
    public function noTitle(Wander $wander): Response
117
    {
118
        return $this->render('admin/problems/no_title.html.twig', [
119
            'wander' => $wander
120
        ]);
121
    }
122
    /**
123
     * @Route("/no_latlng/wander/{id}", name="no_latlng", methods={"GET"})
124
     */
125
    public function noLatlng(Wander $wander): Response
126
    {
127
        return $this->render('admin/problems/no_latlng.html.twig', [
128
            'wander' => $wander
129
        ]);
130
    }
131
    /**
132
     * @Route("/no_location/wander/{id}", name="no_location", methods={"GET"})
133
     */
134
    public function noLocation(Wander $wander): Response
135
    {
136
        return $this->render('admin/problems/no_location.html.twig', [
137
            'wander' => $wander
138
        ]);
139
    }
140
    /**
141
     * @Route("/no_rating/wander/{id}", name="no_rating", methods={"GET"})
142
     */
143
    public function noRating(Wander $wander): Response
144
    {
145
        return $this->render('admin/problems/no_rating.html.twig', [
146
            'wander' => $wander
147
        ]);
148
    }
149
150
    /**
151
     * @Route("/no_tags/wander/{id}", name="no_tags", methods={"GET"})
152
     */
153
    public function noTags(Wander $wander): Response
154
    {
155
        return $this->render('admin/problems/no_tags.html.twig', [
156
            'wander' => $wander
157
        ]);
158
    }
159
160
    /**
161
     * @Route("/no_auto_tags/wander/{id}", name="no_auto_tags", methods={"GET"})
162
     */
163
    public function noAutoTags(Wander $wander): Response
164
    {
165
        return $this->render('admin/problems/no_auto_tags.html.twig', [
166
            'wander' => $wander
167
        ]);
168
    }
169
170
    /**
171
     * @Route("/broken_links", name="broken_links", methods={"GET"})
172
     */
173
    public function brokenLinks(ProblemRepository $problemRepository): Response
174
    {
175
        $problems = $problemRepository->findAll();
176
        return $this->render('admin/problems/broken_links.html.twig', [
177
            'problems' => $problems
178
        ]);
179
    }
180
}
181