Passed
Pull Request — 4.2 (#140)
by David
09:31
created

TdbmController   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 5
dl 0
loc 128
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
B defaultAction() 0 33 5
A generate() 0 9 1
A generateDaos() 0 19 3
A getConfigurationDescriptor() 0 4 1
A getFromConfiguration() 0 8 2
A setInConfiguration() 0 8 2
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 $daoFactoryInstanceName;
29
    protected $autoloadDetected;
30
    ///protected $storeInUtc;
31
    protected $useCustomComposer;
32
    protected $composerFile;
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
        $this->daoNamespace = self::getFromConfiguration($this->moufManager, $name, 'daoNamespace');
45
        $this->beanNamespace = self::getFromConfiguration($this->moufManager, $name, 'beanNamespace');
46
        $this->daoFactoryInstanceName = self::getFromConfiguration($this->moufManager, $name, 'daoFactoryInstanceName');
47
        //$this->storeInUtc = self::getFromConfiguration($this->moufManager, $name, 'storeInUtc');
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% 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...
48
        $this->composerFile = self::getFromConfiguration($this->moufManager, $name, 'customComposerFile');
49
        $this->useCustomComposer = $this->composerFile ? true : false;
50
51
        if ($this->daoNamespace == null && $this->beanNamespace == null) {
52
            $classNameMapper = ClassNameMapper::createFromComposerFile(__DIR__.'/../../../../../../../../composer.json');
53
54
            $autoloadNamespaces = $classNameMapper->getManagedNamespaces();
55
            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...
56
                $this->autoloadDetected = true;
57
                $rootNamespace = $autoloadNamespaces[0];
58
                $this->daoNamespace = $rootNamespace.'Dao';
59
                $this->beanNamespace = $rootNamespace.'Dao\\Bean';
60
            } else {
61
                $this->autoloadDetected = false;
62
                $this->daoNamespace = 'YourApplication\\Dao';
63
                $this->beanNamespace = 'YourApplication\\Dao\\Bean';
64
            }
65
        } else {
66
            $this->autoloadDetected = true;
67
        }
68
69
        $this->content->addFile(__DIR__.'/../../../../views/tdbmGenerate.php', $this);
70
        $this->template->toHtml();
71
    }
72
73
    /**
74
     * This action generates the DAOs and Beans for the TDBM service passed in parameter.
75
     *
76
     * @Action
77
     *
78
     * @param string $name
79
     * @param bool   $selfedit
80
     */
81
    public function generate($name, $daonamespace, $beannamespace, $daofactoryinstancename, /*$storeInUtc = 0,*/ $selfedit = 'false', $useCustomComposer = false, $composerFile = '')
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
82
    {
83
        $this->initController($name, $selfedit);
84
85
        self::generateDaos($this->moufManager, $name, $daonamespace, $beannamespace, $daofactoryinstancename, $selfedit, /*$storeInUtc,*/ $useCustomComposer, $composerFile);
86
87
        // TODO: better: we should redirect to a screen that list the number of DAOs generated, etc...
88
        header('Location: '.ROOT_URL.'ajaxinstance/?name='.urlencode($name).'&selfedit='.$selfedit);
89
    }
90
91
    /**
92
     * This function generates the DAOs and Beans for the TDBM service passed in parameter.
93
     *
94
     * @param MoufManager $moufManager
95
     * @param string      $name
96
     * @param string      $daonamespace
97
     * @param string      $beannamespace
98
     * @param string      $daofactoryclassname
0 ignored issues
show
Bug introduced by
There is no parameter named $daofactoryclassname. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
99
     * @param string      $daofactoryinstancename
100
     * @param string      $selfedit
101
     *
102
     * @throws \Mouf\MoufException
103
     */
104
    public static function generateDaos(MoufManager $moufManager, $name, $daonamespace, $beannamespace, $daofactoryinstancename, $selfedit = 'false', /*$storeInUtc = null,*/ $useCustomComposer = null, $composerFile = null)
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
105
    {
106
        self::setInConfiguration($moufManager, $name, 'daoNamespace', $daonamespace);
107
        self::setInConfiguration($moufManager, $name, 'beanNamespace', $beannamespace);
108
        self::setInConfiguration($moufManager, $name, 'daoFactoryInstanceName', $daofactoryinstancename);
109
        //self::setInConfiguration($moufManager, $name, 'storeInUtc', $storeInUtc);
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% 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...
110
        if ($useCustomComposer) {
111
            self::setInConfiguration($moufManager, $name, 'customComposerFile', $composerFile);
112
        } else {
113
            self::setInConfiguration($moufManager, $name, 'customComposerFile', null);
114
        }
115
        // Let's rewrite before calling the DAO generator
116
        $moufManager->rewriteMouf();
117
118
119
        $tdbmService = new InstanceProxy($name);
120
        /* @var $tdbmService TDBMService */
121
        $tdbmService->generateAllDaosAndBeans(($useCustomComposer ? $composerFile : null));
122
    }
123
124
    private static function getConfigurationDescriptor(MoufManager $moufManager, string $tdbmInstanceName)
125
    {
126
        return $moufManager->getInstanceDescriptor($tdbmInstanceName)->getConstructorArgumentProperty('configuration')->getValue();
127
    }
128
129
    private static function getFromConfiguration(MoufManager $moufManager, string $tdbmInstanceName, string $property)
130
    {
131
        $configuration = self::getConfigurationDescriptor($moufManager, $tdbmInstanceName);
132
        if ($configuration === null) {
133
            throw new \RuntimeException('Unable to find the configuration object linked to TDBMService.');
134
        }
135
        return $configuration->getProperty($property)->getValue();
136
    }
137
138
    private static function setInConfiguration(MoufManager $moufManager, string $tdbmInstanceName, string $property, ?string $value)
139
    {
140
        $configuration = self::getConfigurationDescriptor($moufManager, $tdbmInstanceName);
141
        if ($configuration === null) {
142
            throw new \RuntimeException('Unable to find the configuration object linked to TDBMService.');
143
        }
144
        $configuration->getProperty($property)->setValue($value);
145
    }
146
}
147