Skip to content

Commit fa6501c

Browse files
Igor Melnikovbuskamuza
authored andcommitted
MAGETWO-58692: Refactor Module_Webapi, Module_Elasticsearch
Introducing SerializerInterface
1 parent 9ba6fa8 commit fa6501c

File tree

5 files changed

+769
-226
lines changed

5 files changed

+769
-226
lines changed

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
use Magento\Webapi\Model\Cache\Type\Webapi as WebapiCache;
1010
use Magento\Webapi\Model\Config\Reader;
11+
use Magento\Framework\App\ObjectManager;
12+
use Magento\Framework\Serialize\SerializerInterface;
1113

1214
/**
1315
* Web API Config Model.
@@ -40,16 +42,25 @@ class Config
4042
*/
4143
protected $services;
4244

45+
/**
46+
* @var SerializerInterface
47+
*/
48+
private $serializer;
49+
4350
/**
4451
* Initialize dependencies.
4552
*
4653
* @param WebapiCache $cache
4754
* @param Reader $configReader
4855
*/
49-
public function __construct(WebapiCache $cache, Reader $configReader)
50-
{
56+
public function __construct(
57+
WebapiCache $cache,
58+
Reader $configReader,
59+
SerializerInterface $serializer = null
60+
) {
5161
$this->cache = $cache;
5262
$this->configReader = $configReader;
63+
$this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class);
5364
}
5465

5566
/**
@@ -62,10 +73,10 @@ public function getServices()
6273
if (null === $this->services) {
6374
$services = $this->cache->load(self::CACHE_ID);
6475
if ($services && is_string($services)) {
65-
$this->services = unserialize($services);
76+
$this->services = $this->serializer->unserialize($services);
6677
} else {
6778
$this->services = $this->configReader->read();
68-
$this->cache->save(serialize($this->services), self::CACHE_ID);
79+
$this->cache->save($this->serializer->serialize($this->services), self::CACHE_ID);
6980
}
7081
}
7182
return $this->services;

app/code/Magento/Webapi/Model/ServiceMetadata.php

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
use Magento\Webapi\Model\Config\Converter;
99
use Magento\Webapi\Model\Cache\Type\Webapi as WebApiCache;
10+
use Magento\Framework\App\ObjectManager;
11+
use Magento\Framework\Serialize\SerializerInterface;
1012

1113
/**
1214
* Service Metadata Model
@@ -74,6 +76,11 @@ class ServiceMetadata
7476
*/
7577
protected $typeProcessor;
7678

