Skip to content

Commit 2bf84c2

Browse files
CABI-365::AdobeAdminIms to AdobeIms code migration-fixed static tests
1 parent 74ff6b3 commit 2bf84c2

File tree

7 files changed

+40
-160
lines changed

7 files changed

+40
-160
lines changed

app/code/Magento/AdminAdobeIms/Model/ImsConnection.php

Lines changed: 0 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -69,145 +69,6 @@ public function __construct(
6969
$this->adminAdobeImsLogger = $adminAdobeImsLogger;
7070
}
7171

72-
/**
73-
* Get authorization url
74-
*
75-
* @param string|null $clientId
76-
* @return string
77-
* @throws InvalidArgumentException
78-
*/
79-
public function auth(?string $clientId = null): string
80-
{
81-
$authUrl = $this->adminImsConfig->getAdminAdobeImsAuthUrl($clientId);
82-
return $this->getAuthorizationLocation($authUrl);
83-
}
84-
85-
/**
86-
* Test if given ClientID is valid and is able to return an authorization URL
87-
*
88-
* @param string $clientId
89-
* @return bool
90-
* @throws InvalidArgumentException
91-
*/
92-
public function testAuth(string $clientId): bool
93-
{
94-
$location = $this->auth($clientId);
95-
return $location !== '';
96-
}
97-
98-
/**
99-
* Get authorization location from adobeIMS
100-
*
101-
* @param string $authUrl
102-
* @return string
103-
* @throws InvalidArgumentException
104-
*/
105-
private function getAuthorizationLocation(string $authUrl): string
106-
{
107-
$curl = $this->curlFactory->create();
108-
109-
$curl->addHeader('Content-Type', 'application/x-www-form-urlencoded');
110-
$curl->addHeader('cache-control', 'no-cache');
111-
$curl->get($authUrl);
112-
113-
$this->validateResponse($curl);
114-
115-
return $curl->getHeaders()['location'] ?? '';
116-
}
117-
118-
/**
119-
* Validate authorization call response
120-
*
121-
* @param Curl $curl
122-
* @return void
123-
* @throws InvalidArgumentException
124-
*/
125-
private function validateResponse(Curl $curl): void
126-
{
127-
if (isset($curl->getHeaders()['location'])) {
128-
if (preg_match(
129-
'/error=([a-z_]+)/i',
130-
$curl->getHeaders()['location'],
131-
$error
132-
)
133-
&& isset($error[0], $error[1])
134-
) {
135-
throw new InvalidArgumentException(
136-
__('Could not connect to Adobe IMS Service: %1.', $error[1])
137-
);
138-
}
139-
}
140-
141-
if ($curl->getStatus() !== self::HTTP_REDIRECT_CODE) {
142-
throw new InvalidArgumentException(
143-
__('Could not get a valid response from Adobe IMS Service.')
144-
);
145-
}
146-
}
147-
148-
/**
149-
* Verify if access_token is valid
150-
*
151-
* @param string|null $token
152-
* @param string $tokenType
153-
* @return bool
154-
* @throws AuthorizationException
155-
*/
156-
public function validateToken(?string $token, string $tokenType = 'access_token'): bool
157-
{
158-
$isTokenValid = false;
159-
160-
if ($token === null) {
161-
return false;
162-
}
163-
164-
$curl = $this->curlFactory->create();
165-
166-
$curl->addHeader('Content-Type', 'application/x-www-form-urlencoded');
167-
$curl->addHeader('cache-control', 'no-cache');
168-
169-
$curl->post(
170-
$this->adminImsConfig->getValidateTokenUrl($token, $tokenType),
171-
[]
172-
);
173-
174-
if ($curl->getBody() === '') {
175-
throw new AuthorizationException(
176-
__('Could not verify the access_token')
177-
);
178-
}
179-
180-
$body = $this->json->unserialize($curl->getBody());
181-
182-
if (isset($body['valid'])) {
183-
$isTokenValid = (bool)$body['valid'];
184-
}
185-
186-
if (!$isTokenValid && isset($body['reason'])) {
187-
$this->adminAdobeImsLogger->info($tokenType . ' is not valid. Reason: ' . $body['reason']);
188-
}
189-
190-
return $isTokenValid;
191-
}
192-
193-
/**
194-
* Get token response
195-
*
196-
* @param string $code
197-
* @return TokenResponseInterface
198-
* @throws AdobeImsAuthorizationException
199-
*/
200-
public function getTokenResponse(string $code): TokenResponseInterface
201-
{
202-
try {
203-
return $this->token->execute($code);
204-
} catch (AuthorizationException $exception) {
205-
throw new AdobeImsAuthorizationException(
206-
__($exception->getMessage())
207-
);
208-
}
209-
}
210-
21172
/**
21273
* Get profile url
21374
*

app/code/Magento/AdminAdobeIms/Model/LogOut.php

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77

88
namespace Magento\AdminAdobeIms\Model;
99

10-
use Magento\AdminAdobeIms\Exception\AdobeImsAuthorizationException;
11-
use Magento\AdminAdobeIms\Service\ImsConfig;
10+
use Magento\AdobeImsApi\Api\ConfigInterface;
11+
use Magento\AdobeImsApi\Api\GetProfileInterface;
1212
use Magento\AdminAdobeIms\Api\ImsLogOutInterface;
13+
use Magento\Framework\Exception\AuthorizationException;
1314
use Magento\Framework\Exception\LocalizedException;
1415
use Magento\Framework\HTTP\Client\CurlFactory;
1516
use Psr\Log\LoggerInterface;
@@ -35,14 +36,14 @@ class LogOut implements ImsLogOutInterface
3536
private CurlFactory $curlFactory;
3637

3738
/**
38-
* @var ImsConfig
39+
* @var ConfigInterface
3940
*/
40-
private ImsConfig $adminImsConfig;
41+
private ConfigInterface $imsConfig;
4142

