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 |
||
| 53 | class EccubeServiceProvider implements ServiceProviderInterface, EventListenerProviderInterface |
||
|
|
|||
| 54 | { |
||
| 55 | /** |
||
| 56 | * Registers services on the given app. |
||
| 57 | * |
||
| 58 | * This method should only be used to configure services and parameters. |
||
| 59 | * It should not get services. |
||
| 60 | * |
||
| 61 | * @param BaseApplication $app An Application instance |
||
| 62 | */ |
||
| 63 | 1104 | public function register(Container $app) |
|
| 64 | { |
||
| 65 | $app['eccube.calculate.context'] = function () use ($app) { |
||
| 66 | return new \Eccube\Service\Calculator\CalculateContext(); |
||
| 67 | }; |
||
| 68 | |||
| 69 | $app['eccube.calculate.strategies'] = function () use ($app) { |
||
| 70 | $Collection = new \Eccube\Service\Calculator\CalculateStrategyCollection(); |
||
| 71 | $Collection->setApplication($app); |
||
| 72 | //$Collection->setOrder($Order); |
||
| 73 | // デフォルトのストラテジーをセットしておく |
||
| 74 | $Collection->add($app['eccube.calculate.strategy.shipping']); |
||
| 75 | $Collection->add($app['eccube.calculate.strategy.charge']); |
||
| 76 | $Collection->add($app['eccube.calculate.strategy.tax']); |
||
| 77 | $Collection->add($app['eccube.calculate.strategy.calculate_delivery_fee']); |
||
| 78 | $Collection->add($app['eccube.calculate.strategy.calculate_charge']); |
||
| 79 | $Collection->add($app['eccube.calculate.strategy.calculate_total']); |
||
| 80 | return $Collection; |
||
| 81 | }; |
||
| 82 | $app['eccube.calculate.strategy.shipping'] = function () use ($app) { |
||
| 83 | $Strategy = new \Eccube\Service\Calculator\Strategy\ShippingStrategy(); |
||
| 84 | $Strategy->setApplication($app); |
||
| 85 | return $Strategy; |
||
| 86 | }; |
||
| 87 | $app['eccube.calculate.strategy.charge'] = function () use ($app) { |
||
| 88 | $Strategy = new \Eccube\Service\Calculator\Strategy\ChargeStrategy(); |
||
| 89 | $Strategy->setApplication($app); |
||
| 90 | return $Strategy; |
||
| 91 | }; |
||
| 92 | |||
| 93 | $app['eccube.calculate.strategy.tax'] = function () use ($app) { |
||
| 94 | $Strategy = new \Eccube\Service\Calculator\Strategy\TaxStrategy(); |
||
| 95 | $Strategy->setApplication($app); |
||
| 96 | return $Strategy; |
||
| 97 | }; |
||
| 98 | |||
| 99 | $app['eccube.calculate.strategy.calculate_delivery_fee'] = function () use ($app) { |
||
| 100 | $Strategy = new \Eccube\Service\Calculator\Strategy\CalculateDeliveryFeeStrategy(); |
||
| 101 | $Strategy->setApplication($app); |
||
| 102 | return $Strategy; |
||
| 103 | }; |
||
| 104 | $app['eccube.calculate.strategy.calculate_charge'] = function () use ($app) { |
||
| 105 | $Strategy = new \Eccube\Service\Calculator\Strategy\CalculateChargeStrategy(); |
||
| 106 | $Strategy->setApplication($app); |
||
| 107 | return $Strategy; |
||
| 108 | }; |
||
| 109 | $app['eccube.calculate.strategy.calculate_total'] = function () use ($app) { |
||
| 110 | $Strategy = new \Eccube\Service\Calculator\Strategy\CalculateTotalStrategy(); |
||
| 111 | $Strategy->setApplication($app); |
||
| 112 | return $Strategy; |
||
| 113 | }; |
||
| 114 | |||
| 115 | $app['payment.method'] = $app->protect(function ($clazz, $form) use ($app) { |
||
| 116 | 1 | $PaymentMethod = new $clazz; |
|
| 117 | 1 | $PaymentMethod->setApplication($app); |
|
| 118 | 1 | $PaymentMethod->setFormType($form); |
|
| 119 | 1 | return $PaymentMethod; |
|
| 120 | 1104 | }); |
|
| 121 | |||
| 122 | $app['payment.method.request'] = $app->protect(function ($clazz, $form, $request) use ($app) { |
||
| 123 | 2 | $PaymentMethod = new $clazz; |
|
| 124 | 2 | $PaymentMethod->setApplication($app); |
|
| 125 | 2 | $PaymentMethod->setFormType($form); |
|
| 126 | 2 | $PaymentMethod->setRequest($request); |
|
| 127 | 2 | return $PaymentMethod; |
|
| 128 | 1104 | }); |
|
| 129 | |||
| 130 | $app['eccube.service.calculate'] = $app->protect(function ($Order, $Customer) use ($app) { |
||
| 131 | $Service = new \Eccube\Service\CalculateService($Order, $Customer); |
||
| 132 | $Context = $app['eccube.calculate.context']; |
||
| 133 | $app['eccube.calculate.strategies']->setOrder($Order); |
||
| 134 | $Context->setCalculateStrategies($app['eccube.calculate.strategies']); |
||
| 135 | $Context->setOrder($Order); |
||
| 136 | $Service->setContext($Context); |
||
| 137 | |||
| 138 | return $Service; |
||
| 139 | 1104 | }); |
|
| 140 | |||
| 141 | $app['eccube.service.payment'] = $app->protect(function ($clazz) use ($app) { |
||
| 142 | 3 | $Service = new $clazz($app['request_stack']); |
|
| 143 | |||
| 144 | 3 | return $Service; |
|
| 145 | 1104 | }); |
|
| 146 | |||
| 147 | $app['paginator'] = $app->protect(function () { |
||
| 148 | 29 | $paginator = new \Knp\Component\Pager\Paginator(); |
|
| 149 | 29 | $paginator->subscribe(new \Eccube\EventListener\PaginatorListener()); |
|
| 150 | |||
| 151 | 29 | return $paginator; |
|
| 152 | 1104 | }); |
|
| 153 | |||
| 154 | $app['request_scope'] = function () { |
||
| 155 | 21 | return new ParameterBag(); |
|
| 156 | }; |
||
| 157 | // TODO 使用するか検討 |
||
| 158 | $app['eccube.twig.node.hello'] = $app->protect(function ($node, $compiler) { |
||
| 159 | $compiler |
||
| 160 | ->addDebugInfo($node) |
||
| 161 | ->write("echo 'Helloooooo ' . ") |
||
| 162 | ->subcompile($node->getNode('expr')) |
||
| 163 | ->raw(" . '!';\n") |
||
| 164 | ; |
||
| 165 | |||
| 166 | 1104 | }); |
|
| 167 | // TODO 使用するか検討 |
||
| 168 | $app['eccube.twig.node.jiro'] = $app->protect(function ($node, $compiler) { |
||
| 169 | $compiler |
||
| 170 | ->addDebugInfo($node) |
||
| 171 | ->write("echo 'jirooooooo ' . ") |
||
| 172 | ->subcompile($node->getNode('expr')) |
||
| 173 | ->raw(" . '!';\n") |
||
| 174 | ; |
||
| 175 | |||
| 176 | 1104 | }); |
|
| 177 | |||
| 178 | // TODO 使用するか検討 |
||
| 179 | $app['eccube.twig.generic_node_names'] = function () use ($app) { |
||
| 180 | return [ |
||
| 181 | 116 | 'hello', |
|
| 182 | 'jiro', |
||
| 183 | 'bbb' |
||
| 184 | ]; |
||
| 185 | }; |
||
| 186 | |||
| 187 | // TODO 使用するか検討 |
||
| 188 | $app['twig_parsers'] = function () use ($app) { |
||
| 189 | 116 | $GenericTokenParsers = []; |
|
| 190 | 116 | foreach ($app['eccube.twig.generic_node_names'] as $tagName) { |
|
| 191 | 116 | $GenericTokenParsers[] = new \Eccube\Twig\Extension\GenericTokenParser($app, $tagName); |
|
| 192 | } |
||
| 193 | 116 | return $GenericTokenParsers; |
|
| 194 | }; |
||
| 195 | |||
| 196 | $app['eccube.twig.block.templates'] = function () { |
||
| 197 | $templates = new ArrayCollection(); |
||
| 198 | $templates[] = 'render_block.twig'; |
||
| 199 | |||
| 200 | return $templates; |
||
| 201 | }; |
||
| 202 | |||
| 203 | 1104 | $app['eccube.entity.event.dispatcher']->addEventListener(new \Acme\Entity\SoldOutEventListener()); |
|
| 204 | $app['eccube.queries'] = function () { |
||
| 205 | 1104 | return new \Eccube\Doctrine\Query\Queries(); |
|
| 206 | }; |
||
| 207 | // TODO QueryCustomizerの追加方法は要検討 |
||
| 208 | 1104 | $app['eccube.queries']->addCustomizer(new \Acme\Entity\AdminProductListCustomizer()); |
|
| 209 | |||
| 210 | $app['eccube.purchase.context'] = $app->protect(function (ItemHolderInterface $origin = null) { |
||
| 211 | 17 | return new PurchaseContext($origin); |
|
| 212 | 1104 | }); |
|
| 213 | |||
| 214 | $app['eccube.purchase.flow.cart.item_processors'] = function ($app) { |
||
| 215 | 24 | $processors = new ArrayCollection(); |
|
| 216 | 24 | $processors->add(new DeletedProductValidator()); |
|
| 217 | 24 | $processors->add(new DisplayStatusValidator()); |
|
| 218 | 24 | $processors->add(new SaleLimitValidator()); |
|
| 219 | 24 | $processors->add(new DeliverySettingValidator($app['eccube.repository.delivery'])); |
|
| 220 | |||
| 221 | 24 | return $processors; |
|
| 222 | }; |
||
| 223 | |||
| 224 | $app['eccube.purchase.flow.cart.holder_processors'] = function ($app) { |
||
| 225 | 24 | $processors = new ArrayCollection(); |
|
| 226 | 24 | $processors->add(new PaymentProcessor($app[DeliveryRepository::class])); |
|
| 227 | 24 | $processors->add(new PaymentTotalLimitValidator($app['config']['max_total_fee'])); |
|
| 228 | 24 | $processors->add(new DeliveryFeeFreeProcessor($app[BaseInfoRepository::class])); |
|
| 229 | 24 | $processors->add(new PaymentTotalNegativeValidator()); |
|
| 230 | |||
| 231 | 24 | return $processors; |
|
| 232 | }; |
||
| 233 | |||
| 234 | // example |
||
| 235 | $app->extend('eccube.purchase.flow.cart.item_processors', function ($processors, $app) { |
||
| 236 | |||
| 237 | 24 | $processors->add(new StockValidator()); |
|
| 238 | |||
| 239 | 24 | return $processors; |
|
| 240 | 1104 | }); |
|
| 241 | |||
| 242 | $app['eccube.purchase.flow.cart'] = function ($app) { |
||
| 243 | 24 | $flow = new PurchaseFlow(); |
|
| 244 | 24 | $flow->setItemProcessors($app['eccube.purchase.flow.cart.item_processors']); |
|
| 245 | 24 | $flow->setItemHolderProcessors($app['eccube.purchase.flow.cart.holder_processors']); |
|
| 246 | |||
| 247 | 24 | return $flow; |
|
| 248 | }; |
||
| 249 | |||
| 250 | View Code Duplication | $app['eccube.purchase.flow.shopping'] = function () use ($app) { |
|
| 251 | 21 | $flow = new PurchaseFlow(); |
|
| 252 | 21 | $flow->addItemProcessor(new StockValidator()); |
|
| 253 | 21 | $flow->addItemProcessor(new DisplayStatusValidator()); |
|
| 254 | 21 | $flow->addItemHolderProcessor(new PaymentTotalLimitValidator($app['config']['max_total_fee'])); |
|
| 255 | 21 | $flow->addItemHolderProcessor(new DeliveryFeeProcessor($app['orm.em'])); |
|
| 256 | 21 | $flow->addItemHolderProcessor(new PaymentTotalNegativeValidator()); |
|
| 257 | 21 | return $flow; |
|
| 258 | }; |
||
| 259 | |||
| 260 | 8 | View Code Duplication | $app['eccube.purchase.flow.order'] = function () use ($app) { |
| 261 | 8 | $flow = new PurchaseFlow(); |
|
| 262 | 8 | $flow->addItemProcessor(new StockValidator()); |
|
| 263 | 8 | $flow->addItemHolderProcessor(new PaymentTotalLimitValidator($app['config']['max_total_fee'])); |
|
| 264 | 8 | $flow->addPurchaseProcessor(new UpdateDatePurchaseProcessor($app['config'])); |
|
| 265 | 8 | $flow->addPurchaseProcessor(new AdminOrderRegisterPurchaseProcessor($app)); |
|
| 266 | 8 | return $flow; |
|
| 267 | }; |
||
| 268 | } |
||
| 269 | |||
| 270 | 1104 | public function subscribe(Container $app, EventDispatcherInterface $dispatcher) |
|
| 277 | } |
||
| 278 |