Completed
Push — 1.10.x ( 2635b5...f0080b )
by Yannick
165:55 queued 120:08
created

AbstractMigrationChamilo::addSettingCurrent()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 53
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 43
nc 2
nop 13
dl 0
loc 53
rs 8.7155
c 0
b 0
f 0

How to fix   Long Method    Many Parameters   

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:

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
/* For licensing terms, see /license.txt */
3
4
namespace Application\Migrations;
5
6
use Chamilo\CoreBundle\Entity\SettingsCurrent;
7
use Chamilo\CoreBundle\Entity\SettingsOptions;
8
use Doctrine\DBAL\Migrations\AbstractMigration;
9
use Doctrine\ORM\EntityManager;
10
11
/**
12
 * Class AbstractMigrationChamilo
13
 *
14
 * @package Chamilo\CoreBundle\Migrations
15
 */
16
abstract class AbstractMigrationChamilo extends AbstractMigration
17
{
18
    private $manager;
19
20
    /**
21
     * @param EntityManager $manager
22
     */
23
    public function setEntityManager(EntityManager $manager)
24
    {
25
        $this->manager = $manager;
26
    }
27
28
    /**
29
     * @return EntityManager
30
     */
31
    public function getEntityManager()
32
    {
33
        if (empty($this->manager)) {
34
            $dbParams = array(
35
                'driver' => 'pdo_mysql',
36
                'host' => $this->connection->getHost(),
37
                'user' => $this->connection->getUsername(),
38
                'password' => $this->connection->getPassword(),
39
                'dbname' => $this->connection->getDatabase(),
40
                'port' => $this->connection->getPort()
41
            );
42
            $database = new \Database();
43
            $database->connect(
44
                $dbParams,
45
                __DIR__.'/../../',
46
                __DIR__.'/../../'
47
            );
48
            $this->manager = $database->getManager();
49
        }
50
51
        return $this->manager;
52
    }
53
54
    /**
55
     * Speeds up SettingsCurrent creation
56
     * @param string $variable The variable itself
57
     * @param string $subKey The subkey
58
     * @param string $type The type of setting (text, radio, select, etc)
59
     * @param string $category The category (Platform, User, etc)
60
     * @param string $selectedValue The default value
61
     * @param string $title The setting title string name
62
     * @param string $comment The setting comment string name
63
     * @param string $scope The scope
64
     * @param string $subKeyText Text if there is a subKey
65
     * @param int $accessUrl What URL it is for
66
     * @param bool $accessUrlChangeable Whether it can be changed on each url
67
     * @param bool $accessUrlLocked Whether the setting for the current URL is
68
     * locked to the current value
69
     * @param array $options Optional array in case of a radio-type field,
70
     * to insert options
71
     */
72
    public function addSettingCurrent(
73
        $variable,
74
        $subKey,
75
        $type,
76
        $category,
77
        $selectedValue,
78
        $title,
79
        $comment,
80
        $scope = '',
81
        $subKeyText = '',
82
        $accessUrl = 1,
83
        $accessUrlChangeable = false,
84
        $accessUrlLocked = true,
85
        $options = array()
86
    ) {
87
        $setting = new SettingsCurrent();
88
        $setting
89
            ->setVariable($variable)
90
            ->setSubkey($subKey)
91
            ->setType($type)
92
            ->setCategory($category)
93
            ->setSelectedValue($selectedValue)
94
            ->setTitle($title)
95
            ->setComment($comment)
96
            ->setScope($scope)
97
            ->setSubkeytext($subKeyText)
98
            ->setAccessUrl($accessUrl)
99
            ->setAccessUrlChangeable($accessUrlChangeable)
100
            ->setAccessUrlLocked($accessUrlLocked);
101
102
        $this->getEntityManager()->persist($setting);
103
104
        if (count($options) > 0) {
105
            foreach ($options as $option) {
106
                if (empty($option['text'])) {
107
                    if ($option['value'] == 'true') {
108
                        $option['text'] = 'Yes';
109
                    } else {
110
                        $option['text'] = 'No';
111
                    }
112
                }
113
114
                $settingOption = new SettingsOptions();
115
                $settingOption
116
                    ->setVariable($variable)
117
                    ->setValue($option['value'])
118
                    ->setDisplayText($option['text']);
119
120
                $this->getEntityManager()->persist($settingOption);
121
            }
122
        }
123
        $this->getEntityManager()->flush();
124
    }
125
126
    /**
127
     * @param string $variable
128
     * @return mixed
129
     */
130
    public function getConfigurationValue($variable)
131
    {
132
        global $_configuration;
133
        if (isset($_configuration[$variable])) {
134
            return $_configuration[$variable];
135
        }
136
        return false;
137
    }
138
}
139