Partners.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Marketplace\Model;
  7. use Magento\Framework\HTTP\Client\Curl;
  8. use Magento\Marketplace\Helper\Cache;
  9. use Magento\Backend\Model\UrlInterface;
  10. /**
  11. * @api
  12. * @since 100.0.2
  13. */
  14. class Partners
  15. {
  16. /**
  17. * @var Curl
  18. */
  19. protected $curlClient;
  20. /**
  21. * @var string
  22. */
  23. protected $urlPrefix = 'https://';
  24. /**
  25. * @var string
  26. */
  27. protected $apiUrl = 'magento.com/magento-connect/platinumpartners/list';
  28. /**
  29. * @var \Magento\Marketplace\Helper\Cache
  30. */
  31. protected $cache;
  32. /**
  33. * @param Curl $curl
  34. * @param Cache $cache
  35. * @param UrlInterface $backendUrl
  36. */
  37. public function __construct(Curl $curl, Cache $cache, UrlInterface $backendUrl)
  38. {
  39. $this->curlClient = $curl;
  40. $this->cache = $cache;
  41. $this->backendUrl = $backendUrl;
  42. }
  43. /**
  44. * @return string
  45. */
  46. public function getApiUrl()
  47. {
  48. return $this->urlPrefix . $this->apiUrl;
  49. }
  50. /**
  51. * Gets partners json
  52. *
  53. * @return array
  54. */
  55. public function getPartners()
  56. {
  57. $apiUrl = $this->getApiUrl();
  58. try {
  59. $this->getCurlClient()->post($apiUrl, []);
  60. $this->getCurlClient()->setOptions(
  61. [
  62. CURLOPT_REFERER => $this->getReferer()
  63. ]
  64. );
  65. $response = json_decode($this->getCurlClient()->getBody(), true);
  66. if ($response['partners']) {
  67. $this->getCache()->savePartnersToCache($response['partners']);
  68. return $response['partners'];
  69. } else {
  70. return $this->getCache()->loadPartnersFromCache();
  71. }
  72. } catch (\Exception $e) {
  73. return $this->getCache()->loadPartnersFromCache();
  74. }
  75. }
  76. /**
  77. * @return Curl
  78. */
  79. public function getCurlClient()
  80. {
  81. return $this->curlClient;
  82. }
  83. /**
  84. * @return cache
  85. */
  86. public function getCache()
  87. {
  88. return $this->cache;
  89. }
  90. /**
  91. * @return string
  92. */
  93. public function getReferer()
  94. {
  95. return \Magento\Framework\App\Request\Http::getUrlNoScript($this->backendUrl->getBaseUrl())
  96. . 'admin/marketplace/index/index';
  97. }
  98. }