4243
/**
43-
* @var ImsConnection
44+
* @var GetProfileInterface
4445
*/
45-
private ImsConnection $adminImsConnection;
46+
private GetProfileInterface $profile;
4647

4748
/**
4849
* @var Auth
@@ -51,22 +52,22 @@ class LogOut implements ImsLogOutInterface
5152

5253
/**
5354
* @param LoggerInterface $logger
54-
* @param ImsConfig $adminImsConfig
55+
* @param ConfigInterface $imsConfig
5556
* @param CurlFactory $curlFactory
56-
* @param ImsConnection $adminImsConnection
57+
* @param GetProfileInterface $profile
5758
* @param Auth $auth
5859
*/
5960
public function __construct(
6061
LoggerInterface $logger,
61-
ImsConfig $adminImsConfig,
62+
ConfigInterface $imsConfig,
6263
CurlFactory $curlFactory,
63-
ImsConnection $adminImsConnection,
64+
GetProfileInterface $profile,
6465
Auth $auth
6566
) {
6667
$this->logger = $logger;
6768
$this->curlFactory = $curlFactory;
68-
$this->adminImsConfig = $adminImsConfig;
69-
$this->adminImsConnection = $adminImsConnection;
69+
$this->imsConfig = $imsConfig;
70+
$this->profile = $profile;
7071
$this->auth = $auth;
7172
}
7273

@@ -112,7 +113,7 @@ private function externalLogOut(string $accessToken): void
112113
$curl->addHeader('cache-control', 'no-cache');
113114

114115
$curl->post(
115-
$this->adminImsConfig->getBackendLogoutUrl($accessToken),
116+
$this->imsConfig->getBackendLogoutUrl($accessToken),
116117
[]
117118
);
118119

@@ -133,11 +134,11 @@ private function externalLogOut(string $accessToken): void
133134
private function checkUserProfile(string $accessToken): bool
134135
{
135136
try {
136-
$profile = $this->adminImsConnection->getProfile($accessToken);
137+
$profile = $this->profile->getProfile($accessToken);
137138
if (!empty($profile['email'])) {
138139
return true;
139140
}
140-
} catch (AdobeImsAuthorizationException $exception) {
141+
} catch (AuthorizationException $exception) {
141142
return false;
142143
}
143144
return false;

app/code/Magento/AdobeIms/Model/Config.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,8 @@ private function getAdminScopes(): string
338338
}
339339

340340
/**
341+
* Get ims Urls
342+
*
341343
* @param string $urlType
342344
* @return string
343345
*/
@@ -478,7 +480,9 @@ private function deleteConfig(string $path): void
478480
}
479481

480482
/**
481-
* @inerhitDoc
483+
* Retrieve Organization Id
484+
*
485+
* @return string
482486
*/
483487
public function getOrganizationId(): string
484488
{

app/code/Magento/AdobeIms/Model/GetAuthorizationUrl.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@ public function __construct(
4040
}
4141

4242
/**
43-
* @inerhitDoc
43+
* Get authorization url
44+
*
45+
* @param string|null $clientId
46+
* @return string
47+
* @throws InvalidArgumentException
4448
*/
4549
public function auth(?string $clientId = null): string
4650
{
@@ -49,7 +53,11 @@ public function auth(?string $clientId = null): string
4953
}
5054

5155
/**
52-
* @inerhitDoc
56+
* Test if given ClientID is valid and is able to return an authorization URL
57+
*
58+
* @param string $clientId
59+
* @return bool
60+
* @throws InvalidArgumentException
5361
*/
5462
public function testAuth(string $clientId): bool
5563
{

app/code/Magento/AdobeIms/Model/GetOrganizations.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ public function checkOrganizationMembership(string $access_token): void
6060

6161
$orgCheckUrl = $this->imsConfig->getOrganizationMembershipUrl($configuredOrganizationId);
6262
$curl->get($orgCheckUrl);
63-
return;
6463
if ($curl->getBody() === '') {
6564
throw new AdobeImsOrganizationAuthorizationException(
6665
__('Could not check Organization Membership. Response is empty.')

app/code/Magento/AdobeIms/Model/IsTokenValid.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ public function __construct(
5353
$this->logger = $logger;
5454
}
5555

56+
/**
57+
* Validate token
58+
*
59+
* @param string|null $token
60+
* @param string $tokenType
61+
* @return bool
62+
* @throws AuthorizationException
63+
*/
5664
public function validateToken(?string $token, string $tokenType = 'access_token'): bool
5765
{
5866
$isTokenValid = false;

app/code/Magento/AdobeImsApi/Api/ConfigInterface.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,7 @@ public function getCertificateUrl(string $fileName): string;
124124
public function getOrganizationMembershipUrl(string $orgId): string;
125125

126126
/**
127-
* Enable Admin Adobe IMS Module and set Client ID and Client Secret and
128-
* Organization ID and Two Factor Enabled
127+
* Enable Admin Adobe IMS Module and set Client ID and Client Secret and Organization ID and Two Factor Enabled
129128
*
130129
* @param string $clientId
131130
* @param string $clientSecret

0 commit comments

Comments
 (0)