Completed
Pull Request — experimental/3.1 (#2484)
by Kentaro
57:08 queued 35:11
created

EccubeServiceProvider   C

Complexity

Total Complexity 3

Size/Duplication

Total Lines 229
Duplicated Lines 7.42 %

Coupling/Cohesion

Components 0
Dependencies 35

Test Coverage

Coverage 60.34%

Importance

Changes 14
Bugs 0 Features 0
Metric Value
dl 17
loc 229
ccs 70
cts 116
cp 0.6034
rs 5
c 14
b 0
f 0
wmc 3
lcom 0
cbo 35

2 Methods

Rating   Name   Duplication   Size   Complexity  
B register() 17 210 2
A subscribe() 0 7 1

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
 * This file is part of EC-CUBE
4
 *
5
 * Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved.
6
 *
7
 * http://www.lockon.co.jp/
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
 */
23
24
25
namespace Eccube\ServiceProvider;
26
27
use Doctrine\Common\Collections\ArrayCollection;
28
use Eccube\Entity\ItemHolderInterface;
29
use Eccube\Entity\BaseInfo;
30
use Eccube\EventListener\TransactionListener;
31
use Eccube\Repository\BaseInfoRepository;
32
use Eccube\Repository\DeliveryRepository;
33
use Eccube\Service\PurchaseFlow\Processor\AdminOrderRegisterPurchaseProcessor;
34
use Eccube\Service\PurchaseFlow\Processor\DeletedProductValidator;
35
use Eccube\Service\PurchaseFlow\Processor\DeliveryFeeFreeProcessor;
36
use Eccube\Service\PurchaseFlow\Processor\DeliveryFeeProcessor;
37
use Eccube\Service\PurchaseFlow\Processor\DeliverySettingValidator;
38
use Eccube\Service\PurchaseFlow\Processor\DisplayStatusValidator;
39
use Eccube\Service\PurchaseFlow\Processor\PaymentProcessor;
40
use Eccube\Service\PurchaseFlow\Processor\PaymentTotalLimitValidator;
41
use Eccube\Service\PurchaseFlow\Processor\PaymentTotalNegativeValidator;
42
use Eccube\Service\PurchaseFlow\Processor\SaleLimitValidator;
43
use Eccube\Service\PurchaseFlow\Processor\StockValidator;
44
use Eccube\Service\PurchaseFlow\Processor\UpdateDatePurchaseProcessor;
45
use Eccube\Service\PurchaseFlow\PurchaseContext;
46
use Eccube\Service\PurchaseFlow\PurchaseFlow;
47
use Eccube\Service\TaxRuleService;
48
use Pimple\Container;
49
use Pimple\ServiceProviderInterface;
50
use Silex\Api\EventListenerProviderInterface;
51
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
52
use Symfony\Component\HttpFoundation\ParameterBag;
53
54
class EccubeServiceProvider implements ServiceProviderInterface, EventListenerProviderInterface
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
55
{
56
    /**
57
     * Registers services on the given app.
58
     *
59
     * This method should only be used to configure services and parameters.
60
     * It should not get services.
61
     *
62
     * @param BaseApplication $app An Application instance
63
     */
64 1104
    public function register(Container $app)
65
    {
66
        $app[BaseInfo::class] = function () use ($app) {
67 1104
            return $app[BaseInfoRepository::class]->get();
68
        };
69
70
        $app['eccube.calculate.context'] = function () use ($app) {
71
                return new \Eccube\Service\Calculator\CalculateContext();
72
        };
73
74
        $app['eccube.calculate.strategies'] = function () use ($app) {
75
            $Collection = new \Eccube\Service\Calculator\CalculateStrategyCollection();
76
            $Collection->setApplication($app);
0 ignored issues
show
Compatibility introduced by
$app of type object<Pimple\Container> is not a sub-type of object<Eccube\Application>. It seems like you assume a child class of the class Pimple\Container to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
77
            //$Collection->setOrder($Order);
78
            // デフォルトのストラテジーをセットしておく
79
            $Collection->add($app['eccube.calculate.strategy.shipping']);
80
            $Collection->add($app['eccube.calculate.strategy.charge']);
81
            $Collection->add($app['eccube.calculate.strategy.tax']);
82
            $Collection->add($app['eccube.calculate.strategy.calculate_delivery_fee']);
83
            $Collection->add($app['eccube.calculate.strategy.calculate_charge']);
84
            $Collection->add($app['eccube.calculate.strategy.calculate_total']);
85
            return $Collection;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
86
        };
87
        $app['eccube.calculate.strategy.shipping'] = function () use ($app) {
88
                $Strategy = new \Eccube\Service\Calculator\Strategy\ShippingStrategy();
89
                $Strategy->setApplication($app);
0 ignored issues
show
Compatibility introduced by
$app of type object<Pimple\Container> is not a sub-type of object<Eccube\Application>. It seems like you assume a child class of the class Pimple\Container to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
90
                return $Strategy;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
91
        };
92
        $app['eccube.calculate.strategy.charge'] = function () use ($app) {
93
                $Strategy = new \Eccube\Service\Calculator\Strategy\ChargeStrategy();
94
                $Strategy->setApplication($app);
0 ignored issues
show
Compatibility introduced by
$app of type object<Pimple\Container> is not a sub-type of object<Eccube\Application>. It seems like you assume a child class of the class Pimple\Container to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
95
                return $Strategy;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
96
        };
97
98
        $app['eccube.calculate.strategy.tax'] = function () use ($app) {
99
                $Strategy = new \Eccube\Service\Calculator\Strategy\TaxStrategy();
100
                $Strategy->setApplication($app);
0 ignored issues
show
Compatibility introduced by
$app of type object<Pimple\Container> is not a sub-type of object<Eccube\Application>. It seems like you assume a child class of the class Pimple\Container to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
101
                return $Strategy;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
102
        };
103
104
        $app['eccube.calculate.strategy.calculate_delivery_fee'] = function () use ($app) {
105
            $Strategy = new \Eccube\Service\Calculator\Strategy\CalculateDeliveryFeeStrategy();
106
            $Strategy->setApplication($app);
0 ignored issues
show
Compatibility introduced by
$app of type object<Pimple\Container> is not a sub-type of object<Eccube\Application>. It seems like you assume a child class of the class Pimple\Container to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
107
            return $Strategy;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
108
        };
109
        $app['eccube.calculate.strategy.calculate_charge'] = function () use ($app) {
110
            $Strategy = new \Eccube\Service\Calculator\Strategy\CalculateChargeStrategy();
111
            $Strategy->setApplication($app);
0 ignored issues
show
Compatibility introduced by
$app of type object<Pimple\Container> is not a sub-type of object<Eccube\Application>. It seems like you assume a child class of the class Pimple\Container to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
112
            return $Strategy;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
113
        };
114
        $app['eccube.calculate.strategy.calculate_total'] = function () use ($app) {
115
            $Strategy = new \Eccube\Service\Calculator\Strategy\CalculateTotalStrategy();
116
            $Strategy->setApplication($app);
0 ignored issues
show
Compatibility introduced by
$app of type object<Pimple\Container> is not a sub-type of object<Eccube\Application>. It seems like you assume a child class of the class Pimple\Container to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
117
            return $Strategy;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
118
        };
119
120
        $app['payment.method'] = $app->protect(function ($clazz, $form) use ($app) {
121 1
                $PaymentMethod = new $clazz;
0 ignored issues
show
introduced by
Use parentheses when instantiating classes
Loading history...
122 1
                $PaymentMethod->setApplication($app);
123 1
                $PaymentMethod->setFormType($form);
124 1
                return $PaymentMethod;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
125 1104
        });
126
127
        $app['payment.method.request'] = $app->protect(function ($clazz, $form, $request) use ($app) {
128 2
                $PaymentMethod = new $clazz;
0 ignored issues
show
introduced by
Use parentheses when instantiating classes
Loading history...
129 2
                $PaymentMethod->setApplication($app);
130 2
                $PaymentMethod->setFormType($form);
131 2
                $PaymentMethod->setRequest($request);
132 2
                return $PaymentMethod;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
133 1104
        });
134
135
        $app['eccube.service.calculate'] = $app->protect(function ($Order, $Customer) use ($app) {
136
            $Service = new \Eccube\Service\CalculateService($Order, $Customer);
137
            $Context = $app['eccube.calculate.context'];
138
            $app['eccube.calculate.strategies']->setOrder($Order);
139
            $Context->setCalculateStrategies($app['eccube.calculate.strategies']);
140
            $Context->setOrder($Order);
141
            $Service->setContext($Context);
142
143
            return $Service;
144 1104
        });
145
146
        $app['eccube.service.payment'] = $app->protect(function ($clazz) use ($app) {
147 3
            $Service = new $clazz($app['request_stack']);
148
149 3
            return $Service;
150 1104
        });
151
152
        $app['paginator'] = $app->protect(function () {
153 29
            $paginator = new \Knp\Component\Pager\Paginator();
154 29
            $paginator->subscribe(new \Eccube\EventListener\PaginatorListener());
155
156 29
            return $paginator;
157 1104
        });
158
159
        $app['request_scope'] = function () {
160 21
            return new ParameterBag();
161
        };
162
        // TODO 使用するか検討
163
        $app['eccube.twig.node.hello'] = $app->protect(function ($node, $compiler) {
164
            $compiler
165
            ->addDebugInfo($node)
166
            ->write("echo 'Helloooooo ' . ")
167
            ->subcompile($node->getNode('expr'))
168
            ->raw(" . '!';\n")
169
            ;
170
171 1104
        });
172
        // TODO 使用するか検討
173
        $app['eccube.twig.node.jiro'] = $app->protect(function ($node, $compiler) {
174
            $compiler
175
            ->addDebugInfo($node)
176
            ->write("echo 'jirooooooo ' . ")
177
            ->subcompile($node->getNode('expr'))
178
            ->raw(" . '!';\n")
179
            ;
180
181 1104
        });
182
183
        // TODO 使用するか検討
184
        $app['eccube.twig.generic_node_names'] = function () use ($app) {
185
            return [
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
186 100
                'hello',
187
                'jiro',
188
                'bbb'
189
            ];
190
        };
191
192
        // TODO 使用するか検討
193
        $app['twig_parsers'] = function () use ($app) {
194 100
            $GenericTokenParsers = [];
195 100
            foreach ($app['eccube.twig.generic_node_names'] as $tagName) {
196 100
                $GenericTokenParsers[] = new \Eccube\Twig\Extension\GenericTokenParser($app, $tagName);
197
            }
198 100
            return $GenericTokenParsers;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
199
        };
200
201
        $app['eccube.twig.block.templates'] = function () {
202
            $templates = new ArrayCollection();
203
            $templates[] = 'render_block.twig';
204
205
            return $templates;
206
        };
207
208 1104
        $app['eccube.entity.event.dispatcher']->addEventListener(new \Acme\Entity\SoldOutEventListener());
209
        $app['eccube.queries'] = function () {
210 1104
            return new \Eccube\Doctrine\Query\Queries();
211
        };
212
        // TODO QueryCustomizerの追加方法は要検討
213 1104
        $app['eccube.queries']->addCustomizer(new \Acme\Entity\AdminProductListCustomizer());
214
215
        $app['eccube.purchase.context'] = $app->protect(function (ItemHolderInterface $origin = null) {
216 17
            return new PurchaseContext($origin);
217 1104
        });
218
219
        $app['eccube.purchase.flow.cart.item_processors'] = function ($app) {
220 24
            $processors = new ArrayCollection();
221 24
            $processors->add(new DeletedProductValidator());
222 24
            $processors->add(new DisplayStatusValidator());
223 24
            $processors->add(new SaleLimitValidator());
224 24
            $processors->add(new DeliverySettingValidator($app['eccube.repository.delivery']));
225
226 24
            return $processors;
227
        };
228
229
        $app['eccube.purchase.flow.cart.holder_processors'] = function ($app) {
230 24
            $processors = new ArrayCollection();
231 24
            $processors->add(new PaymentProcessor($app[DeliveryRepository::class]));
232 24
            $processors->add(new PaymentTotalLimitValidator($app['config']['max_total_fee']));
233 24
            $processors->add(new DeliveryFeeFreeProcessor($app[BaseInfo::class]));
234 24
            $processors->add(new PaymentTotalNegativeValidator());
235
236 24
            return $processors;
237
        };
238
239
        // example
240
        $app->extend('eccube.purchase.flow.cart.item_processors', function ($processors, $app) {
0 ignored issues
show
Unused Code introduced by
The parameter $app 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...
241
242 24
            $processors->add(new StockValidator());
243
244 24
            return $processors;
245 1104
        });
246
247
        $app['eccube.purchase.flow.cart'] = function ($app) {
248 24
            $flow = new PurchaseFlow();
249 24
            $flow->setItemProcessors($app['eccube.purchase.flow.cart.item_processors']);
250 24
            $flow->setItemHolderProcessors($app['eccube.purchase.flow.cart.holder_processors']);
251
252 24
            return $flow;
253
        };
254
255 View Code Duplication
        $app['eccube.purchase.flow.shopping'] = function () use ($app) {
256 21
            $flow = new PurchaseFlow();
257 21
            $flow->addItemProcessor(new StockValidator());
258 21
            $flow->addItemProcessor(new DisplayStatusValidator());
259 21
            $flow->addItemHolderProcessor(new PaymentTotalLimitValidator($app['config']['max_total_fee']));
260 21
            $flow->addItemHolderProcessor(new DeliveryFeeProcessor($app['orm.em']));
261 21
            $flow->addItemHolderProcessor(new PaymentTotalNegativeValidator());
262 21
            return $flow;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
263
        };
264
265 8 View Code Duplication
        $app['eccube.purchase.flow.order'] = function () use ($app) {
266 8
            $flow = new PurchaseFlow();
267 8
            $flow->addItemProcessor(new StockValidator());
268 8
            $flow->addItemHolderProcessor(new PaymentTotalLimitValidator($app['config']['max_total_fee']));
269 8
            $flow->addPurchaseProcessor(new UpdateDatePurchaseProcessor($app['config']));
270 8
            $flow->addPurchaseProcessor(new AdminOrderRegisterPurchaseProcessor($app));
0 ignored issues
show
Unused Code introduced by
The call to AdminOrderRegisterPurchaseProcessor::__construct() has too many arguments starting with $app.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
271 8
            return $flow;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
272
        };
273
    }
274
275 1104
    public function subscribe(Container $app, EventDispatcherInterface $dispatcher)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
276
    {
277
        // Add event subscriber to TaxRuleEvent
278 1104
        $app['orm.em']->getEventManager()->addEventSubscriber(new \Eccube\Doctrine\EventSubscriber\TaxRuleEventSubscriber($app[TaxRuleService::class]));
279
280 1104
        $dispatcher->addSubscriber(new TransactionListener($app));
0 ignored issues
show
Compatibility introduced by
$app of type object<Pimple\Container> is not a sub-type of object<Eccube\Application>. It seems like you assume a child class of the class Pimple\Container to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
281
    }
282
}
283