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 |
||
| 54 | class EccubeServiceProvider implements ServiceProviderInterface, EventListenerProviderInterface |
||
|
|
|||
| 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 | 1074 | public function register(Container $app) |
|
| 65 | { |
||
| 66 | $app[BaseInfo::class] = function () use ($app) { |
||
| 67 | 1074 | 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); |
||
| 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; |
||
| 86 | }; |
||
| 87 | $app['eccube.calculate.strategy.shipping'] = function () use ($app) { |
||
| 88 | $Strategy = new \Eccube\Service\Calculator\Strategy\ShippingStrategy(); |
||
| 89 | $Strategy->setApplication($app); |
||
| 90 | return $Strategy; |
||
| 91 | }; |
||
| 92 | $app['eccube.calculate.strategy.charge'] = function () use ($app) { |
||
| 93 | $Strategy = new \Eccube\Service\Calculator\Strategy\ChargeStrategy(); |
||
| 94 | $Strategy->setApplication($app); |
||
| 95 | return $Strategy; |
||
| 96 | }; |
||
| 97 | |||
| 98 | $app['eccube.calculate.strategy.tax'] = function () use ($app) { |
||
| 99 | $Strategy = new \Eccube\Service\Calculator\Strategy\TaxStrategy(); |
||
| 100 | $Strategy->setApplication($app); |
||
| 101 | return $Strategy; |
||
| 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); |
||
| 107 | return $Strategy; |
||
| 108 | }; |
||
| 109 | $app['eccube.calculate.strategy.calculate_charge'] = function () use ($app) { |
||
| 110 | $Strategy = new \Eccube\Service\Calculator\Strategy\CalculateChargeStrategy(); |
||
| 111 | $Strategy->setApplication($app); |
||
| 112 | return $Strategy; |
||
| 113 | }; |
||
| 114 | $app['eccube.calculate.strategy.calculate_total'] = function () use ($app) { |
||
| 115 | $Strategy = new \Eccube\Service\Calculator\Strategy\CalculateTotalStrategy(); |
||
| 116 | $Strategy->setApplication($app); |
||
| 117 | return $Strategy; |
||
| 118 | }; |
||
| 119 | |||
| 120 | $app['payment.method'] = $app->protect(function ($clazz, $form) use ($app) { |
||
| 121 | 1 | $PaymentMethod = new $clazz; |
|
| 122 | 1 | $PaymentMethod->setApplication($app); |
|
| 123 | 1 | $PaymentMethod->setFormType($form); |
|
| 124 | 1 | return $PaymentMethod; |
|
| 125 | 1074 | }); |
|
| 126 | |||
| 127 | $app['payment.method.request'] = $app->protect(function ($clazz, $form, $request) use ($app) { |
||
| 128 | 2 | $PaymentMethod = new $clazz; |
|
| 129 | 2 | $PaymentMethod->setApplication($app); |
|
| 130 | 2 | $PaymentMethod->setFormType($form); |
|
| 131 | 2 | $PaymentMethod->setRequest($request); |
|
| 132 | 2 | return $PaymentMethod; |
|
| 133 | 1074 | }); |
|
| 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 | 1074 | }); |
|
| 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 | 1074 | }); |
|
| 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 | 1074 | }); |
|
| 158 | |||
| 159 | $app['request_scope'] = function () { |
||
| 160 | 22 | 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 | 1074 | }); |
|
| 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 | 1074 | }); |
|
| 182 | |||
| 183 | // TODO 使用するか検討 |
||
| 184 | $app['eccube.twig.generic_node_names'] = function () use ($app) { |
||
| 185 | return [ |
||
| 186 | 114 | 'hello', |
|
| 187 | 'jiro', |
||
| 188 | 'bbb' |
||
| 189 | ]; |
||
| 190 | }; |
||
| 191 | |||
| 192 | // TODO 使用するか検討 |
||
| 193 | $app['twig_parsers'] = function () use ($app) { |
||
| 194 | 114 | $GenericTokenParsers = []; |
|
| 195 | 114 | foreach ($app['eccube.twig.generic_node_names'] as $tagName) { |
|
| 196 | 114 | $GenericTokenParsers[] = new \Eccube\Twig\Extension\GenericTokenParser($app, $tagName); |
|
| 197 | } |
||
| 198 | 114 | return $GenericTokenParsers; |
|
| 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 | $app['eccube.queries'] = function () { |
||
| 209 | 342 | return new \Eccube\Doctrine\Query\Queries(); |
|
| 210 | }; |
||
| 211 | |||
| 212 | $app['eccube.purchase.context'] = $app->protect(function (ItemHolderInterface $origin = null) { |
||
| 213 | 17 | return new PurchaseContext($origin); |
|
| 214 | 1074 | }); |
|
| 215 | |||
| 216 | $app['eccube.purchase.flow.cart.item_processors'] = function ($app) { |
||
| 217 | 24 | $processors = new ArrayCollection(); |
|
| 218 | 24 | $processors->add(new DisplayStatusValidator()); |
|
| 219 | 24 | $processors->add(new SaleLimitValidator()); |
|
| 220 | 24 | $processors->add(new DeliverySettingValidator($app['eccube.repository.delivery'])); |
|
| 221 | |||
| 222 | 24 | return $processors; |
|
| 223 | }; |
||
| 224 | |||
| 225 | $app['eccube.purchase.flow.cart.holder_processors'] = function ($app) { |
||
| 226 | 24 | $processors = new ArrayCollection(); |
|
| 227 | 24 | $processors->add(new PaymentProcessor($app[DeliveryRepository::class])); |
|
| 228 | 24 | $processors->add(new PaymentTotalLimitValidator($app['config']['max_total_fee'])); |
|
| 229 | 24 | $processors->add(new DeliveryFeeFreeProcessor($app[BaseInfo::class])); |
|
| 230 | 24 | $processors->add(new PaymentTotalNegativeValidator()); |
|
| 231 | |||
| 232 | 24 | return $processors; |
|
| 233 | }; |
||
| 234 | |||
| 235 | // example |
||
| 236 | $app->extend('eccube.purchase.flow.cart.item_processors', function ($processors, $app) { |
||
| 237 | |||
| 238 | 24 | $processors->add(new StockValidator()); |
|
| 239 | |||
| 240 | 24 | return $processors; |
|
| 241 | 1074 | }); |
|
| 242 | |||
| 243 | $app['eccube.purchase.flow.cart'] = function ($app) { |
||
| 244 | 24 | $flow = new PurchaseFlow(); |
|
| 245 | 24 | $flow->setItemProcessors($app['eccube.purchase.flow.cart.item_processors']); |
|
| 246 | 24 | $flow->setItemHolderProcessors($app['eccube.purchase.flow.cart.holder_processors']); |
|
| 247 | |||
| 248 | 24 | return $flow; |
|
| 249 | }; |
||
| 250 | |||
| 251 | View Code Duplication | $app['eccube.purchase.flow.shopping'] = function () use ($app) { |
|
| 252 | 22 | $flow = new PurchaseFlow(); |
|
| 253 | 22 | $flow->addItemProcessor(new StockValidator()); |
|
| 254 | 22 | $flow->addItemProcessor(new DisplayStatusValidator()); |
|
| 255 | 22 | $flow->addItemHolderProcessor(new PaymentTotalLimitValidator($app['config']['max_total_fee'])); |
|
| 256 | 22 | $flow->addItemHolderProcessor(new DeliveryFeeProcessor($app['orm.em'])); |
|
| 257 | 22 | $flow->addItemHolderProcessor(new PaymentTotalNegativeValidator()); |
|
| 258 | 22 | return $flow; |
|
| 259 | }; |
||
| 260 | |||
| 261 | 8 | View Code Duplication | $app['eccube.purchase.flow.order'] = function () use ($app) { |
| 262 | 8 | $flow = new PurchaseFlow(); |
|
| 263 | 8 | $flow->addItemProcessor(new StockValidator()); |
|
| 264 | 8 | $flow->addItemHolderProcessor(new PaymentTotalLimitValidator($app['config']['max_total_fee'])); |
|
| 265 | 8 | $flow->addPurchaseProcessor(new UpdateDatePurchaseProcessor($app['config'])); |
|
| 266 | 8 | $flow->addPurchaseProcessor(new AdminOrderRegisterPurchaseProcessor($app)); |
|
| 267 | 8 | return $flow; |
|
| 268 | }; |
||
| 269 | } |
||
| 270 | |||
| 271 | 1074 | public function subscribe(Container $app, EventDispatcherInterface $dispatcher) |
|
| 279 | } |
||
| 280 |