Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Application\Search;
  4.  
  5. use Application\Model\Repository\BillRepositoryInterface;
  6. use Application\Model\Repository\BillProfileRepositoryInterface;
  7.  
  8. class BillSearchResultBillProfileKeywordsDecorator implements BillSearchResultDecoratorInterface
  9. {
  10. protected $profileIds = [];
  11.  
  12. protected $keywordRepository;
  13.  
  14. /**
  15. * This array has billIds as the keys and an array of keywords for $this->profileIds as values
  16. */
  17. protected $billIdKeywordsMap = [];
  18.  
  19. public function __construct(KeywordRepositoryInterface $keywordRepository, array $profileIds = [])
  20. {
  21. $this->keywordRepository = $keywordRepository;
  22. $this->profileIds = [];
  23. }
  24.  
  25. /**
  26. * Add keywords to a single search result
  27. *
  28. * @param BillSearchResult $result
  29. * @param array $profileIds
  30. * @return BillSearchResult
  31. */
  32. public function decorate(BillSearchResult $result)
  33. {
  34. $billId = $result->getId();
  35. if (!isset($this->billIdKeywordsMap[$billId])) {
  36. $this->buildBillIdKeywordsMap([$billId]);
  37. }
  38. $result->setOptionalFieldCollection(
  39. new BillProfileKeywordsOptionFieldDecorator(
  40. $result->getOptionalFieldCollection(),
  41. OptionalField::billProfileKeywords($this->billIdKeywordsMap[$billId])
  42. )
  43. );
  44.  
  45. return $result;
  46. }
  47.  
  48. /**
  49. * Add keywords to each search result in a collection
  50. *
  51. * @param BillSearchResultCollection $collection
  52. * @param array $profileIds
  53. * @return BillSearchResultCollection
  54. */
  55. public function decorateAll(BillSearchResultCollection $collection)
  56. {
  57. // Collect all of the bill ids
  58. $billIds = [];
  59. $results = $collection->getResults();
  60. foreach ($results as $i => $result) {
  61. $billIds[] = $result->getId();
  62. }
  63. // Build the keywords map for them so decorate wont hit the database at all
  64. $this->buildBillIdKeywordsMap($billIds);
  65.  
  66. // Decorate each result
  67. foreach ($results as $i => $result) {
  68. $this->decorate($result);
  69. }
  70.  
  71. return $collection;
  72. }
  73.  
  74. /**
  75. * @param int $billId
  76. * @param array $profileIds
  77. */
  78. public function buildBillIdKeywordsMap($billIds, array $profileIds)
  79. {
  80. // You will need to add this method to KeywordRepositoryInterface and KeywordDoctrineRepository
  81. // This method needs to return an array where the keys are billIds and the values are an array of keywords for the given profileIds
  82. $this->billIdKeywordsMap = $this->keywordRepository->findKeywordsForBillsInProfiles($billIds, $profileIds);
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement