Completed
Pull Request — experimental/3.1 (#2517)
by Kiyotaka
34:45
created

EccubeServiceProvider   C

Complexity

Total Complexity 3

Size/Duplication

Total Lines 226
Duplicated Lines 7.52 %

Coupling/Cohesion

Components 0
Dependencies 33

Test Coverage

Coverage 59.65%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 17
loc 226
ccs 68
cts 114
cp 0.5965
rs 5
c 1
b 0
f 0
wmc 3
lcom 0
cbo 33

2 Methods

Rating   Name   Duplication   Size   Complexity  
B register() 17 207 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 1091
    public function register(Container $app)
65
    {
66
        $app[BaseInfo::class] = function () use ($app) {
67 1091
            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 1091
        });
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 1091
        });
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 1091
        });
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 1091
        });
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 1091
        });
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 1091
        });
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 1091
        });
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 113
                'hello',
187
                'jiro',
188
                'bbb'
189
            ];
190
        };
191
192
        // TODO 使用するか検討
193
        $app['twig_parsers'] = function () use ($app) {
194 113
            $GenericTokenParsers = [];
195 113
            foreach ($app['eccube.twig.generic_node_names'] as $tagName) {
196 113
                $GenericTokenParsers[] = new \Eccube\Twig\Extension\GenericTokenParser($app, $tagName);
197
            }
198 113
            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 1091
        $app['eccube.entity.event.dispatcher']->addEventListener(new \Acme\Entity\SoldOutEventListener());
209
        $app['eccube.queries'] = function () {
210 1089
            return new \Eccube\Doctrine\Query\Queries();
211
        };
212
213
        $app['eccube.purchase.context'] = $app->protect(function (ItemHolderInterface $origin = null) {
214 17
            return new PurchaseContext($origin);
215 1091
        });
216
217
        $app['eccube.purchase.flow.cart.item_processors'] = function ($app) {
218 24
            $processors = new ArrayCollection();
219 24
            $processors->add(new DisplayStatusValidator());
220 24
            $processors->add(new SaleLimitValidator());
221 24
            $processors->add(new DeliverySettingValidator($app['eccube.repository.delivery']));
222
223 24
            return $processors;
224
        };
225
226
        $app['eccube.purchase.flow.cart.holder_processors'] = function ($app) {
227 24
            $processors = new ArrayCollection();
228 24
            $processors->add(new PaymentProcessor($app[DeliveryRepository::class]));
229 24
            $processors->add(new PaymentTotalLimitValidator($app['config']['max_total_fee']));
230 24
            $processors->add(new DeliveryFeeFreeProcessor($app[BaseInfo::class]));
231 24
            $processors->add(new PaymentTotalNegativeValidator());
232
233 24
            return $processors;
234
        };
235
236
        // example
237
        $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...
238
239 24
            $processors->add(new StockValidator());
240
241 24
            return $processors;
242 1091
        });
243
244
        $app['eccube.purchase.flow.cart'] = function ($app) {
245 24
            $flow = new PurchaseFlow();
246 24
            $flow->setItemProcessors($app['eccube.purchase.flow.cart.item_processors']);
247 24
            $flow->setItemHolderProcessors($app['eccube.purchase.flow.cart.holder_processors']);
248
249 24
            return $flow;
250
        };
251
252 View Code Duplication
        $app['eccube.purchase.flow.shopping'] = function () use ($app) {
253 21
            $flow = new PurchaseFlow();
254 21
            $flow->addItemProcessor(new StockValidator());
255 21
            $flow->addItemProcessor(new DisplayStatusValidator());
256 21
            $flow->addItemHolderProcessor(new PaymentTotalLimitValidator($app['config']['max_total_fee']));
257 21
            $flow->addItemHolderProcessor(new DeliveryFeeProcessor($app['orm.em']));
258 21
            $flow->addItemHolderProcessor(new PaymentTotalNegativeValidator());
259 21
            return $flow;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
260
        };
261
262 8 View Code Duplication
        $app['eccube.purchase.flow.order'] = function () use ($app) {
263 8
            $flow = new PurchaseFlow();
264 8
            $flow->addItemProcessor(new StockValidator());
265 8
            $flow->addItemHolderProcessor(new PaymentTotalLimitValidator($app['config']['max_total_fee']));
266 8
            $flow->addPurchaseProcessor(new UpdateDatePurchaseProcessor($app['config']));
267 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...
268 8
            return $flow;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
269
        };
270
    }
271
272 1091
    public function subscribe(Container $app, EventDispatcherInterface $dispatcher)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
273
    {
274
        // Add event subscriber to TaxRuleEvent
275 1091
        $app['orm.em']->getEventManager()->addEventSubscriber(new \Eccube\Doctrine\EventSubscriber\TaxRuleEventSubscriber($app[TaxRuleService::class]));
276
277 1091
        $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...
278
    }
279
}
280