79+
/**
80+
* @var SerializerInterface
81+
*/
82+
private $serializer;
83+
7784
/**
7885
* Initialize dependencies.
7986
*
@@ -86,12 +93,14 @@ public function __construct(
8693
\Magento\Webapi\Model\Config $config,
8794
WebApiCache $cache,
8895
\Magento\Webapi\Model\Config\ClassReflector $classReflector,
89-
\Magento\Framework\Reflection\TypeProcessor $typeProcessor
96+
\Magento\Framework\Reflection\TypeProcessor $typeProcessor,
97+
SerializerInterface $serializer = null
9098
) {
9199
$this->config = $config;
92100
$this->cache = $cache;
93101
$this->classReflector = $classReflector;
94102
$this->typeProcessor = $typeProcessor;
103+
$this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class);
95104
}
96105

97106
/**
@@ -142,12 +151,18 @@ public function getServicesConfig()
142151
$servicesConfig = $this->cache->load(self::SERVICES_CONFIG_CACHE_ID);
143152
$typesData = $this->cache->load(self::REFLECTED_TYPES_CACHE_ID);
144153
if ($servicesConfig && is_string($servicesConfig) && $typesData && is_string($typesData)) {
145-
$this->services = unserialize($servicesConfig);
146-
$this->typeProcessor->setTypesData(unserialize($typesData));
154+
$this->services = $this->serializer->unserialize($servicesConfig);
155+
$this->typeProcessor->setTypesData($this->serializer->unserialize($typesData));
147156
} else {
148157
$this->services = $this->initServicesMetadata();
149-
$this->cache->save(serialize($this->services), self::SERVICES_CONFIG_CACHE_ID);
150-
$this->cache->save(serialize($this->typeProcessor->getTypesData()), self::REFLECTED_TYPES_CACHE_ID);
158+
$this->cache->save(
159+
$this->serializer->serialize($this->services),
160+
self::SERVICES_CONFIG_CACHE_ID
161+
);
162+
$this->cache->save(
163+
$this->serializer->serialize($this->typeProcessor->getTypesData()),
164+
self::REFLECTED_TYPES_CACHE_ID
165+
);
151166
}
152167
}
153168
return $this->services;
@@ -256,12 +271,18 @@ public function getRoutesConfig()
256271
$routesConfig = $this->cache->load(self::ROUTES_CONFIG_CACHE_ID);
257272
$typesData = $this->cache->load(self::REFLECTED_TYPES_CACHE_ID);
258273
if ($routesConfig && is_string($routesConfig) && $typesData && is_string($typesData)) {
259-
$this->routes = unserialize($routesConfig);
260-
$this->typeProcessor->setTypesData(unserialize($typesData));
274+
$this->routes = $this->serializer->unserialize($routesConfig);
275+
$this->typeProcessor->setTypesData($this->serializer->unserialize($typesData));
261276
} else {
262277
$this->routes = $this->initRoutesMetadata();
263-
$this->cache->save(serialize($this->routes), self::ROUTES_CONFIG_CACHE_ID);
264-
$this->cache->save(serialize($this->typeProcessor->getTypesData()), self::REFLECTED_TYPES_CACHE_ID);
278+
$this->cache->save(
279+
$this->serializer->serialize($this->routes),
280+
self::ROUTES_CONFIG_CACHE_ID
281+
);
282+
$this->cache->save(
283+
$this->serializer->serialize($this->typeProcessor->getTypesData()),
284+
self::REFLECTED_TYPES_CACHE_ID
285+
);
265286
}
266287
}
267288
return $this->routes;
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Webapi\Test\Unit\Model;
7+
8+
use Magento\Framework\Serialize\SerializerInterface;
9+
use Magento\Webapi\Model\Config;
10+
use Magento\Webapi\Model\Config\Reader;
11+
use Magento\Webapi\Model\Cache\Type\Webapi;
12+
13+
class ConfigTest extends \PHPUnit_Framework_TestCase
14+
{
15+
/**
16+
* @var Config
17+
*/
18+
private $config;
19+
20+
/**
21+
* @var Webapi|\PHPUnit_Framework_MockObject_MockObject
22+
*/
23+
private $webapiCacheMock;
24+
25+
/**
26+
* @var Reader|\PHPUnit_Framework_MockObject_MockObject
27+
*/
28+
private $configReaderMock;
29+
30+
/**
31+
* @var SerializerInterface|\PHPUnit_Framework_MockObject_MockObject
32+
*/
33+
private $serializerMock;
34+
35+
protected function setUp()
36+
{
37+
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
38+
39+
$this->webapiCacheMock = $this->getMock(\Magento\Webapi\Model\Cache\Type\Webapi::class, [], [], '', false);
40+
$this->configReaderMock = $this->getMock(\Magento\Webapi\Model\Config\Reader::class, [], [], '', false);
41+
$this->serializerMock = $this->getMock(SerializerInterface::class);
42+
43+
$this->config = $objectManager->getObject(
44+
Config::class,
45+
[
46+
'cache' => $this->webapiCacheMock,
47+
'configReader' => $this->configReaderMock,
48+
'serializer' => $this->serializerMock
49+
]
50+
);
51+
}
52+
53+
public function testGetServices()
54+
{
55+
$data = ['foo' => 'bar'];
56+
$serializedData = 'serialized data';
57+
$this->webapiCacheMock->expects($this->once())
58+
->method('load')
59+
->with(Config::CACHE_ID)
60+
->willReturn($serializedData);
61+
$this->serializerMock->expects($this->once())
62+
->method('unserialize')
63+
->with($serializedData)
64+
->willReturn($data);
65+
$this->config->getServices();
66+
$this->assertEquals($data, $this->config->getServices());
67+
}
68+
69+
public function testGetServicesNoCache()
70+
{
71+
$data = ['foo' => 'bar'];
72+
$serializedData = 'serialized data';
73+
$this->webapiCacheMock->expects($this->once())
74+
->method('load')
75+
->with(Config::CACHE_ID)
76+
->willReturn(false);
77+
$this->serializerMock->expects($this->never())
78+
->method('unserialize');
79+
$this->configReaderMock->expects($this->once())
80+
->method('read')
81+
->willReturn($data);
82+
$this->serializerMock->expects($this->once())
83+
->method('serialize')
84+
->with($data)
85+
->willReturn($serializedData);
86+
$this->webapiCacheMock->expects($this->once())
87+
->method('save')
88+
->with(
89+
$serializedData,
90+
Config::CACHE_ID
91+
);
92+
93+
$this->config->getServices();
94+
$this->assertEquals($data, $this->config->getServices());
95+
}
96+
}

0 commit comments

Comments
 (0)