Passed
Push — master ( 980a19...125389 )
by MusikAnimal
05:36
created

PageAssessmentsRepository::getAssessments()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 14
nc 3
nop 2
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file contains only the PageAssessmentsRepository class.
4
 */
5
6
namespace Xtools;
7
8
/**
9
 * An PageAssessmentsRepository is responsible for retrieving page assessment
10
 * information from the database, and the XTools configuration via the Container.
11
 * @codeCoverageIgnore
12
 */
13
class PageAssessmentsRepository extends Repository
14
{
15
    /**
16
     * Get page assessments configuration for the Project.
17
     * @param Project $project
18
     * @return string[]|bool As defined in config/assessments.yml, or false if none exists.
19
     */
20
    public function getConfig(Project $project)
21
    {
22
        $projectsConfig = $this->container->getParameter('assessments');
23
24
        if (isset($projectsConfig[$project->getDomain()])) {
25
            return $projectsConfig[$project->getDomain()];
26
        } else {
27
            return false;
28
        }
29
    }
30
31
    /**
32
     * Get assessment data for the given pages
33
     * @param Page $page
34
     * @param bool $first Fetch only the first result, not for each WikiProject.
35
     * @return string[] Assessment data as retrieved from the database.
36
     */
37
    public function getAssessments(Page $page, $first = false)
38
    {
39
        $cacheKey = $this->getCacheKey(func_get_args(), 'page_assessments');
40
        if ($this->cache->hasItem($cacheKey)) {
41
            return $this->cache->getItem($cacheKey)->get();
42
        }
43
44
        $paTable = $this->getTableName($page->getProject()->getDatabaseName(), 'page_assessments');
45
        $papTable = $this->getTableName($page->getProject()->getDatabaseName(), 'page_assessments_projects');
46
        $pageId = $page->getId();
47
48
        $sql = "SELECT pap_project_title AS wikiproject, pa_class AS class, pa_importance AS importance
49
                FROM $paTable
50
                LEFT JOIN $papTable ON pa_project_id = pap_project_id
51
                WHERE pa_page_id = $pageId";
52
53
        if ($first) {
54
            $sql .= "\nLIMIT 1";
55
        }
56
57
        $result = $this->executeProjectsQuery($sql)->fetchAll();
58
59
        // Cache and return.
60
        return $this->setCache($cacheKey, $result);
61
    }
62
}
63