TdbmController   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 147
Duplicated Lines 11.56 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 7
dl 17
loc 147
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B defaultAction() 17 44 5
A generate() 0 9 1
B generateDaos() 0 49 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Mouf\Database\TDBM\Controllers;
4
5
use Mouf\Composer\ClassNameMapper;
6
use Mouf\Controllers\AbstractMoufInstanceController;
7
use Mouf\Database\TDBM\TDBMService;
8
use Mouf\Database\TDBM\Utils\TDBMDaoGenerator;
9
use Mouf\Html\HtmlElement\HtmlBlock;
10
use Mouf\MoufManager;
11
use Mouf\InstanceProxy;
12
13
/**
14
 * The controller to generate automatically the Beans, Daos, etc...
15
 * Sweet!
16
 *
17
 * @Component
18
 */
19
class TdbmController extends AbstractMoufInstanceController
20
{
21
    /**
22
     * @var HtmlBlock
23
     */
24
    public $content;
25
26
    protected $daoNamespace;
27
    protected $beanNamespace;
28
    protected $daoFactoryName;
29
    protected $daoFactoryInstanceName;
30
    protected $autoloadDetected;
31
    protected $storeInUtc;
32
    protected $useCustomComposer;
33
34
    /**
35
     * Admin page used to display the DAO generation form.
36
     *
37
     * @Action
38
     */
39
    public function defaultAction($name, $selfedit = 'false')
40
    {
41
        $this->initController($name, $selfedit);
42
43
        // Fill variables
44
        if ($this->moufManager->getVariable('tdbmDefaultSourceDirectory_'.$name) != null) {
45
            $this->daoNamespace = $this->moufManager->getVariable('tdbmDefaultDaoNamespace_'.$name);
46
            $this->beanNamespace = $this->moufManager->getVariable('tdbmDefaultBeanNamespace_'.$name);
47
            $this->daoFactoryName = $this->moufManager->getVariable('tdbmDefaultDaoFactoryName_'.$name);
48
            $this->daoFactoryInstanceName = $this->moufManager->getVariable('tdbmDefaultDaoFactoryInstanceName_'.$name);
49
            $this->storeInUtc = $this->moufManager->getVariable('tdbmDefaultStoreInUtc_'.$name);
50
            $this->useCustomComposer = $this->moufManager->getVariable('tdbmDefaultUseCustomComposer_'.$name);
51
            $this->composerFile = $this->moufManager->getVariable('tdbmDefaultComposerFile_'.$name);
52
        } else {
53
            $this->daoNamespace = $this->moufManager->getVariable('tdbmDefaultDaoNamespace');
54
            $this->beanNamespace = $this->moufManager->getVariable('tdbmDefaultBeanNamespace');
55
            $this->daoFactoryName = $this->moufManager->getVariable('tdbmDefaultDaoFactoryName');
56
            $this->daoFactoryInstanceName = $this->moufManager->getVariable('tdbmDefaultDaoFactoryInstanceName');
57
            $this->storeInUtc = $this->moufManager->getVariable('tdbmDefaultStoreInUtc');
58
            $this->useCustomComposer = $this->moufManager->getVariable('tdbmDefaultUseCustomComposer');
59
            $this->composerFile = $this->moufManager->getVariable('tdbmDefaultComposerFile');
60
        }
61
62 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...
63
            $classNameMapper = ClassNameMapper::createFromComposerFile(__DIR__.'/../../../../../../../../composer.json');
64
65
            $autoloadNamespaces = $classNameMapper->getManagedNamespaces();
66
            if ($autoloadNamespaces) {
67
                $this->autoloadDetected = true;
68
                $rootNamespace = $autoloadNamespaces[0];
69
                $this->daoNamespace = $rootNamespace.'Dao';
70
                $this->beanNamespace = $rootNamespace.'Dao\\Bean';
71
            } else {
72
                $this->autoloadDetected = false;
73
                $this->daoNamespace = 'YourApplication\\Dao';
74
                $this->beanNamespace = 'YourApplication\\Dao\\Bean';
75
            }
76
        } else {
77
            $this->autoloadDetected = true;
78
        }
79
80
        $this->content->addFile(__DIR__.'/../../../../views/tdbmGenerate.php', $this);
81
        $this->template->toHtml();
82
    }
83
84
    /**
85
     * This action generates the DAOs and Beans for the TDBM service passed in parameter.
86
     *
87
     * @Action
88
     *
89
     * @param string $name
90
     * @param bool   $selfedit
91
     */
92
    public function generate($name, $daonamespace, $beannamespace, $daofactoryclassname, $daofactoryinstancename, $storeInUtc = 0, $selfedit = 'false', $useCustomComposer = false, $composerFile = '')
