Advertisement
Guest User

Untitled

a guest
Aug 12th, 2015
736
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.82 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Aquatuning Software Development - Bundle - Component
  5. *
  6. * @category Aquatuning
  7. * @package Shopware\Plugins\AtsdBundle
  8. * @copyright Copyright (c) 2013, Aquatuning GmbH
  9. */
  10.  
  11. namespace Shopware\AtsdBundle\Bundle\StoreFrontBundle;
  12.  
  13.  
  14.  
  15. /**
  16. * Aquatuning Software Development - Bundle - Component
  17. */
  18.  
  19. class BundleService
  20. {
  21.  
  22. /**
  23. * Shopware model manager.
  24. *
  25. * @var \Shopware\Components\Model\ModelManager
  26. */
  27.  
  28. private $modelManager;
  29.  
  30.  
  31.  
  32. /**
  33. * Plugin component.
  34. *
  35. * @var \Shopware\AtsdBundle\Components\AtsdBundle
  36. */
  37.  
  38. private $component;
  39.  
  40.  
  41.  
  42. /**
  43. * DI container.
  44. *
  45. * @var \Shopware\Components\DependencyInjection\Container
  46. */
  47.  
  48. private $container;
  49.  
  50.  
  51.  
  52.  
  53.  
  54. /**
  55. * Object constructor.
  56. *
  57. * @param \Shopware\Components\Model\ModelManager $modelManager
  58. * @param \Shopware\AtsdBundle\Components\AtsdBundle $component
  59. * @param \Shopware\Components\DependencyInjection\Container $container
  60. *
  61. * @return \Shopware\AtsdBundle\Bundle\StoreFrontBundle\BundleService
  62. */
  63.  
  64. public function __construct(
  65. \Shopware\Components\Model\ModelManager $modelManager,
  66. \Shopware\AtsdBundle\Components\AtsdBundle $component,
  67. \Shopware\Components\DependencyInjection\Container $container )
  68. {
  69. // set parameters
  70. $this->modelManager = $modelManager;
  71. $this->component = $component;
  72. $this->container = $container;
  73. }
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81. /**
  82. * Get all bundles for the given products. The bundles are returned in an array
  83. * with the article id as key and the bundles as value.
  84. *
  85. * @param \Shopware\Bundle\StoreFrontBundle\Struct\ListProduct[] $products
  86. *
  87. * @return array
  88. */
  89.  
  90. public function getList( array $products )
  91. {
  92. // get article ids
  93. $articleIds = $this->getArticleId( $products );
  94.  
  95. // no articles given?
  96. if ( count( $articleIds ) == 0 )
  97. // done
  98. return array();
  99.  
  100.  
  101.  
  102. // get query builder
  103. $builder = $this->component->getRepository()->getActiveBundlesWithArticlesQueryBuilder(
  104. $this->component->getCustomerGroup(),
  105. $this->component->getMainShop()
  106. );
  107.  
  108. // only for our products
  109. $builder->andWhere(
  110. $builder->expr()->in( "article.id", $articleIds )
  111. );
  112.  
  113. // get the results as array
  114. $bundles = $builder->getQuery()->getArrayResult();
  115.  
  116.  
  117.  
  118. // return array with article id as key
  119. $return = array();
  120.  
  121. // loop the bundles
  122. foreach ( $bundles as $bundle )
  123. {
  124. // does it even validate?
  125. if ( $this->component->validateBundle( $bundle, 1 ) == false )
  126. // ignore it
  127. continue;
  128.  
  129. // get article id
  130. $id = $bundle['article']['id'];
  131.  
  132. // not set?
  133. if ( !isset( $return[$id] ) )
  134. // set it
  135. $return[$id] = array();
  136.  
  137. // add
  138. array_push( $return[$id], $bundle );
  139. }
  140.  
  141.  
  142.  
  143. // return them
  144. return $return;
  145. }
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154. /**
  155. * ...
  156. *
  157. * @param array $products
  158. *
  159. * @return array
  160. */
  161.  
  162. private function getArticleId( array $products )
  163. {
  164. // get the ids
  165. $articleIds = array_map(
  166. function( \Shopware\Bundle\StoreFrontBundle\Struct\ListProduct $product ) {
  167. return $product->getId();
  168. },
  169. $products
  170. );
  171.  
  172. // unique the ids
  173. $articleIds = array_unique( array_values( $articleIds ) );
  174.  
  175. // return them
  176. return $articleIds;
  177. }
  178.  
  179.  
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement