Passed
Push — search ( bdb480...5e7f6e )
by Simon
17:14 queued 07:17
created

RequestQueueHelper::configureDefaults()   C

Complexity

Conditions 12
Paths 4

Size

Total Lines 22
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
dl 0
loc 22
rs 6.9666
c 1
b 0
f 0
cc 12
nc 4
nop 6

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/******************************************************************************
3
 * Wikipedia Account Creation Assistance tool                                 *
4
 *                                                                            *
5
 * All code in this file is released into the public domain by the ACC        *
6
 * Development Team. Please see team.json for a list of contributors.         *
7
 ******************************************************************************/
8
9
namespace Waca\Helpers;
10
11
use Waca\DataObjects\EmailTemplate;
12
use Waca\DataObjects\RequestQueue;
13
use Waca\PdoDatabase;
14
15
class RequestQueueHelper
16
{
17
    /**
18
     * @param RequestQueue $queue
19
     * @param bool         $enabled
20
     * @param bool         $default
21
     * @param bool         $antiSpoof
22
     * @param bool         $titleBlacklist
23
     */
24
    public function configureDefaults(
25
        RequestQueue $queue,
26
        bool $enabled,
27
        bool $default,
28
        bool $antiSpoof,
29
        bool $titleBlacklist,
30
        bool $isTarget
31
    ) {
32
        // always allow enabling a queue
33
        if ($enabled) {
34
            $queue->setEnabled($enabled);
35
        }
36
37
        // only allow other enable-flag changes if we're not a default
38
        if (!($queue->isDefault() || $queue->isDefaultAntispoof() || $queue->isDefaultTitleBlacklist() || $isTarget)) {
39
            $queue->setEnabled($enabled);
40
        }
41
42
        // only allow enabling the default flags, and only when we're enabled.
43
        $queue->setDefault(($queue->isDefault() || $default) && $queue->isEnabled());
44
        $queue->setDefaultAntispoof(($queue->isDefaultAntispoof() || $antiSpoof) && $queue->isEnabled());
45
        $queue->setDefaultTitleBlacklist(($queue->isDefaultTitleBlacklist() || $titleBlacklist) && $queue->isEnabled());
46
    }
47
48
    /**
49
     * @param RequestQueue $queue
50
     * @param PdoDatabase  $database
51
     *
52
     * @return bool
53
     */
54
    public function isEmailTemplateTarget(RequestQueue $queue, PdoDatabase $database): bool
55
    {
56
        $isTarget = false;
57
        /** @var EmailTemplate[] $deferralTemplates */
58
        $deferralTemplates = EmailTemplate::getAllActiveTemplates('defer', $database);
59
        foreach ($deferralTemplates as $t) {
60
            if ($t->getQueue() === $queue->getId()) {
61
                $isTarget = true;
62
                break;
63
            }
64
        }
65
66
        return $isTarget;
67
    }
68
}