93
    {
94
        $this->initController($name, $selfedit);
95
96
        self::generateDaos($this->moufManager, $name, $daonamespace, $beannamespace, $daofactoryclassname, $daofactoryinstancename, $selfedit, $storeInUtc, $useCustomComposer, $composerFile);
97
98
        // TODO: better: we should redirect to a screen that list the number of DAOs generated, etc...
99
        header('Location: '.ROOT_URL.'ajaxinstance/?name='.urlencode($name).'&selfedit='.$selfedit);
100
    }
101
102
    /**
103
     * This function generates the DAOs and Beans for the TDBM service passed in parameter.
104
     *
105
     * @param MoufManager $moufManager
106
     * @param string      $name
107
     * @param string      $daonamespace
108
     * @param string      $beannamespace
109
     * @param string      $daofactoryclassname
110
     * @param string      $daofactoryinstancename
111
     * @param string      $selfedit
112
     * @param bool        $storeInUtc
113
     *
114
     * @throws \Mouf\MoufException
115
     */
116
    public static function generateDaos(MoufManager $moufManager, $name, $daonamespace, $beannamespace, $daofactoryclassname, $daofactoryinstancename, $selfedit = 'false', $storeInUtc = null, $useCustomComposer = null, $composerFile = null)
117
    {
118
        $moufManager->setVariable('tdbmDefaultDaoNamespace_'.$name, $daonamespace);
119
        $moufManager->setVariable('tdbmDefaultBeanNamespace_'.$name, $beannamespace);
120
        $moufManager->setVariable('tdbmDefaultDaoFactoryName_'.$name, $daofactoryclassname);
121
        $moufManager->setVariable('tdbmDefaultDaoFactoryInstanceName_'.$name, $daofactoryinstancename);
122
        $moufManager->setVariable('tdbmDefaultStoreInUtc_'.$name, $storeInUtc);
123
        $moufManager->setVariable('tdbmDefaultUseCustomComposer_'.$name, $useCustomComposer);
124
        $moufManager->setVariable('tdbmDefaultComposerFile_'.$name, $composerFile);
125
126
        // In case of instance renaming, let's use the last used settings
127
        $moufManager->setVariable('tdbmDefaultDaoNamespace', $daonamespace);
128
        $moufManager->setVariable('tdbmDefaultBeanNamespace', $beannamespace);
129
        $moufManager->setVariable('tdbmDefaultDaoFactoryName', $daofactoryclassname);
130
        $moufManager->setVariable('tdbmDefaultDaoFactoryInstanceName', $daofactoryinstancename);
131
        $moufManager->setVariable('tdbmDefaultStoreInUtc', $storeInUtc);
132
        $moufManager->setVariable('tdbmDefaultUseCustomComposer', $useCustomComposer);
133
        $moufManager->setVariable('tdbmDefaultComposerFile', $composerFile);
134
135
        // Remove first and last slash in namespace.
136
        $daonamespace = trim($daonamespace, '\\');
137
        $beannamespace = trim($beannamespace, '\\');
138
139
        $tdbmService = new InstanceProxy($name);
140
        /* @var $tdbmService TDBMService */
141
        $tables = $tdbmService->generateAllDaosAndBeans($daofactoryclassname, $daonamespace, $beannamespace, $storeInUtc, ($useCustomComposer ? $composerFile : null));
0 ignored issues
show
Bug introduced by
It seems like $storeInUtc defined by parameter $storeInUtc on line 116 can also be of type null; however, Mouf\Database\TDBM\TDBMS...nerateAllDaosAndBeans() does only seem to accept boolean, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
142
143
        $moufManager->declareComponent($daofactoryinstancename, $daonamespace.'\\Generated\\'.$daofactoryclassname, false, MoufManager::DECLARE_ON_EXIST_KEEP_INCOMING_LINKS);
144
145
        $tableToBeanMap = [];
146
147
        //$tdbmServiceDescriptor = $moufManager->getInstanceDescriptor('tdbmService');
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
148
149
        foreach ($tables as $table) {
150
            $daoName = TDBMDaoGenerator::getDaoNameFromTableName($table);
151
152
            $instanceName = TDBMDaoGenerator::toVariableName($daoName);
153
            if (!$moufManager->instanceExists($instanceName)) {
154
                $moufManager->declareComponent($instanceName, $daonamespace.'\\'.$daoName);
155
            }
156
            $moufManager->setParameterViaConstructor($instanceName, 0, $name, 'object');
157
            $moufManager->bindComponentViaSetter($daofactoryinstancename, 'set'.$daoName, $instanceName);
158
159
            $tableToBeanMap[$table] = $beannamespace.'\\'.TDBMDaoGenerator::getBeanNameFromTableName($table);
160
        }
161
        $tdbmServiceDescriptor = $moufManager->getInstanceDescriptor($name);
162
        $tdbmServiceDescriptor->getSetterProperty('setTableToBeanMap')->setValue($tableToBeanMap);
163
        $moufManager->rewriteMouf();
164
    }
165
}
166