Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Notifications.php
Go to the documentation of this file.
1 <?php
8 
17 use Psr\Log\LoggerInterface;
18 
23 {
27  private $contentProvider;
28 
32  private $renderer;
33 
39  private static $cachePrefix = 'release-notification-content-';
40 
44  private $cacheStorage;
45 
49  private $serializer;
50 
54  private $productMetadata;
55 
59  private $session;
60 
64  private $logger;
65 
75  public function __construct(
76  ContentProviderInterface $contentProvider,
77  NotificationRenderer $render,
78  CacheInterface $cacheStorage,
79  SerializerInterface $serializer,
80  ProductMetadataInterface $productMetadata,
81  Session $session,
82  LoggerInterface $logger
83  ) {
84  $this->contentProvider = $contentProvider;
85  $this->renderer = $render;
86  $this->cacheStorage = $cacheStorage;
87  $this->serializer = $serializer;
88  $this->productMetadata = $productMetadata;
89  $this->session = $session;
90  $this->logger = $logger;
91  }
92 
96  public function modifyData(array $data)
97  {
98  return $data;
99  }
100 
104  public function modifyMeta(array $meta)
105  {
106  $modalContent = $this->getNotificationContent();
107 
108  if ($modalContent) {
109  $pages = $modalContent['pages'];
110  $pageCount = count($pages);
111  $counter = 1;
112 
113  foreach ($pages as $page) {
114  $meta = $this->buildNotificationMeta($meta, $page, $counter++ == $pageCount);
115  }
116  } else {
117  $meta = $this->hideNotification($meta);
118  }
119 
120  return $meta;
121  }
122 
131  private function buildNotificationMeta(array $meta, array $page, $isLastPage)
132  {
133  $meta['notification_modal_' . $page['name']]['arguments']['data']['config'] = [
134  'isTemplate' => false,
135  'componentType' => Component\Modal::NAME
136  ];
137 
138  $meta['notification_modal_' . $page['name']]['children']['notification_fieldset']['children']
139  ['notification_text']['arguments']['data']['config'] = [
140  'text' => $this->renderer->getNotificationContent($page)
141  ];
142 
143  if ($isLastPage) {
144  $meta['notification_modal_' . $page['name']]['arguments']['data']['config']['options'] = [
145  'title' => $this->renderer->getNotificationTitle($page),
146  'buttons' => [
147  [
148  'text' => 'Done',
149  'actions' => [
150  [
151  'targetName' => '${ $.name }',
152  'actionName' => 'closeReleaseNotes'
153  ]
154  ],
155  'class' => 'release-notification-button-next'
156  ]
157  ],
158  ];
159 
160  $meta['notification_modal_' . $page['name']]['children']['notification_fieldset']['children']
161  ['notification_buttons']['children']['notification_button_next']['arguments']['data']['config'] = [
162  'buttonClasses' => 'hide-release-notification'
163  ];
164  } else {
165  $meta['notification_modal_' . $page['name']]['arguments']['data']['config']['options'] = [
166  'title' => $this->renderer->getNotificationTitle($page)
167  ];
168  }
169 
170  return $meta;
171  }
172 
179  private function hideNotification(array $meta)
180  {
181  $meta['notification_modal_1']['arguments']['data']['config']['options'] = [
182  'autoOpen' => false
183  ];
184 
185  return $meta;
186  }
187 
193  private function getNotificationContent()
194  {
195  $version = strtolower($this->getTargetVersion());
196  $edition = strtolower($this->productMetadata->getEdition());
197  $locale = strtolower($this->session->getUser()->getInterfaceLocale());
198 
199  $cacheKey = self::$cachePrefix . $version . "-" . $edition . "-" . $locale;
200  $modalContent = $this->cacheStorage->load($cacheKey);
201  if ($modalContent === false) {
202  $modalContent = $this->contentProvider->getContent($version, $edition, $locale);
203  $this->cacheStorage->save($modalContent, $cacheKey);
204  }
205 
206  return !$modalContent ? $modalContent : $this->unserializeContent($modalContent);
207  }
208 
215  private function unserializeContent($modalContent)
216  {
217  $result = false;
218 
219  try {
220  $result = $this->serializer->unserialize($modalContent);
221  } catch (\InvalidArgumentException $e) {
222  $this->logger->warning(
223  sprintf(
224  'Failed to unserialize the release notification content. The error is: %s',
225  $e->getMessage()
226  )
227  );
228  }
229 
230  return $result;
231  }
232 
239  private function getTargetVersion()
240  {
241  $metadataVersion = $this->productMetadata->getVersion();
242  $version = strstr($metadataVersion, '-', true);
243 
244  return !$version ? $metadataVersion : $version;
245  }
246 }
return false
Definition: gallery.phtml:36
foreach(array_keys($composerData['require']) as $requiredPackage) if(empty($edition)) if(!empty($opts['edition'])) $edition
$logger
$page
Definition: pages.php:8
__construct(ContentProviderInterface $contentProvider, NotificationRenderer $render, CacheInterface $cacheStorage, SerializerInterface $serializer, ProductMetadataInterface $productMetadata, Session $session, LoggerInterface $logger)