1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MaglMarkdown\Cache; |
4
|
|
|
|
5
|
|
|
use Interop\Container\ContainerInterface; |
6
|
|
|
use Interop\Container\Exception\ContainerException; |
7
|
|
|
use Zend\Cache\Storage\StorageInterface; |
8
|
|
|
use Zend\Cache\StorageFactory; |
9
|
|
|
use Zend\ServiceManager\Exception\ServiceNotCreatedException; |
10
|
|
|
use Zend\ServiceManager\Exception\ServiceNotFoundException; |
11
|
|
|
use Zend\ServiceManager\FactoryInterface; |
12
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class CacheFactory |
16
|
|
|
* |
17
|
|
|
* @package MaglMarkdown\Cache |
18
|
|
|
*/ |
19
|
|
View Code Duplication |
class CacheFactory implements FactoryInterface |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Create service |
24
|
|
|
* |
25
|
|
|
* @param ServiceLocatorInterface $serviceLocator |
26
|
|
|
* @return StorageInterface |
27
|
|
|
*/ |
28
|
|
|
public function createService(ServiceLocatorInterface $serviceLocator) |
29
|
|
|
{ |
30
|
|
|
return $this->create($serviceLocator); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Create an object |
35
|
|
|
* |
36
|
|
|
* @param ContainerInterface $container |
37
|
|
|
* @param string $requestedName |
38
|
|
|
* @param null|array $options |
39
|
|
|
* @return StorageInterface |
40
|
|
|
* @throws \Zend\Cache\Exception\InvalidArgumentException |
41
|
|
|
* @throws \Interop\Container\Exception\NotFoundException |
42
|
|
|
* @throws ServiceNotFoundException if unable to resolve the service. |
43
|
|
|
* @throws ServiceNotCreatedException if an exception is raised when |
44
|
|
|
* creating a service. |
45
|
|
|
* @throws ContainerException if any other error occurs |
46
|
|
|
*/ |
47
|
|
|
public function __invoke(ContainerInterface $container, $requestedName, array $options = null) |
48
|
|
|
{ |
49
|
|
|
return $this->create($container); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param ServiceLocatorInterface|ContainerInterface $container |
54
|
|
|
* @return StorageInterface |
55
|
|
|
*/ |
56
|
|
|
protected function create($container) |
57
|
|
|
{ |
58
|
|
|
if (!$container instanceof ServiceLocatorInterface && !$container instanceof ContainerInterface) { |
59
|
|
|
throw new \InvalidArgumentException('Invalid container to create service'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$config = $container->get('config'); |
63
|
|
|
|
64
|
|
|
return StorageFactory::factory($config['magl_markdown']['cache']); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.