Completed
Pull Request — 4.0 (#48)
by Marc
05:03
created

TdbmInstallController::configure()   C

Complexity

Conditions 7
Paths 10

Size

Total Lines 51
Code Lines 33

Duplication

Lines 17
Ratio 33.33 %

Importance

Changes 10
Bugs 1 Features 2
Metric Value
c 10
b 1
f 2
dl 17
loc 51
rs 6.9743
cc 7
eloc 33
nc 10
nop 1

How to fix   Long Method   

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
namespace Mouf\Database\TDBM\Controllers;
4
5
use Mouf\Composer\ClassNameMapper;
6
use Mouf\Actions\InstallUtils;
7
use Mouf\MoufManager;
8
use Mouf\Html\HtmlElement\HtmlBlock;
9
use Mouf\Mvc\Splash\Controllers\Controller;
10
11
/**
12
 * The controller used in the TDBM install process.
13
 *
14
 * @Component
15
 */
16
class TdbmInstallController extends Controller
17
{
18
    /**
19
     * @var HtmlBlock
20
     */
21
    public $content;
22
23
    public $selfedit;
24
25
    /**
26
     * The active MoufManager to be edited/viewed.
27
     *
28
     * @var MoufManager
29
     */
30
    public $moufManager;
31
32
    /**
33
     * The template used by the main page for mouf.
34
     *
35
     * @Property
36
     * @Compulsory
37
     *
38
     * @var TemplateInterface
39
     */
40
    public $template;
41
42
    /**
43
     * Displays the first install screen.
44
     *
45
     * @Action
46
     * @Logged
47
     *
48
     * @param string $selfedit If true, the name of the component must be a component from the Mouf framework itself (internal use only)
49
     */
50
    public function defaultAction($selfedit = 'false')
51
    {
52
        $this->selfedit = $selfedit;
53
54
        if ($selfedit == 'true') {
55
            $this->moufManager = MoufManager::getMoufManager();
56
        } else {
57
            $this->moufManager = MoufManager::getMoufManagerHiddenInstance();
58
        }
59
60
        $this->content->addFile(dirname(__FILE__).'/../../../../views/installStep1.php', $this);
61
        $this->template->toHtml();
62
    }
63
64
    /**
65
     * Skips the install process.
66
     *
67
     * @Action
68
     * @Logged
69
     *
70
     * @param string $selfedit If true, the name of the component must be a component from the Mouf framework itself (internal use only)
71
     */
72
    public function skip($selfedit = 'false')
73
    {
74
        InstallUtils::continueInstall($selfedit == 'true');
75
    }
76
77
    protected $daoNamespace;
78
    protected $beanNamespace;
79
    protected $autoloadDetected;
80
    protected $storeInUtc;
81
82
    /**
83
     * Displays the second install screen.
84
     *
85
     * @Action
86
     * @Logged
87
     *
88
     * @param string $selfedit If true, the name of the component must be a component from the Mouf framework itself (internal use only)
89
     */
90
    public function configure($selfedit = 'false')
91
    {
92
        $this->selfedit = $selfedit;
93
94
        if ($selfedit == 'true') {
95
            $this->moufManager = MoufManager::getMoufManager();
96
        } else {
97
            $this->moufManager = MoufManager::getMoufManagerHiddenInstance();
98
        }
99
100
        // Let's start by performing basic checks about the instances we assume to exist.
101
        if (!$this->moufManager->instanceExists('dbalConnection')) {
102
            $this->displayErrorMsg("The TDBM install process assumes your database connection instance is already created, and that the name of this instance is 'dbalConnection'. Could not find the 'dbalConnection' instance.");
103
104
            return;
105
        }
106
107
        if (!$this->moufManager->instanceExists('noCacheService')) {
108
            $this->displayErrorMsg("The TDBM install process assumes that a cache instance named 'noCacheService' exists. Could not find the 'noCacheService' instance.");
109
110
            return;
111
        }
112
113
        $this->daoNamespace = $this->moufManager->getVariable('tdbmDefaultDaoNamespace_tdbmService');
114
        $this->beanNamespace = $this->moufManager->getVariable('tdbmDefaultBeanNamespace_tdbmService');
115
116 View Code Duplication
        if ($this->daoNamespace == null && $this->beanNamespace == null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
117
            $classNameMapper = ClassNameMapper::createFromComposerFile(__DIR__.'/../../../../../../../../composer.json');
118
119
            $autoloadNamespaces = $classNameMapper->getManagedNamespaces();
120
            if ($autoloadNamespaces) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $autoloadNamespaces of type array<integer|string> is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
121
                $this->autoloadDetected = true;
122
                $rootNamespace = $autoloadNamespaces[0];
123
                $this->daoNamespace = $rootNamespace.'Dao';
124
                $this->beanNamespace = $rootNamespace.'Dao\\Bean';
125
            } else {
126
                $this->autoloadDetected = false;
127
                $this->daoNamespace = 'YourApplication\\Dao';
128
                $this->beanNamespace = 'YourApplication\\Dao\\Bean';
129
            }
130
        } else {
131
            $this->autoloadDetected = true;
132
        }
133
        $this->defaultPath = true;
134
        $this->storePath = '';
135
136
        $this->castDatesToDateTime = true;
137
138
        $this->content->addFile(dirname(__FILE__).'/../../../../views/installStep2.php', $this);
139
        $this->template->toHtml();
140
    }
141
142
    /**
143
     * This action generates the TDBM instance, then the DAOs and Beans.
144
     *
145
     * @Action
146
     *
147
     * @param string $daonamespace
148
     * @param string $beannamespace
149
     * @param int    $storeInUtc
150
     * @param string $selfedit
151
     *
152
     * @throws \Mouf\MoufException
153
     */
154
    public function generate($daonamespace, $beannamespace, $storeInUtc = 0, $selfedit = 'false', $defaultPath = false, $storePath = '')
155
    {
156
        $this->selfedit = $selfedit;
157
158
        if ($selfedit == 'true') {
159
            $this->moufManager = MoufManager::getMoufManager();
160
        } else {
161
            $this->moufManager = MoufManager::getMoufManagerHiddenInstance();
162
        }
163
164
        if (!$this->moufManager->instanceExists('doctrineApcCache')) {
165
            $doctrineApcCache = $this->moufManager->createInstance('Doctrine\\Common\\Cache\\ApcCache')->setName('doctrineApcCache');
166
            // TODO: set namespace
167
        } else {
168
            $doctrineApcCache = $this->moufManager->getInstanceDescriptor('doctrineApcCache');
169
        }
170
171
        if (!$this->moufManager->instanceExists('tdbmService')) {
172
            $tdbmService = $this->moufManager->createInstance('Mouf\\Database\\TDBM\\TDBMService')->setName('tdbmService');
173
            $tdbmService->getConstructorArgumentProperty('connection')->setValue($this->moufManager->getInstanceDescriptor('dbalConnection'));
174
            $tdbmService->getConstructorArgumentProperty('cache')->setValue($doctrineApcCache);
175
        }
176
177
        $this->moufManager->rewriteMouf();
178
179
        TdbmController::generateDaos($this->moufManager, 'tdbmService', $daonamespace, $beannamespace, 'DaoFactory', 'daoFactory', $selfedit, $storeInUtc, $defaultPath, $storePath);
180
181
        InstallUtils::continueInstall($selfedit == 'true');
182
    }
183
184
    protected $errorMsg;
185
186
    private function displayErrorMsg($msg)
187
    {
188
        $this->errorMsg = $msg;
189
        $this->content->addFile(dirname(__FILE__).'/../../../../views/installError.php', $this);
190
        $this->template->toHtml();
191
    }
192
}
193