Failed Conditions
Pull Request — experimental/sf (#3240)
by Kentaro
71:31 queued 20:38
created

Kernel::build()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 54

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 54
ccs 27
cts 27
cp 1
crap 2
rs 9.0036
c 1
b 0
f 0

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
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) LOCKON CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.lockon.co.jp/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Eccube;
15
16
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass;
17
use Eccube\Common\EccubeNav;
18
use Eccube\Common\EccubeTwigBlock;
19
use Eccube\DependencyInjection\Compiler\AutoConfigurationTagPass;
20
use Eccube\DependencyInjection\Compiler\NavCompilerPass;
21
use Eccube\DependencyInjection\Compiler\PaymentMethodPass;
22
use Eccube\DependencyInjection\Compiler\PaymentServicePass;
23
use Eccube\DependencyInjection\Compiler\PluginPass;
24
use Eccube\DependencyInjection\Compiler\QueryCustomizerPass;
25
use Eccube\DependencyInjection\Compiler\TemplateListenerPass;
26
use Eccube\DependencyInjection\Compiler\TwigBlockPass;
27
use Eccube\DependencyInjection\Compiler\TwigExtensionPass;
28
use Eccube\DependencyInjection\Compiler\WebServerDocumentRootPass;
29
use Eccube\DependencyInjection\EccubeExtension;
30
use Eccube\Doctrine\DBAL\Types\UTCDateTimeType;
31
use Eccube\Doctrine\DBAL\Types\UTCDateTimeTzType;
32
use Eccube\Doctrine\ORM\Mapping\Driver\AnnotationDriver;
33
use Eccube\Doctrine\Query\QueryCustomizer;
34
use Eccube\Plugin\ConfigManager;
35
use Eccube\Service\PaymentServiceInterface;
36
use Eccube\Service\Payment\PaymentMethodInterface;
37
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
38
use Symfony\Component\Config\Loader\LoaderInterface;
39
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
40
use Symfony\Component\DependencyInjection\ContainerBuilder;
41
use Symfony\Component\DependencyInjection\Definition;
42
use Symfony\Component\DependencyInjection\Reference;
43
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
44
use Symfony\Component\Routing\RouteCollectionBuilder;
45
46
class Kernel extends BaseKernel
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
47
{
48
    use MicroKernelTrait;
49
50
    const CONFIG_EXTS = '.{php,xml,yaml,yml}';
51
52 1194
    public function getCacheDir()
53
    {
54 1194
        return $this->getProjectDir().'/var/cache/'.$this->environment;
55
    }
56
57 13
    public function getLogDir()
58
    {
59 13
        return $this->getProjectDir().'/var/log';
60
    }
61
62 1194
    public function registerBundles()
63
    {
64 1194
        $contents = require $this->getProjectDir().'/app/config/eccube/bundles.php';
65 1194
        foreach ($contents as $class => $envs) {
66 1194
            if (isset($envs['all']) || isset($envs[$this->environment])) {
67 1194
                yield new $class();
68
            }
69
        }
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     *
75
     * @see \Symfony\Component\HttpKernel\Kernel::boot()
76
     */
77 1194
    public function boot()
78
    {
79
        // Symfonyがsrc/Eccube/Entity以下を読み込む前にapp/proxy/entity以下をロードする
80 1194
        $this->loadEntityProxies();
81
82 1194
        parent::boot();
83
84
        // DateTime/DateTimeTzのタイムゾーンを設定.
85 1194
        UTCDateTimeType::setTimeZone($this->container->getParameter('timezone'));
0 ignored issues
show
Bug introduced by
The method getParameter cannot be called on $this->container (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
86 1194
        UTCDateTimeTzType::setTimeZone($this->container->getParameter('timezone'));
0 ignored issues
show
Bug introduced by
The method getParameter cannot be called on $this->container (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
87
88
        // Activate to $app
89 1194
        $app = Application::getInstance(['debug' => $this->isDebug()]);
90 1194
        $app->setParentContainer($this->container);
0 ignored issues
show
Documentation introduced by
$this->container is of type null, but the function expects a object<Psr\Container\ContainerInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
91 1194
        $app->initialize();
92 1194
        $app->boot();
93
94 1194
        $this->container->set('app', $app);
0 ignored issues
show
Bug introduced by
The method set cannot be called on $this->container (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
95
    }
96
97 1
    protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader)
0 ignored issues
show
Unused Code introduced by
The parameter $container is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
98
    {
99 1
        $confDir = $this->getProjectDir().'/app/config/eccube';
100 1
        $loader->load($confDir.'/packages/*'.self::CONFIG_EXTS, 'glob');
101 1
        if (is_dir($confDir.'/packages/'.$this->environment)) {
102 1
            $loader->load($confDir.'/packages/'.$this->environment.'/**/*'.self::CONFIG_EXTS, 'glob');
103
        }
104 1
        $loader->load($confDir.'/services'.self::CONFIG_EXTS, 'glob');
105 1
        $loader->load($confDir.'/services_'.$this->environment.self::CONFIG_EXTS, 'glob');
106
107
        // プラグインのservices.phpをロードする.
108 1
        $dir = dirname(__DIR__).'/../app/Plugin/*/Resource/config';
109 1
        $loader->load($dir.'/services'.self::CONFIG_EXTS, 'glob');
110
    }
111
112 57
    protected function configureRoutes(RouteCollectionBuilder $routes)
113
    {
114 57
        $container = $this->getContainer();
115
116 57
        $forceSSL = $container->getParameter('eccube_force_ssl');
117 57
        $scheme = $forceSSL ? 'https' : 'http';
118 57
        $routes->setSchemes($scheme);
119
120 57
        $confDir = $this->getProjectDir().'/app/config/eccube';
121 57
        if (is_dir($confDir.'/routes/')) {
122 57
            $builder = $routes->import($confDir.'/routes/*'.self::CONFIG_EXTS, '/', 'glob');
123 57
            $builder->setSchemes($scheme);
124
        }
125 57
        if (is_dir($confDir.'/routes/'.$this->environment)) {
126
            $builder = $routes->import($confDir.'/routes/'.$this->environment.'/**/*'.self::CONFIG_EXTS, '/', 'glob');
127
            $builder->setSchemes($scheme);
128
        }
129 57
        $builder = $routes->import($confDir.'/routes'.self::CONFIG_EXTS, '/', 'glob');
130 57
        $builder->setSchemes($scheme);
131 57
        $builder = $routes->import($confDir.'/routes_'.$this->environment.self::CONFIG_EXTS, '/', 'glob');
132 57
        $builder->setSchemes($scheme);
133
134
        // 有効なプラグインのルーティングをインポートする.
135 57
        $plugins = $container->getParameter('eccube.plugins.enabled');
136 57
        $pluginDir = $this->getProjectDir().'/app/Plugin';
137 57
        foreach ($plugins as $plugin) {
138
            $dir = $pluginDir.'/'.$plugin['code'].'/Controller';
139
            if (file_exists($dir)) {
140
                $builder = $routes->import($dir, '/', 'annotation');
141
                $builder->setSchemes($scheme);
142
            }
143
        }
144
    }
145
146 1
    protected function build(ContainerBuilder $container)
147
    {
148 1
        $this->addEntityExtensionPass($container);
149
150 1
        $container->registerExtension(new EccubeExtension());
151
152
        // サービスタグの自動設定を行う
153 1
        $container->addCompilerPass(new AutoConfigurationTagPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 11);
154
155
        // サービスタグの収集より先に実行し, 付与されているタグをクリアする.
156
        // FormPassは優先度0で実行されているので, それより速いタイミングで実行させる.
157
        // 自動登録されるタグやコンパイラパスの登録タイミングは, FrameworkExtension::load(), FrameworkBundle::build()を参考に.
158 1
        $container->addCompilerPass(new PluginPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 10);
159
160
        // DocumentRootをルーティディレクトリに設定する.
161 1
        $container->addCompilerPass(new WebServerDocumentRootPass('%kernel.project_dir%/'));
162
163 1
        if ($this->environment !== 'install') {
164
            // テンプレートフックポイントを動作させるように.
165 1
            $container->addCompilerPass(new TemplateListenerPass());
166
        }
167
168
        // twigのurl,path関数を差し替え
169 1
        $container->addCompilerPass(new TwigExtensionPass());
170
171 1
        $container->register('app', Application::class)
172 1
            ->setSynthetic(true)
173 1
            ->setPublic(true);
174
175
        // クエリカスタマイズの拡張.
176 1
        $container->registerForAutoconfiguration(QueryCustomizer::class)
177 1
            ->addTag(QueryCustomizerPass::QUERY_CUSTOMIZER_TAG);
178 1
        $container->addCompilerPass(new QueryCustomizerPass());
179
180
        // 管理画面ナビの拡張
181 1
        $container->registerForAutoconfiguration(EccubeNav::class)
182 1
            ->addTag(NavCompilerPass::NAV_TAG);
183 1
        $container->addCompilerPass(new NavCompilerPass());
184
185
        // TwigBlockの拡張
186 1
        $container->registerForAutoconfiguration(EccubeTwigBlock::class)
187 1
            ->addTag(TwigBlockPass::TWIG_BLOCK_TAG);
188 1
        $container->addCompilerPass(new TwigBlockPass());
189
190
        // PaymentService の拡張
191 1
        $container->registerForAutoconfiguration(PaymentServiceInterface::class)
192 1
            ->addTag(PaymentServicePass::PAYMENT_TAG);
193 1
        $container->addCompilerPass(new PaymentServicePass());
194
195
        // PaymentMethod の拡張
196 1
        $container->registerForAutoconfiguration(PaymentMethodInterface::class)
197 1
            ->addTag(PaymentMethodPass::PAYMENT_METHOD_TAG);
198 1
        $container->addCompilerPass(new PaymentMethodPass());
199
    }
200
201 1
    protected function addEntityExtensionPass(ContainerBuilder $container)
202
    {
203 1
        $projectDir = $container->getParameter('kernel.project_dir');
204
205
        // Eccube
206 1
        $paths = ['%kernel.project_dir%/src/Eccube/Entity'];
207 1
        $namespaces = ['Eccube\\Entity'];
208 1
        $reader = new Reference('annotation_reader');
209 1
        $driver = new Definition(AnnotationDriver::class, [$reader, $paths]);
210 1
        $driver->addMethodCall('setTraitProxiesDirectory', [$projectDir.'/app/proxy/entity']);
211 1
        $container->addCompilerPass(new DoctrineOrmMappingsPass($driver, $namespaces, []));
212
213
        // Acme
214 1
        $container->addCompilerPass(DoctrineOrmMappingsPass::createAnnotationMappingDriver(
215 1
            ['Acme\\Entity'],
216 1
            ['%kernel.project_dir%/app/Acme/Entity']
217
        ));
218
219
        // Plugin
220 1
        $pluginConfigs = ConfigManager::getPluginConfigAll($this->isDebug());
221 1
        foreach ($pluginConfigs as $config) {
222 1
            $code = $config['config']['code'];
223 1
            if (file_exists($projectDir.'/app/Plugin/'.$code.'/Entity')) {
224 1
                $container->addCompilerPass(DoctrineOrmMappingsPass::createAnnotationMappingDriver(
225 1
                    ['Plugin\\'.$code.'\\Entity'],
226 1
                    ['%kernel.project_dir%/app/Plugin/'.$code.'/Entity']
227
                ));
228
            }
229
        }
230
    }
231
232 1194
    protected function loadEntityProxies()
233
    {
234 1194
        foreach (glob(__DIR__.'/../../app/proxy/entity/*.php') as $file) {
235
            require_once $file;
236
        }
237
    }
238
}
239