Passed
Pull Request — 4.3 (#142)
by Jean-Baptiste
04:20
created

TdbmController::getConfigurationDescriptor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 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\PathFinder\PathFinder;
9
use Mouf\Database\TDBM\Utils\TDBMDaoGenerator;
10
use Mouf\Html\HtmlElement\HtmlBlock;
11
use Mouf\MoufManager;
12
use Mouf\InstanceProxy;
13
14
/**
15
 * The controller to generate automatically the Beans, Daos, etc...
16
 * Sweet!
17
 *
18
 * @Component
19
 */
20
class TdbmController extends AbstractMoufInstanceController
21
{
22
    /**
23
     * @var HtmlBlock
24
     */
25
    public $content;
26
27
    protected $daoNamespace;
28
    protected $beanNamespace;
29
    protected $daoFactoryInstanceName;
30
    protected $autoloadDetected;
31
    ///protected $storeInUtc;
32
    protected $useCustomComposer;
33
    protected $composerFile;
34
35
    /**
36
     * Admin page used to display the DAO generation form.
37
     *
38
     * @Action
39
     */
40
    public function defaultAction($name, $selfedit = 'false')
41
    {
42
        $this->initController($name, $selfedit);
43
44
        // Fill variables
45
        $this->daoNamespace = self::getFromConfiguration($this->moufManager, $name, 'daoNamespace');
46
        $this->beanNamespace = self::getFromConfiguration($this->moufManager, $name, 'beanNamespace');
47
        $this->daoFactoryInstanceName = self::getFromConfiguration($this->moufManager, $name, 'daoFactoryInstanceName');
48
        //$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...
49
        $pathFinder = self::getFromConfiguration($this->moufManager, $name, 'pathFinder');
50
        if ($pathFinder !== null) {
51
            $this->composerFile = $pathFinder->getConstructorArgumentProperty('composerFile')->getValue();
52
        } else {
53
            $this->composerFile = null;
54
        }
55
        $this->useCustomComposer = $this->composerFile ? true : false;
56
57
        if ($this->daoNamespace == null && $this->beanNamespace == null) {
58
            $classNameMapper = ClassNameMapper::createFromComposerFile(__DIR__.'/../../../../../../../../composer.json');
59
60
            $autoloadNamespaces = $classNameMapper->getManagedNamespaces();
61
            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...
62
                $this->autoloadDetected = true;
63
                $rootNamespace = $autoloadNamespaces[0];
64
                $this->daoNamespace = $rootNamespace.'Dao';
65
                $this->beanNamespace = $rootNamespace.'Dao\\Bean';
66
            } else {
67
                $this->autoloadDetected = false;
68
                $this->daoNamespace = 'YourApplication\\Dao';
69
                $this->beanNamespace = 'YourApplication\\Dao\\Bean';
70
            }
71
        } else {
72
            $this->autoloadDetected = true;
73
        }
74
75
        $this->content->addFile(__DIR__.'/../../../../views/tdbmGenerate.php', $this);
76
        $this->template->toHtml();
77
    }
78
79
    /**
80
     * This action generates the DAOs and Beans for the TDBM service passed in parameter.
81
     *
82
     * @Action
83
     *
84
     * @param string $name
85
     * @param bool   $selfedit
86
     */
87
    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...
88
    {
89
        $this->initController($name, $selfedit);
90
91
        self::generateDaos($this->moufManager, $name, $daonamespace, $beannamespace, $daofactoryinstancename, $selfedit, /*$storeInUtc,*/ $useCustomComposer, $composerFile);
92
93
        // TODO: better: we should redirect to a screen that list the number of DAOs generated, etc...
94
        header('Location: '.ROOT_URL.'ajaxinstance/?name='.urlencode($name).'&selfedit='.$selfedit);
95
    }
96
97
    /**
98
     * This function generates the DAOs and Beans for the TDBM service passed in parameter.
99
     *
100
     * @param MoufManager $moufManager
101
     * @param string      $name
102
     * @param string      $daonamespace
103
     * @param string      $beannamespace
104
     * @param string      $selfedit
105
     *
106
     * @throws \Mouf\MoufException
107
     */
108
    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...
109
    {
110
        self::setInConfiguration($moufManager, $name, 'daoNamespace', $daonamespace);
111
        self::setInConfiguration($moufManager, $name, 'beanNamespace', $beannamespace);
112
        self::setInConfiguration($moufManager, $name, 'daoFactoryInstanceName', $daofactoryinstancename);
113
        //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...
114
        if ($useCustomComposer) {
115
            $pathFinder = $moufManager->createInstance(PathFinder::class);
116
            $pathFinder->getConstructorArgumentProperty('composerFile')->setValue($composerFile);
117
            self::setInConfiguration($moufManager, $name, 'pathFinder', $pathFinder);
118
        } else {
119
            self::setInConfiguration($moufManager, $name, 'pathFinder', null);
120
        }
121
        // Let's rewrite before calling the DAO generator
122
        $moufManager->rewriteMouf();
123
124
125
        $tdbmService = new InstanceProxy($name);
126
        /* @var $tdbmService TDBMService */
127
        $tdbmService->generateAllDaosAndBeans();
128
    }
129
130
    private static function getConfigurationDescriptor(MoufManager $moufManager, string $tdbmInstanceName)
131
    {
132
        return $moufManager->getInstanceDescriptor($tdbmInstanceName)->getConstructorArgumentProperty('configuration')->getValue();
133
    }
134
135
    private static function getFromConfiguration(MoufManager $moufManager, string $tdbmInstanceName, string $property)
136
    {
137
        $configuration = self::getConfigurationDescriptor($moufManager, $tdbmInstanceName);
138
        if ($configuration === null) {
139
            throw new \RuntimeException('Unable to find the configuration object linked to TDBMService.');
140
        }
141
        return $configuration->getProperty($property)->getValue();
142
    }
143
144
    private static function setInConfiguration(MoufManager $moufManager, string $tdbmInstanceName, string $property, ?string $value)
145
    {
146
        $configuration = self::getConfigurationDescriptor($moufManager, $tdbmInstanceName);
147
        if ($configuration === null) {
148
            throw new \RuntimeException('Unable to find the configuration object linked to TDBMService.');
149
        }
150
        $configuration->getProperty($property)->setValue($value);
151
    }
152
}
153