Passed
Pull Request — main (#442)
by MusikAnimal
08:15 queued 04:14
created

EditRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 8
dl 0
loc 13
rs 10
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types = 1);
4
5
namespace App\Repository;
6
7
use App\Helper\AutomatedEditsHelper;
8
use App\Model\Edit;
9
use App\Model\Page;
10
use App\Model\Project;
11
use GuzzleHttp\Client;
12
use Psr\Cache\CacheItemPoolInterface;
13
use Psr\Container\ContainerInterface;
14
use Psr\Log\LoggerInterface;
15
16
/**
17
 * An EditRepository fetches data about a single revision.
18
 * @codeCoverageIgnore
19
 */
20
class EditRepository extends Repository
21
{
22
    protected AutomatedEditsHelper $autoEditsHelper;
23
    protected PageRepository $pageRepo;
24
25
    /**
26
     * @param ContainerInterface $container
27
     * @param CacheItemPoolInterface $cache
28
     * @param Client $guzzle
29
     * @param LoggerInterface $logger
30
     * @param bool $isWMF
31
     * @param int $queryTimeout
32
     * @param AutomatedEditsHelper $autoEditsHelper
33
     * @param PageRepository $pageRepo
34
     */
35
    public function __construct(
36
        ContainerInterface $container,
37
        CacheItemPoolInterface $cache,
38
        Client $guzzle,
39
        LoggerInterface $logger,
40
        bool $isWMF,
41
        int $queryTimeout,
42
        AutomatedEditsHelper $autoEditsHelper,
43
        PageRepository $pageRepo
44
    ) {
45
        $this->autoEditsHelper = $autoEditsHelper;
46
        $this->pageRepo = $pageRepo;
47
        parent::__construct($container, $cache, $guzzle, $logger, $isWMF, $queryTimeout);
48
    }
49
50
    /**
51
     * @return AutomatedEditsHelper
52
     */
53
    public function getAutoEditsHelper(): AutomatedEditsHelper
54
    {
55
        return $this->autoEditsHelper;
56
    }
57
58
    /**
59
     * Get an Edit instance given the revision ID. This does NOT set the associated User or Page.
60
     * @param UserRepository $userRepo
61
     * @param Project $project
62
     * @param int $revId
63
     * @param Page|null $page Provide if you already know the Page, so as to point to the same instance.
64
     *   This should already have the PageRepository set.
65
     * @return Edit|null Null if not found.
66
     */
67
    public function getEditFromRevIdForPage(
68
        UserRepository $userRepo,
69
        Project $project,
70
        int $revId,
71
        ?Page $page = null
72
    ): ?Edit {
73
        $revisionTable = $project->getTableName('revision', '');
74
        $commentTable = $project->getTableName('comment', 'revision');
75
        $actorTable = $project->getTableName('actor', 'revision');
76
        $pageSelect = '';
77
        $pageJoin = '';
78
79
        if (null === $page) {
80
            $pageTable = $project->getTableName('page');
81
            $pageSelect = "page_title,";
82
            $pageJoin = "JOIN $pageTable ON revs.rev_page = page_id";
83
        }
84
85
        $sql = "SELECT $pageSelect
86
                    revs.rev_id AS id,
87
                    actor_name AS username,
88
                    revs.rev_timestamp AS timestamp,
89
                    revs.rev_minor_edit AS minor,
90
                    revs.rev_len AS length,
91
                    (CAST(revs.rev_len AS SIGNED) - IFNULL(parentrevs.rev_len, 0)) AS length_change,
92
                    comment_text AS comment
93
                FROM $revisionTable AS revs
94
                $pageJoin
95
                JOIN $actorTable ON actor_id = rev_actor
96
                LEFT JOIN $revisionTable AS parentrevs ON (revs.rev_parent_id = parentrevs.rev_id)
97
                LEFT OUTER JOIN $commentTable ON (revs.rev_comment_id = comment_id)
98
                WHERE revs.rev_id = :revId";
99
100
        $result = $this->executeProjectsQuery($project, $sql, ['revId' => $revId])
101
            ->fetchAssociative();
102
103
        if (!$result) {
104
            return null;
105
        }
106
107
        // Create the Page instance.
108
        if (null === $page) {
109
            $page = new Page($this->pageRepo, $project, $result['page_title']);
110
        }
111
112
        return new Edit($this, $userRepo, $page, $result);
113
    }
114
115
    /**
116
     * Use the Compare API to get HTML for the diff.
117
     * @param Edit $edit
118
     * @return string|null Raw HTML, must be wrapped in a <table> tag. Null if no comparison found.
119
     */
120
    public function getDiffHtml(Edit $edit): ?string
121
    {
122
        $params = [
123
            'action' => 'compare',
124
            'fromrev' => $edit->getId(),
125
            'torelative' => 'prev',
126
        ];
127
128
        $res = $this->executeApiRequest($edit->getProject(), $params);
129
        return $res['compare']['*'] ?? null;
130
    }
131
}
132