Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Config.php
Go to the documentation of this file.
1 <?php
7 declare(strict_types=1);
8 
10 
17 
19 {
23  private $cache;
24 
28  private $webApiConfig;
29 
33  private $serializer;
34 
38  private $asyncServices;
39 
47  public function __construct(
48  WebapiCache $cache,
49  WebapiConfig $webApiConfig,
50  SerializerInterface $serializer = null
51  ) {
52  $this->cache = $cache;
53  $this->webApiConfig = $webApiConfig;
54  $this->serializer = $serializer ? : ObjectManager::getInstance()->get(SerializerInterface::class);
55  }
56 
60  public function getServices()
61  {
62  if (null === $this->asyncServices) {
63  $services = $this->cache->load(self::CACHE_ID);
64  if ($services && is_string($services)) {
65  $this->asyncServices = $this->serializer->unserialize($services);
66  } else {
67  $this->asyncServices = $this->generateTopicsDataFromWebapiConfig();
68  $this->cache->save($this->serializer->serialize($this->asyncServices), self::CACHE_ID);
69  }
70  }
71 
72  return $this->asyncServices;
73  }
74 
78  public function getTopicName($routeUrl, $httpMethod)
79  {
80  $services = $this->getServices();
81  $topicName = $this->generateTopicNameByRouteData(
82  $routeUrl,
83  $httpMethod
84  );
85 
86  if (array_key_exists($topicName, $services) === false) {
87  throw new LocalizedException(
88  __('WebapiAsync config for "%topicName" does not exist.', ['topicName' => $topicName])
89  );
90  }
91 
92  return $services[$topicName][self::SERVICE_PARAM_KEY_TOPIC];
93  }
94 
98  private function generateTopicsDataFromWebapiConfig()
99  {
100  $webApiConfig = $this->webApiConfig->getServices();
101  $services = [];
102  foreach ($webApiConfig[Converter::KEY_ROUTES] as $routeUrl => $routeData) {
103  foreach ($routeData as $httpMethod => $httpMethodData) {
104  if ($httpMethod !== \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET) {
105  $serviceInterface = $httpMethodData[Converter::KEY_SERVICE][Converter::KEY_SERVICE_CLASS];
106  $serviceMethod = $httpMethodData[Converter::KEY_SERVICE][Converter::KEY_SERVICE_METHOD];
107 
108  $topicName = $this->generateTopicNameByRouteData(
109  $routeUrl,
110  $httpMethod
111  );
112  $services[$topicName] = [
113  self::SERVICE_PARAM_KEY_INTERFACE => $serviceInterface,
114  self::SERVICE_PARAM_KEY_METHOD => $serviceMethod,
115  self::SERVICE_PARAM_KEY_TOPIC => $topicName,
116  ];
117  }
118  }
119  }
120 
121  return $services;
122  }
123 
134  private function generateTopicNameByRouteData($routeUrl, $httpMethod)
135  {
136  return self::TOPIC_PREFIX . $this->generateTopicName($routeUrl, $httpMethod, '/', false);
137  }
138 
146  private function generateTopicName($typeName, $methodName, $delimiter = '\\', $lcfirst = true)
147  {
148  $parts = explode($delimiter, ltrim($typeName, $delimiter));
149  foreach ($parts as &$part) {
150  $part = ltrim($part, ':');
151  if ($lcfirst === true) {
152  $part = lcfirst($part);
153  }
154  }
155 
156  return implode('.', $parts) . '.' . $methodName;
157  }
158 }
__construct(WebapiCache $cache, WebapiConfig $webApiConfig, SerializerInterface $serializer=null)
Definition: Config.php:47
getTopicName($routeUrl, $httpMethod)
Definition: Config.php:78
__()
Definition: __.php:13