Skip to content

Commit da443ff

Browse files
author
rossbrandon
committed
MAGETWO-85063: Create a framework for dynamically composing what's new content
1 parent 42996a4 commit da443ff

File tree

21 files changed

+1480
-280
lines changed

21 files changed

+1480
-280
lines changed
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\ReleaseNotification\Model\Connector\Http;
8+
9+
use Magento\ReleaseNotification\Model\ContentProviderInterface;
10+
use Magento\Framework\App\ProductMetadataInterface;
11+
use Magento\Framework\App\Config\ScopeConfigInterface;
12+
use Magento\Backend\Model\Auth\Session;
13+
use Psr\Log\LoggerInterface;
14+
use Magento\Framework\HTTP\ClientInterface;
15+
16+
/**
17+
* Class HttpContentProvider
18+
*
19+
* Requests the release notification content data via an HTTP call to a REST API
20+
*/
21+
class HttpContentProvider implements ContentProviderInterface
22+
{
23+
/**
24+
* Path to the configuration value which contains an URL that provides the release notification data.
25+
*
26+
* @var string
27+
*/
28+
private static $notificationUrlConfigPath = 'releaseNotification/url/content';
29+
30+
/**
31+
* Version query parameter value for default release notification content if version specific content is not found.
32+
*
33+
* @var string
34+
*/
35+
private static $defaultContentVersion = 'default';
36+
37+
/**
38+
* @var ClientInterface
39+
*/
40+
private $httpClient;
41+
42+
/**
43+
* @var ScopeConfigInterface
44+
*/
45+
private $config;
46+
47+
/**
48+
* @var ProductMetadataInterface
49+
*/
50+
private $productMetadata;
51+
52+
/**
53+
* @var Session
54+
*/
55+
private $session;
56+
57+
/**
58+
* @var ResponseResolver
59+
*/
60+
private $responseResolver;
61+
62+
/**
63+
* @var LoggerInterface
64+
*/
65+
private $logger;
66+
67+
/**
68+
* @param ClientInterface $httpClient
69+
* @param ScopeConfigInterface $config
70+
* @param ProductMetadataInterface $productMetadata
71+
* @param Session $session
72+
* @param ResponseResolver $responseResolver
73+
* @param LoggerInterface $logger
74+
*/
75+
public function __construct(
76+
ClientInterface $httpClient,
77+
ScopeConfigInterface $config,
78+
ProductMetadataInterface $productMetadata,
79+
Session $session,
80+
ResponseResolver $responseResolver,
81+
LoggerInterface $logger
82+
) {
83+
$this->httpClient = $httpClient;
84+
$this->config = $config;
85+
$this->productMetadata = $productMetadata;
86+
$this->session = $session;
87+
$this->responseResolver = $responseResolver;
88+
$this->logger = $logger;
89+
}
90+
91+
/**
92+
* @inheritdoc
93+
*/
94+
public function getContent()
95+
{
96+
$result = false;
97+
98+
$url = $this->buildUrl(
99+
$this->config->getValue(self::$notificationUrlConfigPath),
100+
[
101+
'version' => $this->getTargetVersion(),
102+
'edition' => $this->productMetadata->getEdition(),
103+
'locale' => $this->session->getUser()->getInterfaceLocale()
104+
]
105+
);
106+
107+
try {
108+
$response = $this->getResponse($url);
109+
if ($response == "[]") {
110+
$response = $this->getDefaultContent();
111+
}
112+
$status = $this->httpClient->getStatus();
113+
$result = $this->responseResolver->getResult($response, $status);
114+
} catch (\Exception $e) {
115+
$this->logger->warning(
116+
sprintf(
117+
'Retrieving the release notification content from the Magento Marketing service has failed: %s',
118+
!empty($response) ? $response : 'Response body is empty.'
119+
)
120+
);
121+
$this->logger->critical(
122+
new \Exception(
123+
sprintf('Magento Marketing service CURL connection error: %s', $e->getMessage())
124+
)
125+
);
126+
}
127+
128+
return $result;
129+
}
130+
131+
/**
132+
* Returns the default content as a fallback if there is no content retrieved from the service/
133+
*
134+
* @return string
135+
*/
136+
private function getDefaultContent()
137+
{
138+
$url = $this->buildUrl(
139+
$this->config->getValue(self::$notificationUrlConfigPath),
140+
[
141+
'version' => self::$defaultContentVersion,
142+
'edition' => $this->productMetadata->getEdition(),
143+
'locale' => $this->session->getUser()->getInterfaceLocale()
144+
]
145+
);
146+
return $this->getResponse($url);
147+
}
148+
149+
/**
150+
* Returns the response body from the HTTP client
151+
*
152+
* @param $url
153+
*
154+
* @return string
155+
*/
156+
private function getResponse($url)
157+
{
158+
$this->httpClient->get($url);
159+
return $this->httpClient->getBody();
160+
}
161+
162+
/**
163+
* Returns the current Magento version used to retrieve the release notification content.
164+
* Version information after the dash (-) character is removed (ex. -dev or -rc).
165+
*
166+
* @return string
167+
*/
168+
private function getTargetVersion()
169+
{
170+
$metadataVersion = $this->productMetadata->getVersion();
171+
$version = strstr($metadataVersion, '-', true);
172+
173+
return !$version ? $metadataVersion : $version;
174+
}
175+
176+
/**
177+
* Builds the URL to request the release notification content data based on passed query parameters.
178+
*
179+
* @param $baseUrl
180+
* @param $queryData
181+
* @return string
182+
*/
183+
private function buildUrl($baseUrl, $queryData)
184+
{
185+
$query = http_build_query($queryData, '', '&');
186+
$baseUrl .= '?' . $query;
187+
return $baseUrl;
188+
}
189+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\ReleaseNotification\Model\Connector\Http;
8+
9+
use Magento\Framework\Serialize\SerializerInterface;
10+
use Magento\ReleaseNotification\Model\Connector\ResponseHandlerInterface;
11+
12+
/**
13+
* Class ResponseResolver
14+
*
15+
* Extract result from http response. Call response handler by status.
16+
*/
17+
class ResponseResolver
18+
{
19+
/**
20+
* @var SerializerInterface
21+
*/
22+
private $serializer;
23+
24+
/**
25+
* @var array
26+
*/
27+
private $responseHandlers;
28+
29+
/**
30+
* @param SerializerInterface $serializer
31+
* @param ResponseHandlerInterface[] $responseHandlers
32+
*/
33+
public function __construct(
34+
SerializerInterface $serializer,
35+
array $responseHandlers = []
36+
) {
37+
$this->serializer = $serializer;
38+
$this->responseHandlers = $responseHandlers;
39+
}
40+
41+
/**
42+
* @param string $response
43+
* @param int $status
44+
* @return bool|string
45+
*/
46+
public function getResult($response, $status)
47+
{
48+
$result = false;
49+
$responseBody = $this->serializer->unserialize($response);
50+
if (array_key_exists($status, $this->responseHandlers)) {
51+
$result = $this->responseHandlers[$status]->handleResponse($responseBody);
52+
}
53+
54+
return $result;
55+
}
56+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\ReleaseNotification\Model\Connector\ResponseHandler;
8+
9+
use Magento\ReleaseNotification\Model\Connector\ResponseHandlerInterface;
10+
11+
/**
12+
* Class NotificationResponse
13+
*
14+
* Retrieves release notification data from the response body
15+
*/
16+
class NotificationResponse implements ResponseHandlerInterface
17+
{
18+
/**
19+
* @param array $responseBody
20+
*
21+
* @return array|false
22+
*/
23+
public function handleResponse(array $responseBody)
24+
{
25+
return !empty($responseBody) ? $responseBody : false;
26+
}
27+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\ReleaseNotification\Model\Connector;
8+
9+
/**
10+
* Class ResponseHandlerInterface
11+
*
12+
* Represents an interface for response handler which process response body.
13+
*/
14+
interface ResponseHandlerInterface
15+
{
16+
/**
17+
* @param array $responseBody
18+
* @return bool|string
19+
*/
20+
public function handleResponse(array $responseBody);
21+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\ReleaseNotification\Model;
8+
9+
/**
10+
* Class ContentProviderInterface
11+
*
12+
* Requests the release notification content data from a defined service
13+
*/
14+
interface ContentProviderInterface
15+
{
16+
/**
17+
* Retrieves the release notification content data.
18+
*
19+
* Returns received content or FALSE in case of failure.
20+
*
21+
* @return string|false
22+
*/
23+
public function getContent();
24+
}

0 commit comments

Comments
 (0)